Convert character, numeric, integer, or POSIXct datetimes to POSIXct.
parseDatetime(
datetime = NULL,
timezone = NULL,
expectAll = FALSE,
isJulian = FALSE,
quiet = TRUE
)Vector of character, numeric, integer, or POSIXct
datetimes.
Olson timezone used to interpret incoming datetimes.
Logical value specifying whether to stop if any non-missing input values fail to parse.
Logical value specifying whether datetime should be
interpreted as a Julian date using day-of-year notation.
Logical value passed to lubridate::parse_date_time() to
suppress parsing warnings.
A POSIXct vector.
This function accepts a variety of compact date/time formats commonly used in
Mazama Science packages, including Y, Ym, Ymd, YmdH, YmdHM, and
YmdHMS. Inputs may be mixed within the same vector.
Examples of equivalent inputs include:
20181012130900
"2018-10-12-13-09-00"
"2018 Oct. 12 13:09:00"All incoming datetimes are interpreted in the specified timezone. If
datetime is already POSIXct, it is converted to the requested timezone
with lubridate::with_tz().
If a character datetime includes signed offset information, such as
"-07:00", that offset is used by lubridate::parse_date_time() when
determining the equivalent instant.
Within Mazama Science packages, datetimes not already in POSIXct format are
often represented as compact decimal values with no separators, such as
20181012 or 20181012130900, either as numbers or strings.
parseDatetime() is a wrapper around lubridate::parse_date_time() that
defines the datetime formats supported by MazamaCoreUtils.
# All Y[mdHMS] formats are accepted
parseDatetime(2018, timezone = "America/Los_Angeles")
#> [1] "2018-01-01 PST"
parseDatetime(201808, timezone = "America/Los_Angeles")
#> [1] "2018-08-01 PDT"
parseDatetime(20180807, timezone = "America/Los_Angeles")
#> [1] "2018-08-07 PDT"
parseDatetime(2018080718, timezone = "America/Los_Angeles")
#> [1] "2018-08-07 18:00:00 PDT"
parseDatetime(201808071812, timezone = "America/Los_Angeles")
#> [1] "2018-08-07 18:12:00 PDT"
parseDatetime(20180807181215, timezone = "America/Los_Angeles")
#> [1] "2018-08-07 18:12:15 PDT"
parseDatetime("2018-08-07 18:12:15", timezone = "America/Los_Angeles")
#> [1] "2018-08-07 18:12:15 PDT"
parseDatetime("2018-08-07 18:12:15-07:00", timezone = "UTC")
#> [1] "2018-08-08 01:12:15 UTC"
# Julian days are accepted
parseDatetime(
2018219181215,
timezone = "America/Los_Angeles",
isJulian = TRUE
)
#> [1] "2018-08-07 18:12:15 PDT"
# Mixed vector inputs are accepted
parseDatetime(
c("2018-10-24 12:00", "201810311200", "2018-11-07 12:00"),
timezone = "America/New_York"
)
#> [1] "2018-10-24 12:00:00 EDT" "2018-10-31 12:00:00 EDT"
#> [3] "2018-11-07 12:00:00 EST"
badInput <- c("20181013", NA, "20181015", "181016", "10172018")
# Return NA for dates that cannot be parsed
parseDatetime(badInput, timezone = "UTC", expectAll = FALSE)
#> [1] "2018-10-13 UTC" NA "2018-10-15 UTC" NA
#> [5] NA
if (FALSE) { # \dontrun{
# Fail if any non-missing dates cannot be parsed
parseDatetime(badInput, timezone = "UTC", expectAll = TRUE)
} # }