Get the format (e.g. 'YYYY-MM-DD') used in a vector of ISO 8601 strings. Supported formats are output by `iso8601_convert`.

iso8601_get_format_string(x, return.format = FALSE)

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.

return.format

(logical) Should format specifiers be returned with the output data? This argument supports identification of where differences in output format occur.

Value

(character) The date time format representing user supplied ISO 8601 data. If more than one date time format is present, then the mode is returned along with a warning message. An error is issued when input data is a mix of datetime, date, or time data.

(data frame) If `return.format` is `TRUE` then a data frame is returned containing the input data and format specifiers. This argument supports identification of where differences in output format occur.

Details

`iso8601_get_format_string` uses regular expressions to parse input data and identify the most common format present in a vector of ISO 8601 strings.

Examples

# Get format strings iso8601_get_format_string('13:45')
#> [1] "hh:mm"
iso8601_get_format_string('13:45:12+06:00')
#> [1] "hh:mm:ss+hh:mm"
iso8601_get_format_string('2012-05-01')
#> [1] "YYYY-MM-DD"
iso8601_get_format_string('2012-05-01T13')
#> [1] "YYYY-MM-DDThh"
iso8601_get_format_string('2012-05-01T13:29:54+05')
#> [1] "YYYY-MM-DDThh:mm:ss+hh"
# Expect a warning when more than one format is present. iso8601_get_format_string(c('2012-05-01T13:45', '2012-05-02T13', '2012-05-03T13:45:30'))
#> Warning: More than one format was found. The returned value is the mode of the detected formats. Use the argument "return.format = T" to see all detected fomats.
#> [1] "YYYY-MM-DDThh:mm"
# return.format = T returns the input data, converted data, and formats iso8601_get_format_string(c('2012-05-01T13:45', '2012-05-02T13', '2012-05-03T13:45:30'), return.format = TRUE)
#> Warning: More than one format was found. The returned value is the mode of the detected formats. Use the argument "return.format = T" to see all detected fomats.
#> x format #> 1 2012-05-01T13:45 YYYY-MM-DDThh:mm #> 2 2012-05-02T13 YYYY-MM-DDThh #> 3 2012-05-03T13:45:30 YYYY-MM-DDThh:mm:ss
# Plus and minus sign is returned when more than one time zone sign is present iso8601_get_format_string(x = c('2012-05-01T13:45:23+05:00', '2012-05-01T13:45:23+05:00', '2012-05-01T13:45:23-05:00'))
#> [1] "YYYY-MM-DDThh:mm:ss±hh:mm"