the time my data are in EST
time zone, and I try to use this time zone.
I want to count the week (in local time, not GMT
), so I manually define an originTime
in EDT
originTime = as.POSIXlt('2000-01-02 00:00:00 EDT')
dt2 = data.frame(time=c(as.POSIXlt('2000-01-09 00:00:05 EDT')))
dt2$week = as.integer( floor( ( as.numeric(dt2$time) - as.numeric(originTime) ) /(3600*24*7) ) )
dt2$wday = weekdays(dt2$time)
This works.
Now I want to find out, what's one week after a given time?
> as.POSIXlt( 1 * 3600*24*7 , origin = originTime)
[1] "2000-01-08 19:00:00 EST"
Here's the problem, R seems to think originTime
is in GMT
. Can somebody help? Thanks
Two serious problems. EDT does not really exist, and even if it did it would not be appropriate for a January date. The (US) Eastern timezone is "EST5EDT" to make it distinct from the Ozzie EST. (Furthermore these may be different on different OSes.) Safest would be tz="America/New_York"
. For data entry, you need to use the 'tz' parameter AND change the default (FALSE) setting of 'usetz':
(originTime = as.POSIXlt('2000-01-02 00:00:00', format="%Y-%m-%d %H:%M:%S",
tz="EST5EDT", usetz=TRUE) )
[1] "2000-01-02 EST"
A test using "%Z" which is only for output:
> format( as.POSIXlt('2000-01-02 00:00:00', format="%Y-%m-%d %H:%M:%S",
tz="America/New_York", usetz=TRUE) ,
format="%Y-%m-%d %H:%M:%S %Z")
[1] "2000-01-02 00:00:00 EST"
I've never used the origin argument in as.POSIXlt so cannot really explain why it fails to deliver the expected result I've always used +.POSIXt
or seq.POSIXt
to construct intervals:
format(as.POSIXlt('2000-01-02 00:00:00', tz="America/New_York", usetz=TRUE)+ 1*3600*24*7,
format="%Y-%m-%d %H:%M:%S %Z") # %Z is only used for output
# [1] "2000-01-09 00:00:00 EST" A week later at midnight.
I think the reason one needs to force a time printing with a format argument is that midnight-times are shortened to just printing the date with no further H:M:S information.