Guard against accidental time-zone conversion

后端 未结 3 1378
臣服心动
臣服心动 2020-12-06 19:12

In R, I have a bunch of datetime values that I measure in GMT. I keep running into accidents where some function or another loses the timezone on my values, or even loses t

3条回答
  •  执念已碎
    2020-12-06 19:34

    This behaviour is documented in ?c, ?DateTimeClasses and ?unlist:

    From ?DateTimeClasses:

    Using c on "POSIXlt" objects converts them to the current time zone, and on "POSIXct" objects drops any "tzone" attributes (even if they are all marked with the same time zone).*

    From ?c:

    c is sometimes used for its side effect of removing attributes except names.*


    That said, my testing indicates that the integrity of your data remains intact, despite using c or unlist. For example:

    x <- structure(1317830532, class = c("POSIXct", "POSIXt"), 
                     tzone = "GMT")
    y <- structure(1317830532+3600, class = c("POSIXct", "POSIXt"), 
                     tzone = "PST8PDT")
    x
    [1] "2011-10-05 16:02:12 GMT"
    
    y
    [1] "2011-10-05 10:02:12 PDT"
    
    strftime(c(x, y), format="%Y/%m/%d %H:%M:%S", tz="GMT")
    [1] "2011/10/05 16:02:12" "2011/10/05 17:02:12"
    
    strftime(c(x, y), format="%Y/%m/%d %H:%M:%S", tz="PST8PDT")
    [1] "2011/10/05 09:02:12" "2011/10/05 10:02:12"
    
    strftime(unlist(y), format="%Y/%m/%d %H:%M:%S", tz="PST8PDT")
    [1] "2011/10/05 10:02:12"
    

    Your Mars Rover should be OK if you use R to keep track of dates.

提交回复
热议问题