Full 64-bit resolution date and time support with resolution up to nanosecond granularity is provided, with easy transition to and from the standard 'POSIXct' type.
Nanosecond Time Resolution for R
R has excellent tools for dates and times. The
POSIXct classes (as well as the 'wide'
POSIXlt) are versatile, and a lot of useful tooling has been built around them.
POSIXct is implemented as a
double with fractional seconds since the epoch. Given the
53 bits accuracy, it leaves just a bit less than microsecond resolution. Which is good enough for
But more and more performance measurements, latency statistics, ... are now measured more finely,
and we need nanosecond resolution. For which commonly an
integer64 is used to represent
nanoseconds since the epoch.
And while R does not a native type for this, the bit64
package by Jens Oehlschlägel offers a performant one implemented as a
lightweight S3 class. So this package uses this
integer64 class, along with two helper functions
for parsing and formatting, respectively, at nano-second resolution from the
RcppCCTZ package which wraps the
CCTZ library from Google. CCTZ is a modern C++11 library
extending the (C++11-native)
R> x <- nanotime("1970-01-01T00:00:00.000000001+00:00")R> print(x)integer64 1R> format(x) "1970-01-01T00:00:00.000000001+00:00"R> x <- x + 1R> print(x)integer64 2R> format(x) "1970-01-01T00:00:00.000000002+00:00"R>
R> options("width"=60)R> v <- nanotime(Sys.time()) + 1:5R> vinteger64 1481505724483583001 1481505724483583002 1481505724483583003 1481505724483583004 1481505724483583005R> format(v) "2016-12-12T01:22:04.483583001+00:00" "2016-12-12T01:22:04.483583002+00:00" "2016-12-12T01:22:04.483583003+00:00" "2016-12-12T01:22:04.483583004+00:00" "2016-12-12T01:22:04.483583005+00:00"R>
R> z <- zoo(cbind(A=1:5, B=5:1), v)R> options("nanotimeFormat"="%H:%M:%E*S") ## override defaultR> zA B01:47:55.812513001 1 501:47:55.812513002 2 401:47:55.812513003 3 301:47:55.812513004 4 201:47:55.812513005 5 1R>
R> library(data.table)R> library(bit64) # register some print methods for integer64R> raw <- data.table(cbind(A=1:5, B=5:1), v)R> fwrite(raw, file="/tmp/raw.csv")R> cooked <- fread("/tmp/raw.csv")
The bit64 package (by
Jens Oehlschlägel) supplies the
integer64 type used to store the
nanosecond resolution time as (positive or negative) offsets to the epoch of January 1, 1970. The
RcppCCTZ package supplies the formatting and
parsing routines based on the (modern C++) library CCTZ from
The package is in the very early stages. Expect changes, maybe even breaking ones. But the package has some tests, and code coverage.
See the issue tickets for an up to date list of potentially desirable, possibly planned, or at least discussed items.
Once on CRAN you will be able to do
Until then, or to install development versions, it can also be installed via a standard
install.packages("RcppCCTZ") # need 0.1.0 or laterremotes::install_github("eddelbuettel/nanotime")
GPL (>= 2)