Skip to contents

Coerce an object to a parttime object

Usage

as.parttime(x, ..., format = parse_iso8601_datetime, on.na = "warning")

Arguments

x

an object for coersion

...

Additional arguments passed to format when 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

parttime vector. See the Details section of parttime for further information.

Examples

as.parttime(c("1985-10-18", "1991-08-23", "1996-09-26"))
#> <partial_time<YMDhms+tz>[3]> 
#> [1] "1985-10-18" "1991-08-23" "1996-09-26" 
# <partial_time<YMDhmsZ>[3]>
# [1] "1985-10-18" "1991-08-23" "1996-09-26"

as.parttime(c("1234", "5678"), format = "(?<year>\\d{4})")
#> <partial_time<YMDhms+tz>[2]> 
#> [1] "1234" "5678" 
# <partial_time<YMDhmsZ>[2]>
# [1] "1234" "5678"

# format function that returns a matrix of components
utf8_str <- function(x) intToUtf8(utf8ToInt(x) - 16)
as.parttime(c("B@", "B@A@"), format = function(x) cbind(year = sapply(x, utf8_str)))
#> <partial_time<YMDhms+tz>[2]> 
#> [1] "0020" "2010" 
# <partial_time<YMDhmsZ>[2]>
# [1] "2000" "2010"

# format function that returns a parttime object by first pre-processing input
as.parttime("B@BB", format = function(x) as.parttime(utf8_str(x)))
#> <partial_time<YMDhms+tz>[1]> 
#> [1] "2022" 
# <partial_time<YMDhmsZ>[1]>
# [1] "2022"

# format function that returns a parttime object by manual construction
as.parttime("AIII", format = function(x) parttime(year = as.numeric(utf8_str(x))))
#> <partial_time<YMDhms+tz>[1]> 
#> [1] "1999" 
# <partial_time<YMDhmsZ>[1]>
# [1] "1999"