[Rd] creating POSIXct dates in C
Whit Armstrong
whit at twinfieldscapital.com
Fri Feb 18 21:09:48 CET 2005
I'm trying to generate POSIXct times in a call to a C function.
However, I'm having trouble generating times with the proper offset from
UTC.
Can anyone offer any help with this issue?
I've looked at R-2.0.1/src/main/datetime.c, but I was not able to find
an example that I could easily pull from that file.
Thanks in advance,
Whit
Here is my example in C:
#include <stdio.h>
#include <time.h>
int main() {
struct tm localtime_tm;
struct tm utc_tm;
time_t localtime_posix;
time_t utc_posix;
char buf[255];
strptime("1970-01-01", &localtime_tm);
localtime_posix = mktime(&localtime_tm);
// this prints 0
// which is what the time would be if we were in GMT
// how do we convert this time to our timezone
// taking into account that the offset is not
// constant, since daylight savings time
// will shift the offset by an hour
// for certain times
printf("%d\n",(double)localtime_posix);
// now convert it to gmtime
gmtime_r(&localtime_posix, &utc_tm);
utc_posix = mktime(&utc_tm);
// this still prints 0
// how do I convert it to a utc time
// representing 1970-01-01 in EST ??
printf("%d\n",(double)utc_posix);
}
> R.Version()
$platform
[1] "i686-pc-linux-gnu"
$arch
[1] "i686"
$os
[1] "linux-gnu"
$system
[1] "i686, linux-gnu"
$status
[1] ""
$major
[1] "2"
$minor
[1] "0.1"
$year
[1] "2004"
$month
[1] "11"
$day
[1] "15"
$language
[1] "R"
RHEL 3.0
[[alternative HTML version deleted]]
More information about the R-devel
mailing list