From ripley at stats.ox.ac.uk  Mon Mar 17 14:11:53 2014
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 17 Mar 2014 13:11:53 +0000
Subject: [Rd] How to convert time_t to R date object
In-Reply-To: <21286.57101.842877.461798@max.nulle.part>
References: <CACY4ALiS73rxKVNwuW8W_0zsE35NcZh==JiSPUv1VZ+7E4P4Kg@mail.gmail.com>	<21285.47984.318336.174502@max.nulle.part>	<CACY4ALhbhtvNR7CNj7efEeSgXbhJRCH325OOW=0AhrbDETsoLQ@mail.gmail.com>
	<21286.57101.842877.461798@max.nulle.part>
Message-ID: <5326F499.6030105@stats.ox.ac.uk>

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



[Rd] How to convert time_t to R date object
Prof Brian Ripley