Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: datetime conversion from R to Stata


From   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: datetime conversion from R to Stata
Date   Fri, 24 Jan 2014 14:54:06 -0500

Hiroyuki,
I think your test illustrates the difference caused by the leap
seconds. This is the leap seconds map as used in Stata 13.1:

30jun1972         31dec1972
                   31dec1973
                   31dec1974
                   31dec1975
                   31dec1976
                   31dec1977
                   31dec1978
                   31dec1979
  30jun1981
  30jun1982
  30jun1983
  30jun1985
                   31dec1987
                   31dec1989
                   31dec1990
  30jun1992
  30jun1993
  30jun1994
                   31dec1995
  30jun1997
                   31dec1998
                   31dec2005
           31dec2008
  30jun2012


There are 25 of them. And 25 is exactly the difference as it can be
seen e.g. in obs #8 of your test.
Consult: http://stat.ethz.ch/R-manual/R-devel/library/base/html/DateTimeClasses.html
for R details. It seems that it relies on the system's converter, so
whether leap seconds are accounted for or not, is not up to R but up
to the OS:
---quote---
It seems that some rare systems use leap seconds, but most ignore them
(as required by POSIX). This is detected and corrected for at build
time, so all "POSIXct" times used by R do not include leap seconds.
---end quote---

However there is also zdatetime in R:
http://svn.r-project.org/R/trunk/src/library/base/R/zdatetime.R
which lists all of the leap seconds.

Interestingly the following blog entry:
http://www.inside-r.org/r-doc/base/DateTimeClasses
lists that seconds can be in the range of 0-61. Why 61?? ( a leap
second should have the value 60).

Leap seconds are available in Stata 10 and more recent.


Hope this helps.
Sergiy Radyakin.



On Fri, Jan 24, 2014 at 6:50 AM, Hiroyuki Kawakatsu <[email protected]> wrote:
> Hi,
>
> Prof Brian Ripley (R-core member) `speculated'[1] that the
> datetime conversion command from R to Stata in
>
> http://www.stata.com/help.cgi?datetime#s11
>
> should use tc() rather than tC(). There are two other typos in
>
> . gen double statatime = rtime - tC(01jan1970 00:00)
>
> which I suspect should be
>
> . gen double statatime = rtime*1000 + tc(01jan1970 00:00)
>
> Can someone in the know confirm this?
> I used the following test. I generated the data file in R as
>
> x = seq(Sys.time(), by="sec", length.out=10);
> x = data.frame(posixct=x, rnum=as.numeric(x));
> write.table(x, "xxx.csv", sep=",", row.names=FALSE)
>
> and read it in Stata using the suggested command
> (with the two typos corrected):
>
>> insheet using xxx.csv, clear names;
> (2 vars, 10 obs)
> . gen double xbigc = rnum*1000 + tC(01jan1970 00:00);
> . format xbigc %tC;
> . gen double xc = rnum*1000 + tc(01jan1970 00:00);
> . format xc %tc;
>
> . desc;
> Contains data
>   obs:            10
>  vars:             4
>  size:           390
> -------------------------------------------------------------------------------------------------
>               storage   display    value
> variable name   type    format     label      variable label
> -------------------------------------------------------------------------------------------------
> Sorted by:
>      Note:  dataset has changed since last saved
>
> . list, clean;
>                    posixct         rnum                xbigc
>         xc
>   1.   2014-01-24 05:08:18   1390540098   24jan2014 05:07:53
> 24jan2014 05:08:18
>   2.   2014-01-24 05:08:19   1390540099   24jan2014 05:07:54
> 24jan2014 05:08:19
>   3.   2014-01-24 05:08:20   1390540100   24jan2014 05:07:55
> 24jan2014 05:08:20
>   4.   2014-01-24 05:08:21   1390540101   24jan2014 05:07:56
> 24jan2014 05:08:21
>   5.   2014-01-24 05:08:22   1390540102   24jan2014 05:07:57
> 24jan2014 05:08:22
>   6.   2014-01-24 05:08:23   1390540103   24jan2014 05:07:58
> 24jan2014 05:08:23
>   7.   2014-01-24 05:08:24   1390540104   24jan2014 05:07:59
> 24jan2014 05:08:24
>   8.   2014-01-24 05:08:25   1390540105   24jan2014 05:08:00
> 24jan2014 05:08:25
>   9.   2014-01-24 05:08:26   1390540106   24jan2014 05:08:01
> 24jan2014 05:08:26
>  10.   2014-01-24 05:08:27   1390540107   24jan2014 05:08:02
> 24jan2014 05:08:27
>
> .   /*close log file*/
>> capture log close;
>
> [1] https://stat.ethz.ch/pipermail/r-devel/2014-January/068271.html
>
> --
> +---
> | Hiroyuki Kawakatsu
> | Business School, Dublin City University
> | Dublin 9, Ireland. Tel +353 (0)1 700 7496
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index