From simon.urbanek at r-project.org  Tue Feb  8 23:31:15 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 8 Feb 2011 17:31:15 -0500
Subject: [Rd] manipulating the Date & Time classes
In-Reply-To: <AANLkTi=RKFKDZgrb072mefmOefjNwwoE_QT8ZWOwFSci@mail.gmail.com>
References: <AANLkTi=RKFKDZgrb072mefmOefjNwwoE_QT8ZWOwFSci@mail.gmail.com>
Message-ID: <273A1D8C-68B9-446C-BFD3-956CCEF0322D@r-project.org>

Mike,

On Feb 8, 2011, at 3:24 PM, Mike Williamson wrote:

> Hello,
> 
>    This is mostly to developers, but in case I missed something in my
> literature search, I am sending this to the broader audience.
> 
> 
>   - Are there any plans in the works to make "time" classes a bit more
>   friendly to the rest of the "R" world?  I am not suggesting to allow for
>   fancy functions to manipulate times, per se, or to figure out how to
>   properly "add" times or anything complicated.  Just some fixes to make it
>   easier to work with the "time" classes.  Here is a sampling of some strange
>   bugs with the time classes that, to my knowledge, don't exist with any other
>   core classes:
>      1. you cannot "unlist" a time without losing the class.  E.g., if you
>      unlist "2010-12-14 20:25:40" (POSIXct), you get "1292387141", at
> least on my
>      OS & with my time zone.  Regardless of the exact number, unlisting a time
>      class converts it to a numeric.

Same answer as Jeff said - it works for me. When posting claims like this it's good to provide some evidence like a reproducible example with details such as the version of R you used, OS etc.


>         - upon converting to a numeric, it seems there is an underlying,
>         assumed origin of "1970-01-01 00:00:00".  

You may want to read up on times a bit - POSIX time (aka unix time) *is* defined as number of seconds elapsed since midnight UTC 1970/1/1. That is also the internal representation of POSIXct (see ?POSIXct) that you get when unclassing it (not unlisting).  


> However, this same
> assumption does
>         not underlie the conversion *back* to a POSIX time, e.g., through
>         as.POSIXct() function.  

And it would be fatal if it did. When you have a number there is no way of knowing what the origin is, that's why it has to be specified. There are many different numeric times with various origins (e.g. Apple used to have its own one before OS X, Excel uses 1900 etc. - see Epoch).
 

> Therefore, whenever a time is "accidentally"
>         converted to a numeric, I have to force it back to a time through
>         as.POSIXct(), *providing my own details* as to the origin.  There
>         is no easy way to find the underlying origin.  

In general, there is no way. *If* your numbers come from unclassing POSIXct then you can simply set the class back - which avoid other hassles such as the fact that you have no idea about the target time zone of the original number otherwise.


> This makes me
> nervous for any
>         persistent functions I create.  If the underlying origin ever
> changes, then
>         all this code will be inaccurate.  

POSIX time is POSIX time (aka unix time) - that's where the name comes from! If that definition changes, we'll see more issues than your code ;).


> Maybe the origin will
> never change, but
>         regardless it makes more sense to allow for the same underlying origin
>         default for "as.POSIXct" that is used when unlisting, or
> similar activities
>         that force the time into a numeric.
>         2. you cannot perform functions that otherwise seem trivial, such
>      as a "max" or "min".

Again, I don't believe you:

> x = Sys.time() + 1:10
> min(x)
[1] "2011-02-08 17:17:47 EST"
> max(x)
[1] "2011-02-08 17:17:56 EST"
> max(x) - min(x)
Time difference of 9 secs


>  I understand why, for instance, adding is
> hard.  But
>      what about max or min?  Greater than or less than are possible, as is
>      order().  I have my own simple scripts using these 2 functions
> in order to
>      create a "max" & "min" for times, but it would be nice to have something
>      vetted & official.
> 
>    If others could chime in with any strange behaviors they've seen in
> working with times, maybe we could get a critical mass of issues that are
> worthy of an overhaul.
> 

Please do provide some real evidence, like reproducible examples. So far none of your claims was verifiable.

Cheers,
Simon




>                                          Thanks & Regards,
>                                                    Mike
> 
> 
> "Telescopes and bathyscaphes and sonar probes of Scottish lakes,
> Tacoma Narrows bridge collapse explained with abstract phase-space maps,
> Some x-ray slides, a music score, Minard's Napoleanic war:
> The most exciting frontier is charting what's already here."
>  -- xkcd
> 
> --
> Help protect Wikipedia. Donate now:
> http://wikimediafoundation.org/wiki/Support_Wikipedia/en
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



[Rd] manipulating the Date & Time classes
Simon Urbanek