As.POSIXlt ignores tz argument

Pretty simple, just not understanding what the tz argument is, if not for that ...

dateIWantToConvert <- as.Date("2013-06-01") #the format of my starting object as.POSIXlt(dateIWantToConvert, tz="America/Chicago") [1] "2013-06-01 UTC" 

There are many of them in data.frame, so efficiency is important.

+3
source share
3 answers

A more reliable way to do this is to use strptime . This takes any tz argument from OlsonNames() . And reliable, I mean, that this should be OS independent.

 (vec.Date <- strptime("2013-06-01", format = "%Y-%m-%d", tz = "America/Chicago")) # "2013-06-01 CDT" class(vec.Date) # [1] "POSIXlt" "POSIXt" 

And for your efficiency question, you really should use strptime . See The difference between as.POSIXct / as.POSIXlt and strptime for converting character vectors to POSIXct / POSIXlt

+3
source

You can try without as.Date() :

 > as.POSIXlt("2013-06-01", tz="America/Chicago") #[1] "2013-06-01 CDT" 
+3
source

According to ?timezones invalid time zones are treated as UTC often without warning. Your particular environment probably does not know how to interpret America / Chicago. The documentation indicates that it is incompatible.

Try using "CDT" instead of "America / Chicago." In addition, the ?timezones have details on how to submit an R list of time zone formats for interpretation.

UPDATE: as.Date returns a date object, not a string of characters. as.POSIXlt will be, quote: "Dates without time are considered to be at midnight UTC", from as.POSIXlt . Thus, it seems that x is a date object in as.POSIXlt and has no time value, the tz parameter is ignored and replaced with UTC .

As a result, another parameter should ensure that the date object passed to as.POSIXlt already has a time and time zone. Or, convert the object to a character string before passing it to as.POSIXlt so that it uses its own tz option.

0
source

All Articles