如何在 R 中将山地标准时间 (MST) 转换为山地夏令时间 (MDT)

Posted

技术标签:

【中文标题】如何在 R 中将山地标准时间 (MST) 转换为山地夏令时间 (MDT)【英文标题】:How to convert Mountain Standard Time (MST) to Mountain Daylight Time (MDT) in R 【发布时间】:2020-02-04 06:52:04 【问题描述】:

我正在尝试将 datetime 变量从 MST 转换为 MDT。

我想要得到的是 datetime 的一个新变量,它反映了山区夏令时 (MDT)。

我不确定如何从这个问题开始。

我正在考虑首先使用 MST 时区强制原始日期时间,然后将其转换为 MDT,但是我不知道该怎么做。

更新:###############

经过一番挖掘,我发现原来的datetime 起源于UTC -07:00,这意味着它不反映夏令时。但是,我们需要 datetime 来遵循夏令时。

容易看出时间变化的数据:

df <- structure(list(datetime.char = c("2019-03-09 21:00", "2019-03-09 21:05", 
"2019-03-09 21:10", "2019-03-09 21:15", "2019-03-09 21:20", "2019-03-09 21:25", 
"2019-03-09 21:30", "2019-03-09 21:35", "2019-03-09 21:40", "2019-03-09 21:45", 
"2019-03-09 21:50", "2019-03-09 21:55", "2019-03-09 22:00", "2019-03-09 22:05", 
"2019-03-09 22:10", "2019-03-09 22:15", "2019-03-09 22:20", "2019-03-09 22:25", 
"2019-03-09 22:30", "2019-03-09 22:35", "2019-03-09 22:40", "2019-03-09 22:45", 
"2019-03-09 22:50", "2019-03-09 22:55", "2019-03-09 23:00", "2019-03-09 23:05", 
"2019-03-09 23:10", "2019-03-09 23:15", "2019-03-09 23:20", "2019-03-09 23:25", 
"2019-03-09 23:30", "2019-03-09 23:35", "2019-03-09 23:40", "2019-03-09 23:45", 
"2019-03-09 23:50", "2019-03-09 23:55", "2019-03-10 00:00", "2019-03-10 00:05", 
"2019-03-10 00:10", "2019-03-10 00:15", "2019-03-10 00:20", "2019-03-10 00:25", 
"2019-03-10 00:30", "2019-03-10 00:35", "2019-03-10 00:40", "2019-03-10 00:45", 
"2019-03-10 00:50", "2019-03-10 00:55", "2019-03-10 01:00", "2019-03-10 01:05", 
"2019-03-10 01:10", "2019-03-10 01:15", "2019-03-10 01:20", "2019-03-10 01:25", 
"2019-03-10 01:30", "2019-03-10 01:35", "2019-03-10 01:40", "2019-03-10 01:45", 
"2019-03-10 01:50", "2019-03-10 01:55", "2019-03-10 02:00", "2019-03-10 02:05", 
"2019-03-10 02:10", "2019-03-10 02:15", "2019-03-10 02:20", "2019-03-10 02:25", 
"2019-03-10 02:30", "2019-03-10 02:35", "2019-03-10 02:40", "2019-03-10 02:45", 
"2019-03-10 02:50", "2019-03-10 02:55", "2019-03-10 03:00", "2019-03-10 03:05", 
"2019-03-10 03:10", "2019-03-10 03:15", "2019-03-10 03:20", "2019-03-10 03:25", 
"2019-03-10 03:30", "2019-03-10 03:35", "2019-03-10 03:40", "2019-03-10 03:45", 
"2019-03-10 03:50", "2019-03-10 03:55", "2019-03-10 04:00")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -85L), spec = structure(list(
    cols = list(datetime.char = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

这是我们使用@d.b 解决方案获得的结果的屏幕截图。但是它并不能完全提供预期的输出。

11 月月份时钟变化

这是 11 月的虚拟数据:

df <- structure(list(datetime.char = c("2017-11-04 21:00:00", "2017-11-04 21:05:00", 
"2017-11-04 21:10:00", "2017-11-04 21:15:00", "2017-11-04 21:20:00", 
"2017-11-04 21:25:00", "2017-11-04 21:30:00", "2017-11-04 21:35:00", 
"2017-11-04 21:40:00", "2017-11-04 21:45:00", "2017-11-04 21:50:00", 
"2017-11-04 21:55:00", "2017-11-04 22:00:00", "2017-11-04 22:05:00", 
"2017-11-04 22:10:00", "2017-11-04 22:15:00", "2017-11-04 22:20:00", 
"2017-11-04 22:25:00", "2017-11-04 22:30:00", "2017-11-04 22:35:00", 
"2017-11-04 22:40:00", "2017-11-04 22:45:00", "2017-11-04 22:50:00", 
"2017-11-04 22:55:00", "2017-11-04 23:00:00", "2017-11-04 23:05:00", 
"2017-11-04 23:10:00", "2017-11-04 23:15:00", "2017-11-04 23:20:00", 
"2017-11-04 23:25:00", "2017-11-04 23:30:00", "2017-11-04 23:35:00", 
"2017-11-04 23:40:00", "2017-11-04 23:45:00", "2017-11-04 23:50:00", 
"2017-11-04 23:55:00", "2017-11-05 00:00:00", "2017-11-05 00:05:00", 
"2017-11-05 00:10:00", "2017-11-05 00:15:00", "2017-11-05 00:20:00", 
"2017-11-05 00:25:00", "2017-11-05 00:30:00", "2017-11-05 00:35:00", 
"2017-11-05 00:40:00", "2017-11-05 00:45:00", "2017-11-05 00:50:00", 
"2017-11-05 00:55:00", "2017-11-05 01:00:00", "2017-11-05 01:00:00", 
"2017-11-05 01:05:00", "2017-11-05 01:05:00", "2017-11-05 01:10:00", 
"2017-11-05 01:10:00", "2017-11-05 01:15:00", "2017-11-05 01:15:00", 
"2017-11-05 01:20:00", "2017-11-05 01:20:00", "2017-11-05 01:25:00", 
"2017-11-05 01:25:00", "2017-11-05 01:30:00", "2017-11-05 01:30:00", 
"2017-11-05 01:35:00", "2017-11-05 01:35:00", "2017-11-05 01:40:00", 
"2017-11-05 01:40:00", "2017-11-05 01:45:00", "2017-11-05 01:45:00", 
"2017-11-05 01:50:00", "2017-11-05 01:50:00", "2017-11-05 01:55:00", 
"2017-11-05 01:55:00", "2017-11-05 02:00:00", "2017-11-05 02:05:00", 
"2017-11-05 02:10:00", "2017-11-05 02:15:00", "2017-11-05 02:20:00", 
"2017-11-05 02:25:00", "2017-11-05 02:30:00", "2017-11-05 02:35:00", 
"2017-11-05 02:40:00", "2017-11-05 02:45:00", "2017-11-05 02:50:00", 
"2017-11-05 02:55:00", "2017-11-05 03:00:00", "2017-11-05 03:05:00", 
"2017-11-05 03:10:00", "2017-11-05 03:15:00", "2017-11-05 03:20:00", 
"2017-11-05 03:25:00", "2017-11-05 03:30:00", "2017-11-05 03:35:00", 
"2017-11-05 03:40:00", "2017-11-05 03:45:00", "2017-11-05 03:50:00", 
"2017-11-05 03:55:00", "2017-11-05 04:00:00")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-97L))

有趣的是,当我进行 3 月转换时,有一个明显的警告消息,提示 12 个条目无法解析。

df <- df %>% 
  mutate(datetime = ymd_hms(df$datetime.char, tz = "MST7MDT"))

如果我打印 df$date(请参阅 cmets 中的@d.b 代码),最终结果中的输出将保持 MST/MDT tz 并且在切换时可见:

[55] "2019-03-10 01:30:00 MST" "2019-03-10 01:35:00 MST" "2019-03-10 01:40:00 MST" "2019-03-10 01:45:00 MST" "2019-03-10 01:50:00 MST" "2019-03-10 01:55:00 MST"
[61] "2019-03-10 03:00:00 MDT" "2019-03-10 03:05:00 MDT" "2019-03-10 03:10:00 MDT" "2019-03-10 03:15:00 MDT" "2019-03-10 03:20:00 MDT" "2019-03-10 03:25:00 MDT"

这似乎不适用于 11 月的转化

【问题讨论】:

“它还没有时区属性”:不是真的,它有tzone="UTC",所以你的进程中有一些东西正在设置它。我不清楚时间是否是错误的 UTC 并且应该是美国山区,或者您是否认为无论一年中的哪一天都应该强制 MST 或 MDT。你能澄清一下吗? (顺便说一句:attr(df$original, "tzone") &lt;- "US/Mountain" 从 UTC 更改为山区时间。) 【参考方案1】:

anytime 的选项

library(anytime)
anytime("2018-03-12 02:03:12", tz = "MST7MDT")

【讨论】:

以上是关于如何在 R 中将山地标准时间 (MST) 转换为山地夏令时间 (MDT)的主要内容,如果未能解决你的问题,请参考以下文章

在javascript中将日期从“Thu Jun 09 2011 00:00:00 GMT+0530(印度标准时间)”转换为“YYYY-MM-DD”

如何在javascript中将日期时间从用户时区转换为EST [重复]

在文本文件中将纪元转换为标准的bash脚本[重复]

R将年-月-日-小时本地标准时间转换为UTC

如何将此时间戳格式转换为标准时间戳格式? [复制]

需要在 oracle 中将任何时区转换为 GMT 时区