Odd POSIXct Function Behavior In R

后端 未结 2 1224
醉梦人生
醉梦人生 2021-01-19 14:50

I\'m working with the POSIXct data type in R. In my work, I incorporate a function that returns two POSIXct dates in a vector. However, I am discovering some unexpected beha

相关标签:
2条回答
  • 2021-01-19 15:26

    From ?c.POSIXct:

    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).

    See also here.

    0 讨论(0)
  • 2021-01-19 15:47

    The problem is that the function c removes the timezone attribute:

    attributes(myTime)
    #$class
    #[1] "POSIXct" "POSIXt" 
    #
    #$tzone
    #[1] "UTC"
    
    attributes(c(myTime))
    #$class
    #[1] "POSIXct" "POSIXt"
    

    To fix, you can e.g. use the setattr function from data.table, to modify the attribute in place:

    (setattr(c(myTime), 'tzone', attributes(myTime)$tzone))
    #[1] "2015-01-01 UTC"
    
    0 讨论(0)
提交回复
热议问题