Dirk Eddelbuettel — written Nov 11, 2017 — source
R has excellent facilities for dealing with both dates and datetime objects.
For datetime objects, the
POSIXt time type can be mapped to
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 type 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 measured in nanoseconds. Other languages uses a (signed)
64-bit integer to represent (integer) nanoseconds since the epoch. A bit over a year ago I realized
that we have this in R too—by combining the
integer64 type in the very nice
bit64 package by Jens Oehlschlaegel with the
CCTZ-based parser and formatter in my own
RcppCCTZ package. And thus the
nanotime package was created.
A simple example:
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. See the
nanotime page and package for more.
So more recently I had a need to efficiently generate such (many such) integer vectors from
int64_t data. Both Leonardo and Dan
helped with initial discussions and tests. One can either use a
reinterpret_cast<>, or a straight
memcpy as the key trick in bit64 is to (re-)use the
int64_t representation (which we do not have in R) via the 64-bit
representative. Just never access it as a
double. So we have the space, we just need to ensure we
copy the bits (i.e. actual binary content) rather than their value (when “mapped” to a type).
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
object (provided the bit64 package has been loaded).
reinterpret_cast<>() can be used too, but leads to a compiler warning (under
g++-6). Per Matt’s excellent compiler explorer, both
approaches lead to the same
mov semantics, so we prefer the variant that does not yell at us.
nanotime vector is creating using an internal
integer64 vector. So the previous function
almost gets us there. But we need to set the S4 type correctly. So that needed some extra
work—and the following function seems to do it right:
This creates a
nanotime vector as a proper S4 object. As before, we set some class attributes
(though in a nested fashion as S4 is that fancy) and also invoke one R macro.
The astute reader will have noticed that neither one of the functions presented so far had an
Rcpp::export tag. This is because of their 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
integer64, and we had the additional formatters etc.
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 1000 1000000 1000000000
 "2017-11-11T23:18:14.123456789+00:00"  "2017-11-11T23:18:15.123456789+00:00"  "2017-11-11T23:18:16.123456789+00:00"  "2017-11-11T23:18:17.123456789+00:00"
integer64  1000000000 1000000000 1000000000
With that we’re done for this piece. Happy
nanotime-ing from C++!