Convert a time string to a POSIXct object. Allow for different possible timestamp formats.

stringToPosix(
  x,
  formats = c("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d"),
  ...
)

Arguments

x

character vector of length one representing a timestamp

formats

vector of allowed time formats (using %-placeholders)

...

arguments passed to hsToPosix

Examples

stringToPosix("2016-05-26")
#> [1] "2016-05-26 UTC"
stringToPosix("2016-05-26 12:00")
#> [1] "2016-05-26 12:00:00 UTC"
# additional arguments passed to hsToPosix stringToPosix("2016-05-26 12:00:33", tzone = "ETC/Gmt-1")
#> [1] "2016-05-26 12:00:33 +01"
# lt = TRUE -> create POSIXlt instead of POSIXct lt1 <- stringToPosix("2016-05-26 17:00", lt = TRUE) lt2 <- stringToPosix("2016-05-26 17:00", lt = TRUE, tz = "Europe/Berlin") lt1$hour
#> [1] 17
lt1$isdst # normal time (is daylight saving time = FALSE)
#> [1] 0
lt2$isdst # summer time (is daylight saving time = TRUE)
#> [1] 1