[Rd] How to convert time_t to R date object

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 17 14:11:53 CET 2014


On 17/03/2014 11:39, Dirk Eddelbuettel wrote:
>
> Bill,
>
> On 17 March 2014 at 12:54, Bill Wang wrote:
> | Thanks for your reply, I neede convert time_t to R type in C code, can not use
> | Rcpp. Maybe Rcpp source code could help me.
>
> Start by reading 'Writing R Extensions' and figure out how to send an int
> back and forth.  Then cast between int and time_t.  Then set the class of the
> int variable to Date type.

But note that time_t is not 'int' on many modern systems: almost all 
64-bit ones and on some 32-bit ones.

I guess this is actually meant to be a date-time object, hence class 
POSIXct.  Class POSIXct is based on doubles, so return a double and add 
the classes in the R wrapper.  If you want class Date, divide by 86400 
and return an integer.

That does assume that the system is POSIX-compliant and so ignores leap 
seconds.   I have not encountered one that is not for many years, but 
allegedly there are locales which do count leap seconds on some 
Unix-alikes.  C code to adjust that is in src/main/datetime.c in the R 
sources.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list