Skip to content

timeDate

10 messages · João Mendes Moreira, Gabor Grothendieck, Uwe Ligges +2 more

#
what package should I include to use timeDate? I want to convert a
double (num of millis) into date object.
#
library("chron")

Joao
----- Original Message ----- 
From: "Yasser El-Zein" <abu3ammar at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, November 22, 2004 5:45 PM
Subject: [R] timeDate
#
Yasser El-Zein <abu3ammar <at> gmail.com> writes:

: 
: what package should I include to use timeDate? I want to convert a
: double (num of millis) into date object.

The times class of the chron package stores times as fractions of
a day.  You could check whether or not that gives you the resolution you
need.  Install chron and issue the R commands:

library(chron)
?chron

If you only need resolution to the second then you could alternately
use the POSIXct class.

Also check out the article on dates and times in R News 4/1 which
discusses the various possibilities.
#
Yasser El-Zein wrote:

            
You might want to read the Help Desk column in R News 4 (1).

Uwe Ligges
#
a double representing the number of millis since 1/1/1970 and I need
to construct a datetime object). I see it showing how to construct the
time object from a string representing the time but now fro a double
of millis. Does anyone know hoe to do that?


On Mon, 22 Nov 2004 19:24:48 +0100, Uwe Ligges
<ligges at statistik.uni-dortmund.de> wrote:
#
Yasser El-Zein <abu3ammar <at> gmail.com> writes:
If by millis you mean milliseconds (i.e. one thousandths of a second)
then POSIXct does not support that resolution, but if rounding to 
seconds is ok then 

  structure(round(x/1000), class = c("POSIXt", "POSIXct"))

should give it to you assuming x is the number of milliseconds.
#
In which case, be aware that R's round() function rounds to the 
nearest even number when the fraction is 0.5:
[1] 2 2

which might not be desirable for times. Consider floor() or trunc() instead.
[1] 1 2
[1] 1 2
At 9:48 PM +0000 11/22/04, Gabor Grothendieck wrote:

  
    
#
I am looking for up to the millisecond resolution. Is there a package
that has that?


On Mon, 22 Nov 2004 21:48:20 +0000 (UTC), Gabor Grothendieck
<ggrothendieck at myway.com> wrote:
#
Yasser El-Zein <abu3ammar <at> gmail.com> writes:

: 
: I am looking for up to the millisecond resolution. Is there a package
: that has that?
: 
: On Mon, 22 Nov 2004 21:48:20 +0000 (UTC), Gabor Grothendieck
: <ggrothendieck <at> myway.com> wrote:
: > Yasser El-Zein <abu3ammar <at> gmail.com> writes:
: > 
: > >
: > > >From the document it is apparent to me that I need as.POSIXct  (I have
: > > a double representing the number of millis since 1/1/1970 and I need
: > > to construct a datetime object). I see it showing how to construct the
: > > time object from a string representing the time but now fro a double
: > > of millis. Does anyone know hoe to do that?
: > >
: > 
: > If by millis you mean milliseconds (i.e. one thousandths of a second)
: > then POSIXct does not support that resolution, but if rounding to
: > seconds is ok then
: > 
: >   structure(round(x/1000), class = c("POSIXt", "POSIXct"))
: > 
: > should give it to you assuming x is the number of milliseconds.

There is no package/class that represents times and dates
internally as milliseoncds since Jan 1, 1970.   You can
rework your data into chron's internal representation, viz.
day number plus fraction of day, like this:

	# x is vector of milliseconds since Jan 1/70
	# x.chron is corresponding chron date/time
	# untested
	library(chron) 
	ms.in.day <- 1000*24*60*60 
	day <- floor(x/ms.in.day) 
	frac <- (x-1000*day)/ms.in.day
	x.chron <- chron(day+frac)

If you need to take leap seconds into account (which the above
does not) then note that R comes with a builtin vector called
leap.seconds.
#
Gabor Grothendieck <ggrothendieck <at> myway.com> writes:

: 
: Yasser El-Zein <abu3ammar <at> gmail.com> writes:
: 
: : 
: : I am looking for up to the millisecond resolution. Is there a package
: : that has that?
: : 
: : On Mon, 22 Nov 2004 21:48:20 +0000 (UTC), Gabor Grothendieck
: : <ggrothendieck <at> myway.com> wrote:
: : > Yasser El-Zein <abu3ammar <at> gmail.com> writes:
: : > 
: : > >
: : > > >From the document it is apparent to me that I need as.POSIXct  (I have
: : > > a double representing the number of millis since 1/1/1970 and I need
: : > > to construct a datetime object). I see it showing how to construct the
: : > > time object from a string representing the time but now fro a double
: : > > of millis. Does anyone know hoe to do that?
: : > >
: : > 
: : > If by millis you mean milliseconds (i.e. one thousandths of a second)
: : > then POSIXct does not support that resolution, but if rounding to
: : > seconds is ok then
: : > 
: : >   structure(round(x/1000), class = c("POSIXt", "POSIXct"))
: : > 
: : > should give it to you assuming x is the number of milliseconds.
: 
: There is no package/class that represents times and dates
: internally as milliseoncds since Jan 1, 1970.   You can
: rework your data into chron's internal representation, viz.
: day number plus fraction of day, like this:
: 
: 	# x is vector of milliseconds since Jan 1/70
: 	# x.chron is corresponding chron date/time
: 	# untested
: 	library(chron) 
: 	ms.in.day <- 1000*24*60*60 
: 	day <- floor(x/ms.in.day) 
: 	frac <- (x-1000*day)/ms.in.day
: 	x.chron <- chron(day+frac)

Not sure why I made the above so complicated but it can
be written just as:

        library(chron)
        ms.in.day <- 1000*24*60*60
        x.chron <- chron(x/ms.in.day)

: If you need to take leap seconds into account (which the above
: does not) then note that R comes with a builtin vector called
: leap.seconds.