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


R lubridate ymd_hms 解析包含年、月、日、小時、分鍾和秒組成部分的日期時間。


將存儲為字符或數字向量的日期轉換為 POSIXct 對象。 ymd_hms() 係列函數可識別所有非字母數字分隔符(如果 frac = TRUE 則 "." 除外)並正確處理異構日期時間表示。要更靈活地處理異構格式,請參閱低級解析器 parse_date_time()

用法

ymd_hms(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

ymd_hm(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

ymd_h(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

dmy_hms(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

dmy_hm(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

dmy_h(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

mdy_hms(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

mdy_hm(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

mdy_h(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

ydm_hms(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

ydm_hm(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

ydm_h(
  ...,
  quiet = FALSE,
  tz = "UTC",
  locale = Sys.getlocale("LC_TIME"),
  truncated = 0
)

參數

...

年、月、日、時、分、秒格式的日期字符向量

quiet

合乎邏輯的。如果是 TRUE ,函數將在不顯示常規消息的情況下進行計算。

tz

一個字符串,指定使用哪個時區來解析日期。該字符串必須是用戶操作係統可識別的時區。

locale

要使用的區域設置,請參閱locales。在 Linux 係統上,您可以使用 system("locale -a") 列出所有已安裝的區域設置。

truncated

整數,表示可以缺少多少種格式。查看具體信息。

POSIXct 日期時間對象的向量

細節

ymd_hms() 函數自動將通用協調時區 (UTC) 分配給解析的日期。可以使用 force_tz() 更改該時區。

日期時間數據中最常見的不規則類型是由於四舍五入或時間戳不可用而導致的截斷。如果 truncated 參數非零,ymd_hms() 函數還會檢查截斷的格式。例如, ymd_hms()truncated = 3 也會解析不完整的日期,例如 2012-06-01 12:232012-06-01 122012-06-01 。注意:ymd() 係列函數基於 base::strptime(),目前無法解析 %y-%m 格式。

在異構日期格式的情況下,ymd_hms() 係列根據輸入向量的子集猜測格式。如果輸入向量包含許多缺失值或非日期字符串,則子集可能不包含有意義的日期,並且不會猜測日期時間格式,從而導致 All formats failed to parse 錯誤。在這種情況下,請參閱parse_date_time()以獲得更靈活的解析接口。

從 1.3.0 版本開始,潤滑的解析函數不再返回顯示它們用於解析輸入的格式的消息。您可以通過設置來更改此設置lubridate.verbose選項TRUEoptions(lubridate.verbose = TRUE).

也可以看看

例子


x <- c("2010-04-14-04-35-59", "2010-04-01-12-00-00")
ymd_hms(x)
#> [1] "2010-04-14 04:35:59 UTC" "2010-04-01 12:00:00 UTC"
x <- c("2011-12-31 12:59:59", "2010-01-01 12:00:00")
ymd_hms(x)
#> [1] "2011-12-31 12:59:59 UTC" "2010-01-01 12:00:00 UTC"


## ** heterogeneous formats **
x <- c(20100101120101, "2009-01-02 12-01-02", "2009.01.03 12:01:03",
       "2009-1-4 12-1-4",
       "2009-1, 5 12:1, 5",
       "200901-08 1201-08",
       "2009 arbitrary 1 non-decimal 6 chars 12 in between 1 !!! 6",
       "OR collapsed formats: 20090107 120107 (as long as prefixed with zeros)",
       "Automatic wday, Thu, detection, 10-01-10 10:01:10 and p format: AM",
       "Created on 10-01-11 at 10:01:11 PM")
ymd_hms(x)
#>  [1] "2010-01-01 12:01:01 UTC" "2009-01-02 12:01:02 UTC"
#>  [3] "2009-01-03 12:01:03 UTC" "2009-01-04 12:01:04 UTC"
#>  [5] "2009-01-05 12:01:05 UTC" "2009-01-08 12:01:08 UTC"
#>  [7] "2009-01-06 12:01:06 UTC" "2009-01-07 12:01:07 UTC"
#>  [9] "2010-01-10 10:01:10 UTC" "2010-01-11 22:01:11 UTC"

## ** fractional seconds **
op <- options(digits.secs=3)
dmy_hms("20/2/06 11:16:16.683")
#> [1] "2006-02-20 11:16:16.683 UTC"
options(op)

## ** different formats for ISO8601 timezone offset **
ymd_hms(c("2013-01-24 19:39:07.880-0600",
"2013-01-24 19:39:07.880", "2013-01-24 19:39:07.880-06:00",
"2013-01-24 19:39:07.880-06", "2013-01-24 19:39:07.880Z"))
#> [1] "2013-01-25 01:39:07.88 UTC" "2013-01-24 19:39:07.88 UTC"
#> [3] "2013-01-25 01:39:07.88 UTC" "2013-01-25 01:39:07.88 UTC"
#> [5] "2013-01-24 19:39:07.88 UTC"

## ** internationalization **
if (FALSE) {
x_RO <- "Ma 2012 august 14 11:28:30 "
  ymd_hms(x_RO, locale = "ro_RO.utf8")
}

## ** truncated time-dates **
x <- c("2011-12-31 12:59:59", "2010-01-01 12:11", "2010-01-01 12", "2010-01-01")
ymd_hms(x, truncated = 3)
#> [1] "2011-12-31 12:59:59 UTC" "2010-01-01 12:11:00 UTC"
#> [3] "2010-01-01 12:00:00 UTC" "2010-01-01 00:00:00 UTC"
x <- c("2011-12-31 12:59", "2010-01-01 12", "2010-01-01")
ymd_hm(x, truncated = 2)
#> [1] "2011-12-31 12:59:00 UTC" "2010-01-01 12:00:00 UTC"
#> [3] "2010-01-01 00:00:00 UTC"
## ** What lubridate might not handle **
## Extremely weird cases when one of the separators is "" and some of the
## formats are not in double digits might not be parsed correctly:
if (FALSE) {
ymd_hm("20100201 07-01", "20100201 07-1", "20100201 7-01")}

源代碼:R/parse.r

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Parse date-times with year, month, and day, hour, minute, and second components.。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。