Read ISO 8601 strings into POSIXct, POSIXt.

iso8601_read(x, tz.name = NULL)

Arguments

x

(character) A vector of ISO 8601 strings created with `iso8601_convert`. Vector contents must be one of dates and times, dates, or times. A mix of more than one type is not supported. A mix of input temporal resolution is supported for date and time data.

tz.name

(character) Time zone name used to override parsing of time zone offset listed in `x`. See `OlsonNames()` for valid options.

Value

(POSIXct, POSIXt) A vector of POSIXct POSIXt, unless the format is 'YYYY', in which case integer class data is returned.

Details

`iso8601_read` provides a lightweight option for reading ISO 8601 strings created with `iso8601_convert` into the POSIXct POXIXt class. This function uses regular expressions to extract the orders and time zone offset arguments and passess this info to `lubridate::parse_date_time` for parsing.

Time zone offsets are parsed "as is", i.e. using Olson Names that do not recognize Day Light Savings time. For example a time zone offset of "+06" is parsed using the Olson Name "Etc/GMT-6". Date times without a time zone offset default to "UTC". Use the argument `tz.name` to override the input data time zone.

Examples

# Time zone are automatically parsed datetimes <- iso8601_read('2012-05-01T13:23:00+05') attributes(datetimes)
#> $class #> [1] "POSIXct" "POSIXt" #> #> $tzone #> [1] "Etc/GMT-5" #>
# Time zone defaults to 'UTC' when none is specified iso8601_read('2012-05-01T13:23:00')
#> Parsing data with format specifier "YYYY-MM-DDThh:mm:ss" Default time zone "UTC" will be used.
#> [1] "2012-05-01 13:23:00 UTC"
# Vairance of input temporal resolution is supported iso8601_read(c('2012-05-01T13:45:23+05', '2012-05-01T13:45+05', '2012-05-01T13+05'))
#> [1] "2012-05-01 13:45:23 +05" "2012-05-01 13:45:00 +05" #> [3] "2012-05-01 13:00:00 +05"
# tz.name overrides input time zone iso8601_read('2012-05-01T13:45+09:30', tz.name = 'Etc/GMT+4')
#> Parsing data with time zone offset = Etc/GMT+4
#> [1] "2012-05-01 13:45:00 -04"
if (FALSE) { # Multiple time zones are not yet supported iso8601_read(c('2012-05-01T13-05', '2012-05-01T13-06')) iso8601_read(c('2012-05-01T13-05', '2012-05-01T13')) }