當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。