Skip to content

Class for time of day?

11 messages · Stavros Macrakis, jim holtman, Gabor Grothendieck

#
There is a times class in the chron package.  Times are measured
in fractions of a day so 1/24 is one hour.
[1] 12:27:46
[1] 13:27:46

There is an article on dates and times in R News 4/1.

On Wed, May 20, 2009 at 10:57 AM, Stavros Macrakis
<macrakis at alum.mit.edu> wrote:
1 day later
#
On Thu, May 21, 2009 at 5:04 PM, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
It uses hours/minutes/seconds for values < 1 day and uses days and fractions
of a day otherwise.

For values and operations that it has not considered it falls back to
the internal
representation.

Most of your examples start to make sense once you realize this.
#
You could create a subclass of times with its own print and format
methods that printed and formatted hours/minutes/seconds even if
greater than one day if that is the main item you need.

Regarding division you could contribute that to the chron package.
I've contributed a few missing items and they were incorporated.

Giving an error when it does not understand something would be
dangerous as it could break much existing code so that would
probably not be possible at this stage.
The idea of defaulting to internal representations is based on
the idea that you get many features for free since the way the
internal representations work gives the right answer in many
cases.   Its best to stick with the implicit philosophy that
underlies a package.  If you want a different philosophy then
its really tantamount to creating a new package.  I don't
think that one is right and the other wrong but simply
represent different viewpoints.
On Fri, May 22, 2009 at 9:33 AM, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
#
On Fri, May 22, 2009 at 11:01 AM, Stavros Macrakis
<macrakis at alum.mit.edu> wrote:
The way this might appear in code is if someone wanted to calculate the
number of one hour intervals in 18 hours.  One could write:

t18 <- times("18:00:00")
t1 <- times("1:00:00")
as.numeric(t18) / as.numeric(t1)

but since we all know that it uses internal representations unless it
indicates otherwise a typical code snippet might shorten it to:

as.numeric(t18 / t1)

and all such code would break if one were to cause that to generate an error.
(I think it would be ok if it generated a numeric automatically and that was
the enhancement I had suggested to you.)

You can try to argue that the user should not code that way but a huge
amount of chron code exists by now (note that chron may pre-date R).
If it were a new package one could consider raising new errors but at this
point in time it would be unwise.
#
On Fri, May 22, 2009 at 1:55 PM, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
Your point was that otherwise undefined operations should produce an
error and I was illustrating why that could not be introduced at this stage.
I had already suggested that you implement division if you found it important
and that was not the source of any disagreement.