以明确的方式在日期和秒之间切换 [英] Switch between dates and seconds in a well defined manner

查看:116
本文介绍了以明确的方式在日期和秒之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行离散事件模拟,时间来自日期。我认为,当我将所有日期转换为整数(相对时间)时,模拟运行得更快。
什么是最好的方式,以一个明确的方式在我想要的之间切换 date




  • 设置参考时间(例如1970-01-01 00:00:00 GMT code>或2016-01-01 00:00:00 GMT)手动,

  • 时区和

  • 原始(不可能在 lubridate ?)



我以为我可以使用来源为此目的,但不影响结果:

 > as.numeric(as.POSIXct(2016-01-01 00:00:00 GMT,origin =2016-01-01,tz =GMT))
> as.numeric(as.POSIXct(2016-01-01 00:00:00 GMT,origin =1970-01-01,tz =GMT))

都会导致 [1] 1451606400



(只有 tz 参数更改结果,这当然可以:

> as.numeric(as.POSIXct(2016-01-01 00:00:00 CEST,tz =America / Chicago))

[1] 1451628000

解决方案

code> difftime()来计算一些时间戳和参考时间之间的差异:

  as.numeric(as.POSIXct(2016-01-01 00:00:00,tz =GMT),
as.POSIXct(1970-01-01 00:00:00 ,tz =GMT),units =secs))
## [1] 1451606400

通过为单位选择另一个值,您还可以获取分钟数,小时数等。



您为 origin 的两个选项获得相同结果的原因是,此参数仅在将数字转换为日期时使用。然后,该数字将被解释为从您传递给该函数的原点开始的秒数。



在内部,POSIXct对象始终保存为自1970年1月20日00:00:00 UTC开始的秒数,与执行转换时指定的原始码无关。因此,转换为数字可以为任何原点选择提供相同的结果。



您可以查看 as.POSIXct( )

  ##类'character'的S3方法
as.POSIXlt x,tz =,格式,...)

##类'数字'的$ s
as.POSIXlt(x,tz =,origin,... )

如您所见, origin 只有数字的方法的参数,但不是字符


I run discrete event simulations where the time originates from dates. I think that simulations run much faster, when I convert all the dates to integers (relative time in seconds). What is the best way, to switch between date and seconds in a well definied way where I want to

  • set the reference time (e.g. "1970-01-01 00:00:00 GMT" or "2016-01-01 00:00:00 GMT") manually,
  • the time zone and
  • the origin (Not possible in lubridate?)

I thought I can use the origin for this purpose but it does not influence the result:

> as.numeric(as.POSIXct("2016-01-01 00:00:00 GMT",origin="2016-01-01",tz="GMT"))
> as.numeric(as.POSIXct("2016-01-01 00:00:00 GMT",origin="1970-01-01",tz="GMT"))

both result in [1] 1451606400.

(Only the tz argument changes the result, which is ok of course:
> as.numeric(as.POSIXct("2016-01-01 00:00:00 CEST", tz= "America/Chicago"))
[1] 1451628000)

解决方案

You can use difftime() to calculate the difference between some timestamp and a reference time:

as.numeric(difftime(as.POSIXct("2016-01-01 00:00:00",tz="GMT"),
    as.POSIXct("1970-01-01 00:00:00",tz="GMT"), units = "secs"))
## [1] 1451606400

By choosing another value for units, you could also get the number of minutes, hours, etc.

The reason that you get the same result for both choices of origin is that this argument is only intended to be used when converting a number into a date. Then, the number is interpreted as seconds since the origin that you pass to the function.

Internally, a POSIXct object is always stored as seconds since 1970-01-01 00:00:00, UTC, independent of the origin that you specified when doing the conversion. And accordingly, converting to numeric gives the same result for any choice of origin.

You can have a look at the documentation of as.POSIXct():

## S3 method for class 'character'
as.POSIXlt(x, tz = "", format, ...)

## S3 method for class 'numeric'
as.POSIXlt(x, tz = "", origin, ...)

As you can see, origin is only an argument for the method for numeric, but not for character.

这篇关于以明确的方式在日期和秒之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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