As.POSIXct() is giving difference of one second for dates before 1970-01-01

Viewed 79

In R studio when I am getting following

> as.POSIXct("1970-01-01 18:30:00.001", origin = "1900-01-01")
[1] "1970-01-01 18:30:00.000 IST"

> as.POSIXct("1969-01-01 18:30:00.001", origin = "1900-01-01")
[1] "1969-01-01 18:30:01.000 IST" ## extra second got added here

> as.POSIXct("1969-01-01 18:30:01.001", origin = "1900-01-01")
[1] "1969-01-01 18:30:02.000 IST" ## extra second got added here

I am doing same thing in Rcpp and I am getting same result

// [[Rcpp::export]]
Rcpp::Datetime rcppdatetime() {
  Rcpp::Datetime dt("1969-01-01 18:30:00.001");
  return(dt);
}

/*** R
rcppdatetime()
*/

"1969-01-01 18:30:01.000 IST"

This is expected as Rcpp::Datetime object is POSIXct type.

I need help in following regard

) How to correct this second value for dates before year 1970 ?

) Similar error I am facing for microseconds representation. I went through this thread https://github.com/RcppCore/Rcpp/issues/899 Can someone point me to documentation in R/Rcpp where its mentioned as R constraint. I am using mingw 8.1.0 to compile my application, so I am not sure how C++ 11 specific code will help here.

) I checked this thread How R formats POSIXct with fractional seconds But it provides output in character format. I need output in posixct form so that end user can format as he wants or do further processing.

) I want to do this in Rcpp. As most of my application code in in .cpp (we have DLLwhich gets loaded). If I decide to do it in R, since I am putting all date time objects in DatetimeVector, if I decide to do it in R, I may need to go through entire vector once again. Are there any links which can help ?

) Any other package/interface available which I can use in my .cpp files as achieve it there itself?

0 Answers
Related