I'm using strptime(...)
in a function of my package. I need to parse a string using specific local settings and used Sys.setlocale
as a workaround to get english localization settings. To reduce side effects, the previous local setting is restored afterwards.
The basic code fragment of the function looks as follows:
#parameter settings
sometext <- "Mon, 14 Mar 2011 23:42:16 GMT"
timeFormat <- "%a, %d %b %Y %H:%M:%S"
timeZone <- "GMT"
#get current locale
loc <- Sys.getlocale("LC_TIME")
#set british localization
dummy <- Sys.setlocale("LC_TIME", "en_GB.UTF-8")
#parse datetime string
time <- strptime(sometext, format = timeFormat, tz= timeZone)
#set local back
dummy <- Sys.setlocale("LC_TIME", loc)
Unfortunately, a colleague of mine gets the following warning when using this function:
In Sys.setlocale("LC_TIME", "en_GB.UTF-8") :
OS reports request to set locale to "en_GB.UTF-8" cannot be honored
On my computer everything works fine. Is there a better (and independent from installed R localization) way of performing this task? Generally I would like to use strptime as it allows a very flexible way of parsing datetime strings.
I am quite sure that the "en_GB.UTF-8" locale is not installed on your college's computer. The easiest way could be to install it :) Well, this is not trivial with every OS.
Other option could be to use a standard locale which can be found on every computer. As your added example shows no special format, you could try with setting LC_TIME
to C
, which works under Linux and Windows also. With that locale, your given example will work like a charm. See:
> Sys.setlocale("LC_TIME", "C")
> strptime("Mon, 14 Mar 2011 23:42:16 GMT", format = "%a, %d %b %Y %H:%M:%S", tz="GMT")
[1] "2011-03-14 23:42:16 GMT"
Or otherwise you should transform your data - e.g.: write a short function to substitute all week- and months' name to standard strings and restructure your imported strings to standard ones.
I have tried your code on my Windows machine and get the same error. For reference, the results of Sys.getlocale("LC_TIME"):
> Sys.getlocale("LC_TIME")
[1] "English_United Kingdom.1252"
I suspect this might be a fairly standard locale.
But I also suspect that the better way of approaching this problem is to use some of the functions in package lubridate, which makes it easy to work with dates.
You don't give enough details in your question what you are tring to do, but I am guessing that "sometext" is in a specific expected format, such as DMY or YMD. Lubridate provides functions to parse dates in any specified format, e.g. dmy(), ymd(), mdy() - you get the picture.
If you provide more details about your real problem, we might be able to help more specifically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With