Question

I've been searching around on stackoverflow for about 2 hours now and I can't seem to answer this, very basic question. Not sure if it's how I expect things to work or R that is the issue. I haven't found this specific question here, but if it's documented and I didn't find it in the last few hours, please post the link to the question because I haven't found it!

I've been using this website, http://www.timeanddate.com/worldclock/timezone.html?n=250&syear=2000, as a guide to tell me when daylight savings time (DST) is in effect.

I'm at UTC-5 or EST in the winter and UTC - 4 or EDT during the summer - Canada/Eastern timezone.

I have data that were collected in GMT and I would like to convert them to local time, accounting for daylight savings time. I made a test dataframe ("tzs", below). The column "DT" shows the date and time in GMT and the Localtime column shows what the local time SHOULD be, according to the website I'm working from.

I have tried the following changes:

with base:

attr(tzs$DT, "tzone") <- "Canada/Eastern"

with lubridate:

tzs$DT <- with_tz(tzs$DT, tz = "Canada/Eastern")

and I have tried a number of different timezones, such as EST5EDT, EST, etc. None seem to end out with the desired changes for daylight savings (eg, when I apply the change, DT does not always equal LocalTime).

Am I expecting the wrong date/times in my manually typed "LocalTime" field? Or is there actually some error in the way R is converting?

Here is my test data.frame:

> df
                    DT           Localtime
1  2002-10-27 02:00:00 2002-10-26 21:00:00
2  2003-04-06 01:59:59 2003-04-05 20:59:59
3  2003-04-06 02:00:00 2003-04-05 22:00:00
4  2003-10-26 01:59:59 2003-10-25 21:59:59
5  2003-10-26 02:00:00 2003-10-25 21:00:00
6  2004-04-04 01:59:59 2004-04-03 20:59:59
7  2004-04-04 02:00:00 2004-04-03 22:00:00
8  2004-10-31 01:59:59 2004-10-30 21:59:59
9  2009-11-01 02:00:00 2009-30-31 21:00:00
10 2010-03-14 01:59:59 2010-03-13 20:59:59
11 2010-03-14 02:00:00 2010-03-13 22:00:00
12 2010-11-07 01:59:59 2010-11-06 21:59:59
13 2010-11-07 02:00:00 2010-11-06 21:00:00
14 2011-03-13 01:59:59 2011-03-12 20:59:59
15 2011-03-13 02:00:00 2011-03-12 22:00:00
16 2011-11-06 01:59:59 2011-11-05 21:59:59

tzs <- structure(list(DT = structure(c(1035684000, 1049594399, 1049594400, 
1067133599, 1067133600, 1081043999, 1081044000, 1099187999, 1257040800, 
1268531999, 1268532000, 1289095199, 1289095200, 1299981599, 1299981600, 
1320544799), class = c("POSIXct", "POSIXt"), tzone = "GMT"), 
    Localtime = structure(c(1L, 2L, 3L, 5L, 4L, 6L, 7L, 8L, 9L, 
    10L, 11L, 13L, 12L, 14L, 15L, 16L), .Label = c("2002-10-26 21:00:00", 
    "2003-04-05 20:59:59", "2003-04-05 22:00:00", "2003-10-25 21:00:00", 
    "2003-10-25 21:59:59", "2004-04-03 20:59:59", "2004-04-03 22:00:00", 
    "2004-10-30 21:59:59", "2009-30-31 21:00:00", "2010-03-13 20:59:59", 
    "2010-03-13 22:00:00", "2010-11-06 21:00:00", "2010-11-06 21:59:59", 
    "2011-03-12 20:59:59", "2011-03-12 22:00:00", "2011-11-05 21:59:59"
    ), class = "factor")), .Names = c("DT", "Localtime"), row.names = c(NA, 
16L), class = "data.frame")
Was it helpful?

Solution

The problem is that DST occurs at the specified time in the local timezone. Your example expects DST in Canada/Eastern to take effect at 2AM GMT.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top