t.ct = as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.lt = as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.st = strptime("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
These seem to be the same times:
> t.ct -t.lt
Time difference of 0 secs
> t.ct -t.st
Time difference of 0 secs
> str(t.ct)
POSIXct[1:1], format: "2009-01-04 21:19:00"
> str(t.lt)
POSIXlt[1:1], format: "2009-01-04 21:19:00"
> str(t.st)
POSIXlt[1:1], format: "2009-01-04 21:19:00"
>
But these appear to have different timezone information in them, and it is not what I'd expect:
> strftime(t.ct,"%Y-%m-%d %H:%M:%S %z")
[1] "2009-01-04 21:19:00 -0500"
> strftime(t.lt,"%Y-%m-%d %H:%M:%S %z")
[1] "2009-01-04 21:19:00 +1200"
> strftime(t.st,"%Y-%m-%d %H:%M:%S %z")
[1] "2009-01-04 21:19:00 +1200"
>
The timezone on my Mac is:
> Sys.timezone()
[1] "America/New_York"
The questions Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt and as.POSIXlt ignores tz argument seemed related, but didn't clarify this for me.
How do I definitively set a time and use it?
Update:
From user3293236's answer below, it seems one should always declare the timezone of the string, and if you are parsing the '-hhmm' offset, then always use tz="UTC"
:
t.ct = as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC")
t.lt = as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC")
t.st = strptime("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="UTC")