在不改变显示的情况下修改 POSIXct 对象的时区 [英] Modifying timezone of a POSIXct object without changing the display

查看:25
本文介绍了在不改变显示的情况下修改 POSIXct 对象的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 POSIXct 对象,并且想更改它的 tz 属性而不用 R 来解释它(解释它意味着更改日期时间在屏幕上的显示方式).

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 displayed on the screen).

一些背景:我正在使用 S.Urbanek 的 fasttime 包,它可以非常快速地获取字符串并将其转换为 POSIXct.问题是该字符串应表示GMT"中的日期时间,而我的数据并非如此.

Some background: I am using the fasttime package from S.Urbanek, which take strings and cast it to POSIXct very quickly. Problem is that the string should represent a datetime in "GMT" and it's not the case of my data.

我最终得到一个带有 tz=GMTPOSIXct 对象,实际上它是 tz=GMT+1,如果我改变时区带

I end up with a POSIXct object with tz=GMT, in reality it is tz=GMT+1, if I change the timezone with

attr(datetime, "tzone") <- "Europe/Paris";
datetime  <- .POSIXct(datetime,tz="Europe/Paris"); 

然后它将显示"为GMT+2(底层值永远不会改变).

then it will be "displayed" as GMT+2 (the underlying value never change).

这是一个例子

datetime=as.POSIXct("2011-01-01 12:32:23.234",tz="GMT")
attributes(datetime)
#$tzone
#[1] "GMT"
datetime
#[1] "2011-01-01 12:32:23.233 GMT"

如何在没有 R 的情况下更改此属性来解释它,即如何更改 tzone 并且仍然将日期时间显示为 "2011-01-01 12:32:23.233" ?

How can I change this attribute without R to interpret it aka how can I change tzone and still have datetime displayed as "2011-01-01 12:32:23.233" ?

编辑/解决方案,@GSee 的解决方案相当快,lubridate::force_tz 非常慢

EDIT/SOLUTION, @GSee's solution is reasonably fast, lubridate::force_tz very slow

datetime=rep(as.POSIXct("2011-01-01 12:32:23.234",tz="GMT"),1e5)
f <- function(x,tz) return(as.POSIXct(as.numeric(x), origin="1970-01-01", tz=tz))
> system.time(datetime2 <- f(datetime,"Europe/Paris"))
   user  system elapsed 
   0.01    0.00    0.02 
> system.time(datetime3 <- force_tz(datetime,"Europe/Paris"))
   user  system elapsed 
   5.94    0.02    5.98 
identical(datetime2,datetime3)
[1] TRUE

推荐答案

我之前的解决方案是将字符值传递给 origin(即origin="1970-01-01").这只在这里工作是因为一个错误 (#PR14973)现在已修复 在 R-devel 中.

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 强制为 POSIXct,而不是"GMT" 正如它所记录的那样.行为已更改以匹配文档,在这种情况下,这意味着您必须为 originas.POSIXct 调用指定时区.

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"

这也适用于旧版本的 R.

This will also works in older versions of R.

这篇关于在不改变显示的情况下修改 POSIXct 对象的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆