Learn R Programming

DescTools (version 0.99.10)

fastPOSIXct: Fast version of as.POSIXct.character for GMT fixed format.

Description

fastPOSIXct converts timestamps in textual (string) form into POSIXct objects. It interprets sequences of digits separated by non-digits as a timestamp in GMT. The order of interpretation is fixed: year, month, day, hour, minute, second. Note that only true (positive) POSIX dates (since 1970-01-01 00:00:00) are supported. It is extremely fast (compared to as.POSIXct by several orders of magnitude - on some platfroms 1000x faster) since it uses pure text parsing and no system calls.

Usage

fastPOSIXct(x, tz = NULL, required.components = 3L)

Arguments

x
string vector to interpret as timestamps
tz
timezone for the resulting POSIXct object - this is NOT the time of x, that will always be GMT!
required.components
minimum number of timestamp components that are required. For example 3 means only the date is required, 6 means all components (up to the seconds) are required. If the requirement is not met, the result for that entry will be NA

Value

  • Numeric vector of the class POSIXct. In fact this function computes the numeric vector and calls .POSIXct to create the result.

See Also

as.POSIXct

Examples

Run this code
## let us generate a bunch of random timestamps until today
  ts <- as.character(.POSIXct(runif(1e4) * unclass(Sys.time())))

  ## convert them using as.POSIXct
  system.time(a <- as.POSIXct(ts, "GMT"))

  ## same using the fast method
  system.time(b <- fastPOSIXct(ts, "GMT"))

  identical(a, b)

Run the code above in your browser using DataLab