0

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

YJZ
  • 3,934
  • 11
  • 43
  • 67

1 Answers1

2

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • You can also use `tz="US/Eastern"` – Chris Holbrook Sep 11 '15 at 02:55
  • I agree that main issue here is timestamp is incorrectly formatted. An important point in this answer too is that EST and EDT are not zones but offsets from UTC (-5 and -4, respectively). The great thing about specifying a zone is that the offsets are automatically applied based on local time in a given zone. But if for some reason your time is in EDT during Jan. then you would want to use `%Z` format within `as.POSIXlt` to specify the offset from UTC rather than zone. – Chris Holbrook Sep 11 '15 at 03:09
  • 1
    Ah, yes. An example of "States rights", a hallowed Murkin tradition. On my machine there is a `../zoneinfo/US/ directory` whose contents are: `"Alaska" ,"Central", "Hawaii", "Mountain", "Samoa", "Aleutian", "East-Indiana", "Indiana-Starke", "Pacific", "Arizona", "Eastern", "Michigan", "Pacific-New"` – IRTFM Sep 11 '15 at 03:46
  • Hi thanks @BondedDust. I tried `originTime = as.POSIXlt('2000-01-02 00:00:00', tz="EST5EDT", usetz=T)` `as.POSIXlt( 1 * 3600*24*7 , origin = originTime)` and the result is still `[1] "2000-01-08 19:00:00 EST"` Do you know how to fix this? thank you! – YJZ Sep 11 '15 at 19:01
  • Try using the `"+"`-function. It has a POSIXt method: `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") # [1] "2000-01-09 00:00:00 EST"` – IRTFM Sep 11 '15 at 19:27