0

I'm looking to create a sequence of time stamps starting and ending at a specific date for every three hours. The ways that I have tried work well, but all have the same problem. Somewhere in the middle of the sequence there is a time conversion between PST to PDT and I am not sure why. Below are the codes that I have tried, similar to the questions asked on this site beforehand (Question1 and Question2):

 TIME1 <- as.POSIXct( "2000-01-01 02:00:00", tz = "America/Los_Angeles")
 TIME2 <- as.POSIXct( "2016-02-29 23:00:00" , tz = "America/Los_Angeles")
 temp <- seq(from = TIME1, to = TIME2, by = "3 hours")

When I look at the output of temp, it looks good at first except when looking at the specified interval below

 temp[720:745] 

[1] "2000-03-30 23:00:00 PST" "2000-03-31 02:00:00 PST" "2000-03-31 05:00:00 PST" "2000-03-31 08:00:00 PST" "2000-03-31 11:00:00 PST" "2000-03-31 14:00:00 PST"
[7] "2000-03-31 17:00:00 PST" "2000-03-31 20:00:00 PST" "2000-03-31 23:00:00 PST" "2000-04-01 02:00:00 PST" "2000-04-01 05:00:00 PST" "2000-04-01 08:00:00 PST"
[13] "2000-04-01 11:00:00 PST" "2000-04-01 14:00:00 PST" "2000-04-01 17:00:00 PST" "2000-04-01 20:00:00 PST" "2000-04-01 23:00:00 PST" "2000-04-02 03:00:00 PDT"
[19] "2000-04-02 06:00:00 PDT" "2000-04-02 09:00:00 PDT" "2000-04-02 12:00:00 PDT" "2000-04-02 15:00:00 PDT" "2000-04-02 18:00:00 PDT" "2000-04-02 21:00:00 PDT"
[25] "2000-04-03 00:00:00 PDT" "2000-04-03 03:00:00 PDT"

Notice on line [1] of the output, it goes from "2000-03-30 23:00:00 PST" to "2000-03-31 02:00:00 PST" which is 3 hours, but on line [13] it goes from "2000-04-01 23:00:00 PST" to "2000-04-02 03:00:00 PDT". Notice the time zone change there. This happens throughout the sequence. Any ideas how to avoid such a result? I believe it is converting automatically for me the daylight savings time, which I don't want. If I can keep it as LST (local standard time), that will be great.

UPDATE: I have found recently this suggestion, where we just convert to timezone "UTC" to do away with daylight savings time. It works, just marks all time stamps as UTC. However, it would be nice to see if there is still a way to keep it as LST.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
lurodrig
  • 99
  • 3
  • 8
  • 1
    Possible duplicate of [Handling data on the days when we switch to daylight savings time and back in R](http://stackoverflow.com/questions/13865172/handling-data-on-the-days-when-we-switch-to-daylight-savings-time-and-back-in-r) – dayne Jul 24 '16 at 18:24
  • 1
    LST = local standard time? – Ben Bolker Jul 24 '16 at 18:28
  • also take a look at the `dst` and `dhours` functions in the `lubridate` package. They are relevant here. – shayaa Jul 24 '16 at 19:53
  • @BenBolker Correct on the LST meaning. – lurodrig Jul 26 '16 at 00:33
  • @shayaa dst and dhours may be a great alternative after creating the time series. Thank you for the suggestion. – lurodrig Jul 26 '16 at 00:33

2 Answers2

4

If you want Pacific standard time (PST) as the time zone and don't want to switch to Pacific daylight time (PDT), why not just specify that? "America/Los Angeles" means by definition the time zone that is in effect in Los Angeles at a particular time (including DST switches) ...

TIME1 <- as.POSIXct( "2000-01-01 02:00:00", tz = "PST")
TIME2 <- as.POSIXct( "2016-02-29 23:00:00" , tz = "PST")
temp <- seq(from = TIME1, to = TIME2, by = "3 hours") 
temp[720:745]
 [1] "2000-03-30 23:00:00 PST" "2000-03-31 02:00:00 PST"
 [3] "2000-03-31 05:00:00 PST" "2000-03-31 08:00:00 PST"
 [5] "2000-03-31 11:00:00 PST" "2000-03-31 14:00:00 PST"
 [7] "2000-03-31 17:00:00 PST" "2000-03-31 20:00:00 PST"
 [9] "2000-03-31 23:00:00 PST" "2000-04-01 02:00:00 PST"
[11] "2000-04-01 05:00:00 PST" "2000-04-01 08:00:00 PST"
[13] "2000-04-01 11:00:00 PST" "2000-04-01 14:00:00 PST"
[15] "2000-04-01 17:00:00 PST" "2000-04-01 20:00:00 PST"
[17] "2000-04-01 23:00:00 PST" "2000-04-02 02:00:00 PST"
[19] "2000-04-02 05:00:00 PST" "2000-04-02 08:00:00 PST"
[21] "2000-04-02 11:00:00 PST" "2000-04-02 14:00:00 PST"
[23] "2000-04-02 17:00:00 PST" "2000-04-02 20:00:00 PST"
[25] "2000-04-02 23:00:00 PST" "2000-04-03 02:00:00 PST"

The referenced question, which recommends using UTC, comments that three-letter time zone codes can be ambiguous (e.g. Australia and North America both have eastern standard time, although this reference suggests that the Australian one is coded as AEST). You should certainly be careful to check your results carefully, as time-zone coding in this way has a good chance of being operating-system-specific ...

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 1
    `"PST"` is not a valid timezone, and therefore does not mean "Pacific standard time". It's an abbreviation for Pacific standard time, but may be an abbreviation for others timezones as well. – Joshua Ulrich Jul 25 '16 at 00:44
  • OK; do you suggest I delete this answer? (FWIW, it looks like `tz=PST` does give the same answer as `tz=GMT-8`, so perhaps the OS time/date library has guessed correctly? – Ben Bolker Jul 25 '16 at 02:14
  • It doesn't look like `tz = "PST"` gives the same answer as `tz = "GMT-8"`. The *format* of the printed answers are the same (save for the abbreviated timezone), but the underlying values are not (e.g. compare the results from calling `unclass` on each object). When `tz = "PST"`, the underlying values are all UTC (e.g. `.POSIXct(unclass(temp), tz="UTC")`). No need to delete your answer... though it might be good to edit and update/clarify whatever makes you think it should be deleted. – Joshua Ulrich Jul 25 '16 at 02:39
1

Notice on line [1] of the output, it goes from "2000-03-30 23:00:00 PST" to "2000-03-31 02:00:00 PST" which is 3 hours, but on line [13] it goes from "2000-04-01 23:00:00 PST" to "2000-04-02 03:00:00 PDT".

Note that "2000-04-01 23:00:00 PST" to "2000-04-02 03:00:00 PDT" is also 3 hours.

If you want a timezone that doesn't observe daylight saving time, but is the same offset from GMT as your current local time, then you can try the suggestion in the Time zone names section of ?timezone:

Most platforms support time zones of the form ‘GMT+n’ and ‘GMT-n’, which assume at a fixed offset from UTC (hence no DST).

R> TIME1 <- as.POSIXct( "2000-01-01 02:00:00", tz = "GMT-8")
R> TIME2 <- as.POSIXct( "2016-02-29 23:00:00", tz = "GMT-8")
R> temp <- seq(from = TIME1, to = TIME2, by = "3 hours")
R> temp[720:745] 
 [1] "2000-03-30 23:00:00 GMT" "2000-03-31 02:00:00 GMT"
 [3] "2000-03-31 05:00:00 GMT" "2000-03-31 08:00:00 GMT"
 [5] "2000-03-31 11:00:00 GMT" "2000-03-31 14:00:00 GMT"
 [7] "2000-03-31 17:00:00 GMT" "2000-03-31 20:00:00 GMT"
 [9] "2000-03-31 23:00:00 GMT" "2000-04-01 02:00:00 GMT"
[11] "2000-04-01 05:00:00 GMT" "2000-04-01 08:00:00 GMT"
[13] "2000-04-01 11:00:00 GMT" "2000-04-01 14:00:00 GMT"
[15] "2000-04-01 17:00:00 GMT" "2000-04-01 20:00:00 GMT"
[17] "2000-04-01 23:00:00 GMT" "2000-04-02 02:00:00 GMT"
[19] "2000-04-02 05:00:00 GMT" "2000-04-02 08:00:00 GMT"
[21] "2000-04-02 11:00:00 GMT" "2000-04-02 14:00:00 GMT"
[23] "2000-04-02 17:00:00 GMT" "2000-04-02 20:00:00 GMT"
[25] "2000-04-02 23:00:00 GMT" "2000-04-03 02:00:00 GMT"
R> attr(temp, "tzone")
[1] "GMT-8"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • I was able to replicate your output, however, I did get warnings that GMT-8 is an unknown timezone. But this may be a good alternative as well. Thank you for the suggestion – lurodrig Jul 26 '16 at 00:34