[Rd] Date conversion with as.POSIXct and as.POSIXlt (PR#9196)

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Sep 1 15:16:27 CEST 2006

On Fri, 1 Sep 2006, Peter Dalgaard wrote:

> Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:
> > On Fri, 1 Sep 2006, Peter Dalgaard wrote:
> > 
> > [...]
> > 
> > > I'm not sure any of this is actual bugs (Brian will tell us soon
> > > enough), but it does appear at least to be underdocumented.
> > 
> > (Your message crossed mine.)
> > 
> > I think it is not a bug and actually explicitly documented in ?as.POSIXlt.  
> > What may help those who have not read that documentation is to stick the 
> > UTC timezone on the conversion to POSIXlt.  Getting
> Hmmm, which part of ?as.POSIXlt didn't I understand? I can't seem to
> spot anything pointing to -lt being different from -ct conversion (in
> a week-old version of r-devel). What I do see is:
>     The 'as.POSIX*' functions convert an object to one of the two
>      classes used to represent date/times (calendar dates plus time to
>      the nearest second).  They can convert a wide variety of objects,
>      including objects of the other class and of classes '"Date"',
>      '"date"' (from package 'date' or 'survival'), '"chron"' and
>      '"dates"' (from package 'chron') to these classes.  Dates are
>      treated as being at midnight UTC.
> The last sentence would appear not to be true for as.POSIXlt; dates
> are treated as midnight in the _current_ timezone ignoring DST (which
> of course is undetectable in Oxford because that _is_ UTC in your
> timezone) .

As it tells you it was treated as being in UTC, you need to tell 
subsequent conversions that it is a time in UTC, e.g.

   as.POSIXct(as.POSIXlt(Sys.Date()), tz="UTC")

It is as.POSIXct() that is doing 'dates are treated as midnight in the 
_current_ timezone ignoring DST', and that is what the documentation of 
argument 'tz' says will happen.  (Since UTC has no DST, it has specified 
that DST is not in effect, as that is part of a POSIXlt object.)

In your version of R, as.POSIXlt(Sys.Date()) did not specify a timezone 
(they are optional).  Now it does.

> The documentation in ?POSIXlt does have some hints about POSIXlt being
> converted to current locale, but I still find the whole thing a bit
> unclear.

Which is why I have (I hope) clarified it so tz="UTC" is not needed 
explicitly anymore.

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

More information about the R-devel mailing list