是否有与 strtotime 等效的 R 语言
PHP 有一个很棒的函数 strtotime
,它可以接受包含几乎任何日期格式的任何字符串并返回时间(自 1970 年以来的秒数)。例如,它比 strptime 更具前瞻性,因为如果日期格式发生更改,我的脚本也不会中断。 R有类似的东西吗?
(从今天开始,我不需要 strtotime 的时间相关功能,但我确信在某些时候我会需要 strtotime("next Wednesday")
或 strtotime( “上个月的第一天”)
,所以如果你知道 R 扩展也能做到这一点,那么我很想听听它!)
更新:如果有人(可能是我)未来的某个时刻)想要为了尝试用 R 或任何其他语言实现这个,我找到了 它的源代码。相关文件是timelib.h、timelib.re和timelib_structs.h。它似乎都是标准的C语言并且是独立的,不需要引入PHP头文件。但是编译过程将*.re文件编译成真正的C语言,因此您需要至少安装和编译PHP一次。
调用它的代码 也非常简单(在撰写本文时,请参阅第 1428 行到第 1433 行;同一函数中上面较长的代码只是为了获取当前时间,以便在相对时间中使用)。
PHP has this wonderful function, strtotime
, that takes any string containing just about any date format and returns a time (secs-since-1970). It is more future-proof than strptime
, for instance, because if the date format changes my script does not break. Does R have anything similar?
(I don't need the time-relative feature of strtotime as of today, but I'm sure at some point I'll need strtotime("next Thursday")
, or strtotime("first day of last month")
, so if you know R extensions that do that too, then I'd love to hear about it!)
UPDATE: If anyone (possibly me at some point in the future) want to try implementing this in R, or any other language, I tracked down the source code for it. The relevant files are timelib.h, timelib.re and timelib_structs.h. It appears to all be standard C and standalone, no PHP headers to bring in. However the compile process compiles the *.re file into real C, so you will need to install and compile PHP at least once.
The code that calls it is also quite straightforward (see lines 1428 to 1433, at the time of writing; the longer code above it in the same function is just to get the current time, for use in relative times).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢 PHP 中的这个函数,但不幸的是 R 中似乎没有等效的函数(并且语言参考中没有提及,最值得注意的是 http://rss.acs.unt.edu/Rdoc/library/base/html/DateTimeClasses.html)。
I love that function in PHP, but unfortunately there seems to not be an equivalent in R (and none is mentioned in the language reference, most notably http://rss.acs.unt.edu/Rdoc/library/base/html/DateTimeClasses.html).