[Rd] {round,trunc}.POSIXt and daylight savings time (PR#1543)

macq@llnl.gov macq@llnl.gov
Sun, 12 May 2002 07:24:23 +0200 (MET DST)


I have found what looks like a small problem in trunc.POSIXt() 
involving the transition to/from standard time and daylight savings 
time. Assuming my assessment is correct, I have a potential solution 
to offer.

If a time in daylight savings time is rounded such that the rounded 
value is on the other side of the transition, the isdst element does 
not get changed accordingly. I have tested only the case of PDT 
rounded to PST in the US/Pacific time zone. I have tested it on 
(only) two different platforms, with slightly different results.  See 
below.

More specifically, in the US/Pacific time zone the transition from 
standard to daylight savings time takes place at 02:00 on the first 
Sunday in April, which in 2001 was 1 April.   My example is a vector 
x of two dates. x[1] is 2001-4-1 3:15, which is in daylight savings 
time. Rounded to the nearest midnight it is 2001-4-1 00:00, which is 
in standard time.

A potential solution is to insert
      x$isdst <- as.POSIXlt(format(x))$isdst
into trunc.POSIXt as its penultimate expression.

-Don

#### Solaris #####
>  x <- c(as.POSIXct('2001-4-1 3:15'),as.POSIXct('2001-5-2 17:56'))
>  print(x)
[1] "2001-04-01 03:15:00 PDT" "2001-05-02 17:56:00 PDT"
>
>  tmp <- round.POSIXt(x,'day')
>  print(tmp)    ## note that tmp[1] is labeled PDT, not PST
[1] "2001-04-01 PDT" "2001-05-03 PDT"
>
### converting back to POSIXct reveals the problem more explicitly
>  print( as.POSIXct(tmp) )
[1] "2001-03-31 23:00:00 PST" "2001-05-03 00:00:00 PDT"
>
>  tmp$isdst <- as.POSIXlt(format(tmp))$isdst
>  print( as.POSIXct(tmp) )
[1] "2001-04-01 PST" "2001-05-03 PDT"
>
>  version
          _
platform sparc-sun-solaris2.7
arch     sparc
os       solaris2.7
system   sparc, solaris2.7
status
major    1
minor    5.0
year     2002
month    04
day      29
language R

>  Sys.getenv('TZ')
           TZ
"US/Pacific"

###### Darwin ######
>  x <- c(as.POSIXct('2001-4-1 3:15'),as.POSIXct('2001-5-2 17:56'))
>  print(x)
[1] "2001-04-01 03:15:00 PDT" "2001-05-02 17:56:00 PDT"
>
>  tmp <- round.POSIXt(x,'day')
>  print(tmp)
[1] "2001-04-01 PDT" "2001-05-03 PDT"
>
### gets NA instead
>  print( as.POSIXct(tmp) )
[1] NA               "2001-05-03 PDT"
>
>  tmp$isdst <- as.POSIXlt(format(tmp))$isdst
>  print( as.POSIXct(tmp) )
[1] "2001-04-01 PST" "2001-05-03 PDT"
>
>  version
          _
platform powerpc-apple-darwin5.5
arch     powerpc
os       darwin5.5
system   powerpc, darwin5.5
status
major    1
minor    5.0
year     2002
month    04
day      29
language R

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._