有没有办法在lubridate中自动分配DST转换? [英] Is there a way to assign DST transitions automatically in lubridate?

查看:70
本文介绍了有没有办法在lubridate中自动分配DST转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CSV文件中有日期/时间对,看起来像是

I have date-time pairs in a csv file that look like

2012年1月4日

位于一列和

12:06:08 AM

12:06:08 AM

在相邻列中.它们以当地时间记录(即,它们在适当的时间切换到PST和PDT),但是文件中没有tz或DST指示符.唯一可见的检测方法是时间序列执行有趣的操作.例如,在2012年11月4日,我有一个类似的时间序列

in the neighboring column. They are recorded in local time (i.e., they switch to PST and PDT at the appropriate times), but there is no tz or DST indicator in the file. The only visible way to detect that is that the sequence of times does funny things. For example, on November 4, 2012, I have a sequence of times like

12:51:20 AM 上午1:13:08 凌晨1:24:58 凌晨1:40:28 凌晨1:48:08 上午1:54:08 上午1:56:58 凌晨1:04:28 凌晨1:05:48 上午1:07:18 上午1:15:00 上午1:39:08 2:05:38 AM

12:51:20 AM 1:13:08 AM 1:24:58 AM 1:40:28 AM 1:48:08 AM 1:54:08 AM 1:56:58 AM 1:04:28 AM 1:05:48 AM 1:07:18 AM 1:15:00 AM 1:39:08 AM 2:05:38 AM

PST可能以上午1:04:28读数开始,但没有指示器.

PST presumably begins with the 1:04:28 AM reading, but there is no indicator.

是否有一种直接的方法来正确地分配时区(大概使用lubridate)?该文件很长,因此我不想一次遍历一个读数,因为我担心这可能会花费一些时间.春天,我将不得不相反地做同样的事情.

Is there a straightforward approach to assigning time zones properly (presumably using lubridate)? The file is long, so I'd rather not loop through one reading at a time, as I fear that could take some time. I'll have to do the same thing in reverse for the spring.

推荐答案

这是不可能的.无法确定"11/4/2012 1:04:28 AM"是PST,而实际上不是在"11/4/2012 12:51:20 AM""11/4/2012 1:13:08 AM" PDT之间的观察结果.

This isn't possible. There's no way to know with certainty that "11/4/2012 1:04:28 AM" is PST and not actually an observation between "11/4/2012 12:51:20 AM" and "11/4/2012 1:13:08 AM" PDT.

如果您确定观测值在文件中已排序,则可以将其转换为POSIXt并采用矢量的diff.任何负值将是DST更改.但是,如果在DST更改之间的两次观察之间的时间间隔大于1小时,您可能会错过一些机会.

If you're certain the observations are ordered in the file, you could convert them to POSIXt and take the diff of the vector. Any negative values will be DST changes. You may miss some, however, if the time between observations across a DST change is greater than 1 hour.

Lines <- "11/4/2012 12:51:20 AM
11/4/2012 01:13:08 AM
11/4/2012 01:24:58 AM
11/4/2012 01:40:28 AM
11/4/2012 01:48:08 AM
11/4/2012 01:54:08 AM
11/4/2012 01:56:58 AM
11/4/2012 01:04:28 AM
11/4/2012 01:05:48 AM
11/4/2012 01:07:18 AM
11/4/2012 01:15:00 AM
11/4/2012 01:39:08 AM
11/4/2012 02:05:38 AM"

x <- scan(con <- textConnection(Lines), what="", sep="\n")
close(con)
diff(strptime(x, format="%m/%d/%Y %I:%M:%S %p"))
# Time differences in mins
#  [1]  21.800000  11.833333  15.500000   7.666667   6.000000   2.833333
#  [7] -52.500000   1.333333   1.500000   7.700000  24.133333  86.500000

这篇关于有没有办法在lubridate中自动分配DST转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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