guess_datetime: Guess Timestamp Format

Description Usage Arguments Details Value Warning Author(s) See Also Examples

View source: R/functions.R

Description

Tries to convert a character vector to POSIXct.

Usage

1

Arguments

s

character

Details

The function first coerces its argument to character. It then applies a list of patterns to each element of s. Let d be a numeric digit; then the rules are roughly those in the table below. (For the precise rules, see Examples below.)

original pattern assumed format
dddd-dd-dd dd:dd:dd %Y-%m-%d %H:%M:%S
dd/dd/dddd dd:dd:dd %m/%d/%Y %H:%M:%S
dd.dd.dddd dd:dd:dd %d.%m.%Y %H:%M:%S

The rules are followed in the given order; an element will be matched only once. If there is a match, strptime will be tried with the assumed format.

For elements that do not match any pattern or for which strptime fails, NA is returned.

Value

POSIXct

Warning

If you know the format of a timestamp, then do not use this function (use strptime instead). If you have no idea at all about the format of a timestamp, then do not use this function.

Author(s)

Enrico Schumann

See Also

strptime; see also the function anytime in the package of the same name.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
s <- c("  1999-08-19     10:00:31   ",
       "   1999-08-19 10:00",
       "19.8.1999 10:00",
       "8/19/99      10:00:31",
       "8/19/1999 10:00:31",
       "19.8.1999 10:00:31")

guess_datetime(s)

## Not run: ## the actual rules
rules <- as.data.frame(matrix(datetimeutils:::.dt_patterns,
                              byrow = TRUE, ncol = 2),
                       stringsAsFactors = FALSE)
names(rules) <- c("pattern", "assumed_format")
rules


## End(Not run)

datetimeutils documentation built on March 18, 2018, 2:30 p.m.