Bug 16764 - as.difftime erroneous values on daylight savings changeover days
Summary: as.difftime erroneous values on daylight savings changeover days
Status: UNCONFIRMED
Alias: None
Product: R
Classification: Unclassified
Component: Language (show other bugs)
Version: R 3.2.2
Hardware: x86_64/x64/amd64 (64-bit) OS X Mavericks
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-03-13 23:12 UTC by cory
Modified: 2016-12-22 09:16 UTC (History)
1 user (show)

See Also:


Attachments
Make as.difftime independent from daylight savings in the local timezone (603 bytes, patch)
2016-12-22 09:14 UTC, Johannes Ranke
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description cory 2016-03-13 23:12:00 UTC
On daylight savings changeover days (such as today 2016-03-13 for timezone America/New_York) as.difftime produces erroneous results:

> as.difftime('18:00',format="%H:%M")
Time difference of 17 hours


The result should be 18 hours. This is due to the time changeover at 02:00 as illustrated here:

> as.difftime('01:00',format="%H:%M")
Time difference of 1 hours

> as.difftime('02:00',format="%H:%M")
Time difference of NA secs

> as.difftime('03:00',format="%H:%M")
Time difference of 2 hours


The problem arises from the expression in the as.difftime function:

difftime(strptime(tim, format = format), strptime("0:0:0", 
            format = "%X"), units = units)

The two calls to strptime give results in different timezones leading to the erroneous as.difftime value:

> strptime('18:00',format='%H:%M',tz='America/New_York')
[1] "2016-03-13 18:00:00 EDT"

> strptime('0:0:0',format='%X',tz='America/New_York')
[1] "2016-03-13 EST"
Comment 1 Johannes Ranke 2016-12-22 09:14:31 UTC
Created attachment 2197 [details]
Make as.difftime independent from daylight savings in the local timezone
Comment 2 Johannes Ranke 2016-12-22 09:16:20 UTC
The fact that different time zones are set can be reproduced without being on a system that happens to have its clock set to a changeover day (here R 3.2.3 on Windows 7):

> strptime('2016-03-13 18:00', format = '%Y-%m-%d %H:%M', tz = 'America/New_York')
[1] "2016-03-13 18:00:00 EDT"

> strptime('2016-03-13 00:00', format = '%Y-%m-%d %H:%M', tz = 'America/New_York')
[1] "2016-03-13 EST"

I think the behaviour of as.difftime can be fixed by using a time zone without daylight savings in the code for as.difftime:

> t_18 <- strptime('2016-03-13 18:00',
                   format = '%Y-%m-%d %H:%M', tz = 'America/New_York')
> t_0 <- strptime('2016-03-13 00:00', 
                  format = '%Y-%m-%d %H:%M', tz = 'America/New_York')

> difftime(t_18, t_0)
Time difference of 17 hours
> difftime(t_18, t_0, tz = "UTC")
Time difference of 18 hours

I am attaching a patch. However, I think that verifying the fix needs a system with its clock set to a changeover day.