datetime: Parse ISO 8601 Date/Time Strings

Description Arguments Details Value Examples

Description

Extract date/time components from strings following one of the six formats specified in the NOTE-datetime ISO 8601 profile (https://www.w3.org/TR/NOTE-datetime).

Arguments

x

a character vector.

Details

For character strings in one of the formats in the profile, the corresponding date/time components are extracted, with seconds and decimal fractions of seconds combined. Other (malformed) strings are warned about.

The extracted components for each string are gathered into a named list with elements of the appropriate type (integer for year to min; double for sec; character for the time zone designator). The object returned is a (suitably classed) list of such named lists. This internal representation may change in future versions.

One can subscript such ISO 8601 date/time objects using [ and extract components using $ (where missing components will result in NAs), and convert them to the standard R date/time classes using as.Date(), as.POSIXct() and as.POSIXlt() (incomplete elements will convert to suitably missing elements). In addition, there are print() and as.data.frame() methods for such objects.

Value

An object inheriting from class "ISO_8601_datetime" with the extracted date/time components.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
## Use the examples from <https://www.w3.org/TR/NOTE-datetime>, plus one
## in UTC.
x <- c("1997",
       "1997-07",
       "1997-07-16",
       "1997-07-16T19:20+01:00",
       "1997-07-16T19:20:30+01:00",
       "1997-07-16T19:20:30.45+01:00",
       "1997-07-16T19:20:30.45Z")
y <- parse_ISO_8601_datetime(x)
y
## Conversions: note that "incomplete" elements are converted to
## "missing".
as.Date(y)
as.POSIXlt(y)
## Subscripting and extracting components:
head(y, 3)
y$mon

Example output

                             year mon day hour min   sec    tzd
1997                         1997                              
1997-07                      1997   7                          
1997-07-16                   1997   7  16                      
1997-07-16T19:20+01:00       1997   7  16   19  20       +01:00
1997-07-16T19:20:30+01:00    1997   7  16   19  20    30 +01:00
1997-07-16T19:20:30.45+01:00 1997   7  16   19  20 30.45 +01:00
1997-07-16T19:20:30.45Z      1997   7  16   19  20 30.45      Z
[1] NA           NA           "1997-07-16" "1997-07-16" "1997-07-16"
[6] "1997-07-16" "1997-07-16"
[1] NA                        NA                       
[3] NA                        NA                       
[5] "1997-07-16 18:20:30 UTC" "1997-07-16 18:20:30 UTC"
[7] "1997-07-16 19:20:30 UTC"
           year mon day hour min sec tzd
1997       1997                         
1997-07    1997   7                     
1997-07-16 1997   7  16                 
[1] NA  7  7  7  7  7  7

NLP documentation built on Oct. 23, 2020, 6:18 p.m.

Related to datetime in NLP...