# [R] How to get time differences in consistent units?

Patrick Connolly p.connolly at hortresearch.co.nz
Fri Feb 13 03:07:49 CET 2004

```I'm still having trouble getting to grips with time classes.

I wish to calculate the difference in days between events.

Browse[1]> insp.j\$First
[1] "2002-02-19 13:00:00 NZDT"
Browse[1]> spray.j\$Date
[1] "2001-11-29 13:00:00 NZDT"
Browse[1]> insp.jk - spray.j\$Date
Time difference of 82 days

If I save insp.jk to a vector, I get a nice useful value of 82.

However, when the dreaded daylight savings enters the picture, we get
this sort of thing:

Browse[1]> insp.j\$First
[1] "2003-02-14 13:00:00 NZDT"
Browse[1]> spray.j\$Date
[1] "2002-12-16 13:00:00 NZDT" "2003-01-15 13:00:00 NZDT"
[3] "2003-02-14 13:00:00 NZDT" "2003-02-14 13:00:00 NZDT"
[5] "2003-03-25 12:00:00 NZST"
Browse[1]> insp.jk - spray.j\$Date
Time differences of  5184000,  2592000,        0,        0, -3369600 secs

Saving that insp.jk to a vector, I get one in seconds which isn't
simply comparable to others.  It would be simple enough to put in an
as.numeric() so that comparisons are always in seconds, but it would
be preferable to have some control over how the difference is reported.

Looking through previous discussions on this sort of thing I thought I
could save hassle by using tz = GMT for everything which is what I've
tried, and hence that's why the times are shown as 1pm and noon
depending on whether it's NZST or NSDT.  It appears to me that while
the dates are known to the software as GMT, they are displayed in
local time equivalent but before the differnce between them is
calculated, that converson happens again whether we like it or not.

Evidently, that's not what happens when as.numeric() is used before
calculating the difference since in that case (with my data), the
difference is always a whole number of days which is appropriate.

Is my experience with date differences standard behaviour or an OS
idiosyncrasy?

platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    1
minor    8.1
year     2003
month    11
day      21

Redhat 7.3 (with the dreaded gcc-2.96 compiler)

best

--
Patrick Connolly
HortResearch
Mt Albert
Auckland
New Zealand
Ph: +64-9 815 4200 x 7188
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it.  ---Steven Wright
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~

```