4

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?

tech1978
  • 51
  • 1
  • That may not be an Rcpp or even R issue, those things are ultimately done at the system level (though R regroups a some functionality in its sources and Rcpp shadows a bit of that). You may need to differentiate between precision _in comparison and calculation_ and in display. If you want to, you can also switch to higher-resolution date types underneath as _e.g._ in our package [nanotime](http://cloud.r-project.org/package=nanotime). – Dirk Eddelbuettel Apr 21 '21 at 12:20
  • @DirkEddelbuettel Can you put more light on what do you mean by those things are done at system level. As simple as.POSIXct() with "1969-01-01 18:30:00.001" as input is failing. I need those for more of display purpose. Can I use nanotime in Rcpp (.cpp files?) – tech1978 Apr 22 '21 at 05:43
  • Yes. `nantotime` uses the `RcppCCTZ` package you could use. You can also use `RcppDate` to rely on `chrono` in C++11. As for 'system', try `man 3 strptime` -- it is a C library function. There are others. They use the same `POSIXct` representation. – Dirk Eddelbuettel Apr 23 '21 at 12:39

0 Answers0