Skip to contents

Significantly faster time series parsing than readr::parse_date, readr::parse_datetime, lubridate::as_date(), and lubridate::as_datetime(). Uses anytime package, which relies on Boost.Date_Time C++ library for date/datetime parsing.

Usage

parse_date2(x, ..., silent = FALSE)

parse_datetime2(x, tz = "UTC", tz_shift = FALSE, ..., silent = FALSE)

Arguments

x

A character vector

...

Additional parameters passed to anytime() and anydate()

silent

If TRUE, warns the user of parsing failures.

tz

Datetime only. A timezone (see OlsenNames()).

tz_shift

Datetime only. If FALSE, forces the datetime into the time zone. If TRUE, offsets the datetime from UTC to the new time zone.

Value

Returns a date or datatime vector from the transformation applied to character timestamp vector.

Details

Parsing Formats

  • Date Formats: Must follow a Year, Month, Day sequence. (e.g. parse_date2("2011 June") is OK, parse_date2("June 2011") is NOT OK).

  • Date Time Formats: Must follow a YMD HMS sequence.

Refer to lubridate::mdy() for Month, Day, Year and additional formats.

Time zones (Datetime)

Time zones are handled in a similar way to lubridate::as_datetime() in that time zones are forced rather than shifted. This is a key difference between anytime::anytime(), which shifts datetimes to the specified timezone by default.

References

Examples


# Fast date parsing
parse_date2("2011")
#> [1] "2011-01-01"
parse_date2("2011 June 3rd")
#> [1] "2011-06-03"

# Fast datetime parsing
parse_datetime2("2011")
#> [1] "2011-01-01 UTC"
parse_datetime2("2011 Jan 1 12:35:21")
#> [1] "2011-01-01 12:35:21 UTC"

# Time Zones (datetime only)
parse_datetime2("2011 Jan 1 12:35:21", tz = "Europe/London")
#> [1] "2011-01-01 12:35:21 GMT"