Modifying timezone of a POSIXct object without changing the display

后端 未结 3 444
无人及你
无人及你 2021-02-01 23:45

I have a POSIXct object and would like to change it\'s tz attribute WITHOUT R to interpret it (interpret it would mean to change how the datetime is di

3条回答
  •  猫巷女王i
    2021-02-02 00:14

    EDITED:

    My previous solution was passing a character value to origin (i.e.origin="1970-01-01"). That only worked here because of a bug (#PR14973) that has now been fixed in R-devel.

    origin was being coerced to POSIXct using the tz argument of the as.POSIXct call, and not "GMT" as it was documented to do. The behavior has been changed to match the documentation which, in this case, means that you have to specify your timezone for both the origin and the as.POSIXct call.

    datetime
    #[1] "2011-01-01 12:32:23.233 GMT"
    as.POSIXct(as.numeric(datetime), origin=as.POSIXct("1970-01-01", tz="Europe/Paris"),
               tz="Europe/Paris")
    #[1] "2011-01-01 12:32:23.233 CET"
    

    This will also works in older versions of R.

提交回复
热议问题