matchingTimeFormat: Find Time Format matching a Timestamp

Description Usage Arguments Value Examples

View source: R/matchingTimeFormat.R

Description

Find Time Format matching a Timestamp

Usage

1
2
3
4
5
6
7
matchingTimeFormat(
  timestamp,
  timeFormats = getTimeFormats(),
  method = 1L,
  warn = TRUE,
  failureValue = NULL
)

Arguments

timestamp

character timestamp to be checked against different possible timstamp formats

timeFormats

vector of possible time formats with placeholders (year), described for format.POSIXct

method

passed to hasTimeFormat

warn

if TRUE an R warning is issued if no matching format was found. Otherwise a message is given.

failureValue

value returned in case that no matching format was found. Default: NULL

Value

first time format in timeformats that matches the given timestamp. NULL is returned if none of the given timeformats matches.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Convert a character timestamp of which the format can be one of two
# possible formats into a POSIXct-object
possibleFormats <- c("%d.%m.%Y", "%d/%m/%Y")

x <- "14.01.2015"
t1 <- hsToPosix(x, format = matchingTimeFormat(x, possibleFormats))
  
# In fact this is what stringToPosix does (for only one timestamp)
t2 <- stringToPosix(x, formats = possibleFormats)
stopifnot(identical(t1, t2))
  
# You get a warning if none of the possible formats matches
matchingTimeFormat("01.14.2015", possibleFormats)

KWB-R/kwb.datetime documentation built on July 9, 2021, 10:28 p.m.