当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R lubridate fit_to_timeline 将 POSIXlt 日期时间与时间线相匹配


POSIXlt 格式允许您创建由于夏令时和其他约定而在现实生活中不存在的瞬间。 fit_to_timeline 将 POSIXlt 日期时间与实际时间相匹配。如果某个瞬间不存在,适合时间线会将其替换为 NA。如果某个时刻确实存在,但与不正确的时区/夏令时组合配对,fit_to_timeline 将返回具有正确组合的时刻。

用法

fit_to_timeline(lt, class = "POSIXct", simple = FALSE)

参数

lt

POSIXlt 日期时间对象。

class

说明要返回的对象类型的字符串,POSIXlt 或 POSIXct。默认为 POSIXct。这是为了避免不必要的转换而进行的优化。

simple

如果属实,润滑不尝试检测无意义的 time-dates 或更正时区。不会生成 NA,而是返回最有意义的有效日期。请参阅示例。

不包含虚幻日期时间的 POSIXct 或 POSIXlt 对象

例子

if (FALSE) {

tricky <- structure(list(
  sec = c(5, 0, 0, -1),
  min = c(0L, 5L, 5L, 0L),
  hour = c(2L, 0L, 2L, 2L),
  mday = c(4L, 4L, 14L, 4L),
  mon = c(10L, 10L, 2L, 10L),
  year = c(112L, 112L, 110L, 112L),
  wday = c(0L, 0L, 0L, 0L),
  yday = c(308L, 308L, 72L, 308L),
  isdst = c(1L, 0L, 0L, 1L)
),
.Names = c(
  "sec", "min", "hour", "mday", "mon",
  "year", "wday", "yday", "isdst"
),
class = c("POSIXlt", "POSIXt"),
tzone = c("America/Chicago", "CST", "CDT")
)

tricky
## [1] "2012-11-04 02:00:00 CDT" Doesn't exist because clocks "fall back" to 1:00 CST
## [2] "2012-11-04 00:05:00 CST" Times are still CDT, not CST at this instant
## [3] "2010-03-14 02:00:00 CDT" DST gap
## [4] "2012-11-04 01:59:59 CDT" Does exist, but has deceptive internal structure

fit_to_timeline(tricky)
## Returns:
## [1] "2012-11-04 02:00:00 CST" instant paired with correct tz & DST combination
## [2] "2012-11-04 00:05:00 CDT" instant paired with correct tz & DST combination
## [3] NA - fake time changed to NA (compare to as.POSIXct(tricky))
## [4] "2012-11-04 01:59:59 CDT" -real instant, left as is

fit_to_timeline(tricky, simple = TRUE)
## Returns valid time-dates by extrapolating CDT and CST zones:
## [1] "2012-11-04 01:00:05 CST" "2012-11-04 01:05:00 CDT"
## [3] "2010-03-14 03:05:00 CDT" "2012-11-04 01:59:59 CDT"
}
源代码:R/update.r

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Fit a POSIXlt date-time to the timeline。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。