Title: | Recognize and Parse Dates in Various Formats, Including All ISO 8601 Formats |
---|---|
Description: | Parse dates automatically, without the need of specifying a format. Currently it includes the git date parser. It can also recognize and parse all ISO 8601 formats. |
Authors: | Gábor Csárdi, Linus Torvalds |
Maintainer: | Gábor Csárdi <[email protected]> |
License: | GPL-2 |
Version: | 1.3.1.9000 |
Built: | 2024-10-25 05:47:32 UTC |
Source: | https://github.com/gaborcsardi/parsedate |
Three useful functions to parse and format dates.
parse_iso_8601
recognizes and parses all valid ISO
8601 date and time formats. It can also be used as an ISO 8601
validator.
parse_date
can parse a date when you don't know
which format it is in. First it tries all ISO 8601 formats.
Then it tries git's versatile date parser. Lastly, it tries
as.POSIXct
.
format_iso_8601
formats a date (and time) in
a specific ISO 8601 format.
Useful links:
Report bugs at https://github.com/gaborcsardi/parsedate/issues
Format a date in a fixed format that is ISO 8601 valid, and can be used to compare dates as character strings. It converts the date(s) to UTC.
format_iso_8601(date)
format_iso_8601(date)
date |
The date(s) to format. |
Character vector of formatted dates.
format_iso_8601(parse_iso_8601("2013-02-08")) format_iso_8601(parse_iso_8601("2013-02-08 09:34:00")) format_iso_8601(parse_iso_8601("2013-02-08 09:34:00+01:00")) format_iso_8601(parse_iso_8601("2013-W06-5")) format_iso_8601(parse_iso_8601("2013-039"))
format_iso_8601(parse_iso_8601("2013-02-08")) format_iso_8601(parse_iso_8601("2013-02-08 09:34:00")) format_iso_8601(parse_iso_8601("2013-02-08 09:34:00+01:00")) format_iso_8601(parse_iso_8601("2013-W06-5")) format_iso_8601(parse_iso_8601("2013-039"))
Recognize and parse dates from a wide range of formats. The current algorithm is the following:
Try parsing dates using all valid ISO 8601 formats, by
calling parse_iso_8601
.
If this fails, then try parsing them using the git date parser.
If this fails, then try parsing them using as.POSIXct
.
(It is unlikely that this step will parse any dates that the
first two steps couldn't, but it is still a logical fallback,
to make sure that we can parse at least as many dates as
as.POSIXct
.
parse_date
returns quickly in case of empty input elements.
parse_date(dates, approx = TRUE, default_tz = "UTC")
parse_date(dates, approx = TRUE, default_tz = "UTC")
dates |
A character vector. An error is reported if the function cannot coerce this parameter to a character vector. |
approx |
Logical flag, whether the git parse should try
hard(er). If this is set to |
default_tz |
Time zone to assume for dates that don't specify a time zone explicitly. Defaults to UTC, and an empty string means the local time zone. |
All dates are returned in the UTC time zone. If you prefer a different time zone, simply use '.POSIXct()' on the result, see examples below.
A POSIXct
vector. NA
is returned for
the dates that parse_date
could not parse.
# Some easy examples parse_date("2014-12-12") parse_date("04/15/99") parse_date("15/04/99") # Ambiguous format, parsed assuming MM/DD/YY parse_date("12/11/99") parse_date("11/12/99") # Fill in the current date and time parse_date("03/20") parse_date("12") # But not for this, because this is ISO 8601 parse_date("2014") # Handle vectors and empty input parse_date(c("2014","2015","","2016")) # Convert result to local time tz <- format(Sys.time(), "%Z") as.POSIXct(parse_date("2014-12-13T11:12:13"), tz) # Local time zone parse_date("2014-12-13T11:12:13", default_tz = "CET") parse_date("2014-12-13T11:12:13", default_tz = "UTC") # Convert results to different timezone parse_date("2015-12-13T11:12:13") .POSIXct(parse_date("2015-12-13T11:12:13"), tz = "CET")
# Some easy examples parse_date("2014-12-12") parse_date("04/15/99") parse_date("15/04/99") # Ambiguous format, parsed assuming MM/DD/YY parse_date("12/11/99") parse_date("11/12/99") # Fill in the current date and time parse_date("03/20") parse_date("12") # But not for this, because this is ISO 8601 parse_date("2014") # Handle vectors and empty input parse_date(c("2014","2015","","2016")) # Convert result to local time tz <- format(Sys.time(), "%Z") as.POSIXct(parse_date("2014-12-13T11:12:13"), tz) # Local time zone parse_date("2014-12-13T11:12:13", default_tz = "CET") parse_date("2014-12-13T11:12:13", default_tz = "UTC") # Convert results to different timezone parse_date("2015-12-13T11:12:13") .POSIXct(parse_date("2015-12-13T11:12:13"), tz = "CET")
See https://en.wikipedia.org/wiki/ISO_8601 and links therein for the complete standard.
parse_iso_8601(dates, default_tz = "UTC")
parse_iso_8601(dates, default_tz = "UTC")
dates |
A character vector. An error is reported if the function cannot coerce this parameter to a character vector. |
default_tz |
Time zone to assume for dates that don't specify a time zone explicitly. Defaults to UTC, and an empty string means the local time zone. |
A POSIXct
vector. NA
is returned for
the dates that parse_date
could not parse.
# Missing fields parse_iso_8601("2013-02-08 09") parse_iso_8601("2013-02-08 09:30") # Separator between date and time can be a 'T' parse_iso_8601("2013-02-08T09") parse_iso_8601("2013-02-08T09:30") parse_iso_8601("2013-02-08T09:30:26") # Fractional seconds, minutes, hours parse_iso_8601("2013-02-08T09:30:26.123") parse_iso_8601("2013-02-08T09:30.5") parse_iso_8601("2013-02-08T09,25") # Zulu time zone is UTC parse_iso_8601("2013-02-08T09:30:26Z") # ISO weeks, not very intuitive parse_iso_8601("2013-W06-5") parse_iso_8601("2013-W01-1") parse_iso_8601("2009-W01-1") parse_iso_8601("2009-W53-7") # Day of the year parse_iso_8601("2013-039") parse_iso_8601("2013-039 09:30:26Z")
# Missing fields parse_iso_8601("2013-02-08 09") parse_iso_8601("2013-02-08 09:30") # Separator between date and time can be a 'T' parse_iso_8601("2013-02-08T09") parse_iso_8601("2013-02-08T09:30") parse_iso_8601("2013-02-08T09:30:26") # Fractional seconds, minutes, hours parse_iso_8601("2013-02-08T09:30:26.123") parse_iso_8601("2013-02-08T09:30.5") parse_iso_8601("2013-02-08T09,25") # Zulu time zone is UTC parse_iso_8601("2013-02-08T09:30:26Z") # ISO weeks, not very intuitive parse_iso_8601("2013-W06-5") parse_iso_8601("2013-W01-1") parse_iso_8601("2009-W01-1") parse_iso_8601("2009-W53-7") # Day of the year parse_iso_8601("2013-039") parse_iso_8601("2013-039 09:30:26Z")