Skip to contents

Coerce character date representations to parttime objects

Usage

# S3 method for partial_time.character
vec_cast(x, to, ..., format = parse_iso8601_datetime, on.na = warning)

Arguments

x

Vectors to cast.

to

Type to cast to. If NULL, x will be returned as is.

...

Additional arguments passed to format if a function is provided.

format

a function or character value. If a function, it should accept a character vector and return a matrix of parttime components. If a character it should provide a regular exprssion which contains capture groups for each of the parttime components. See parse_to_parttime_matrix's regex parameter for more details.

on.na

a function used to signal a condition for new NA values introduced by coercion, a character value among "error", "warning" or "suppress" (for silencing messages) or NULL equivalent to "suppress".

Value

A partial_time vector

Examples

dates <- c(
  NA,
  "2001",
  "2002-01-01",
  "2004-245", # yearday
  "2005-W13", # yearweek
  "2006-W02-5", # yearweek + weekday
  "2007-10-01T08",
  "2008-09-20T08:35",
  "2009-08-12T08:35.048", # fractional minute
  "2010-07-22T08:35:32",
  "2011-06-13T08:35:32.123", # fractional second
  "2012-05-23T08:35:32.123Z", # Zulu time
  "2013-04-14T08:35:32.123+05", # time offset from GMT
  "2014-03-24T08:35:32.123+05:30", # time offset with min from GMT
  "20150101T083532.123+0530" # condensed form
)

as.parttime(dates)
#> Warning: Date strings including week and excluding weekday can not be fully
#> represented. To avoid loss of datetime resolution, such partial dates
#> are best represented as timespans. See `?timespan`.
#> <partial_time<YMDhms+tz>[15]> 
#>  [1] NA                              "2001"                         
#>  [3] "2002-01-01"                    "2004-09-01"                   
#>  [5] "2005"                          "2006-01-12"                   
#>  [7] "2007-10-01 08"                 "2008-09-20 08:35"             
#>  [9] "2009-08-12 08:35:02.880"       "2010-07-22 08:35:32"          
#> [11] "2011-06-13 08:35:32.123"       "2012-05-23 08:35:32.123"      
#> [13] "2013-04-14 08:35:32.123+05:00" "2014-03-24 08:35:32.123+05:30"
#> [15] "2015-01-01 08:35:32.123+05:30"