Creating integer64 and nanotime vectors in C++
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Motivation: More Precise Timestamps
R has excellent facilities for dealing with both dates and datetime objects.
For datetime objects, the POSIXt
time type can be mapped to POSIXct
and
its representation of fractional seconds since the January 1, 1970 “epoch” as
well as to the broken-out list representation in POSIXlt
. Many add-on
packages use these facilities.
POSIXct
uses a double to provide 53 bits of resolution. That is generally
good enough for timestamps down to just above a microsecond, and has served
the R community rather well.
But increasingly, time increments are measure in nanoseconds. Other languages uses a (signed)
64-bit integer to represent (integer) nanoseconds since the epoch. A bit over a year I realized
that we have this in R too—by combining the integer64
type in the
bit64 package by Jens Oehlschlaegel with the
CCTZ-based parser and formatter in my
RcppCCTZ package. And thus the
nanotime package was created.
Leonardo Silvestri then significantly enhanced nanotime by redoing it as an S4 class.
A simple example:
[1] "1970-01-01T00:00:00.000000042+00:00"
Here we used a single element with value 42, and created a nanotime
vector from it—which is
taken to me 42 nanoseconds since the epoch, or basically almost at January 1, 1970.
Step 1: Large Integer Types
So more recently I had a need to efficiently generate such integer vector from int64_t
data.
Both Leonardo and Dan helped with
initial discussion and tests. One can either use a reinterpret_cast<>
or a straight memcpy
as
the key trick in bit64 is to use the underlying 64-bit
double
. So we have the space, we just need to ensure we copy the bits rather than their values.
This leads to the following function to create an integer64
vector for use in R at the C++ level:
This uses the standard trick of setting a class
attribute to set an S3 class. Now the values in
v
will return to R (exactly how is treated below), and R will treat the vector as integer64
object (provided the bit64 package has been loaded).
Step 2: Nanotime
A nanotime
vector is creating using an internal integer64
vector. So the previous functions
almost gets us there. But we need to set the S4 type correctly. So that needed some extra work.
The following function does it:
This creates a nanotime
vector as a proper S4 object.
Step 3: Returning them R via data.table
The astute reader will have noticed that neither function had an Rcpp::export
tag. This is
because of the function argument: int64_t
is not representable natively by R, which is why we
need a workaround. Matt Dowle has been very helpful in providing
excellent support for nanotime
in data.table
(even after we, ahem, borked it by switching from S3 to S4). This support was of course relatively
straightforward because data.table already had
support for the underlying integer64
, and we had the additional formatters etc.
Example
The following example shows the output from the preceding function:
int64s nanos 1: 1 2017-11-11T23:18:14.123456789+00:00 2: 1000 2017-11-11T23:18:15.123456789+00:00 3: 1000000 2017-11-11T23:18:16.123456789+00:00 4: 1000000000 2017-11-11T23:18:17.123456789+00:00
integer64 [1] 1 1000 1000000 1000000000
[1] "2017-11-11T23:18:14.123456789+00:00" [2] "2017-11-11T23:18:15.123456789+00:00" [3] "2017-11-11T23:18:16.123456789+00:00" [4] "2017-11-11T23:18:17.123456789+00:00"
integer64 [1] 1000000000 1000000000 1000000000
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.