[R] convert character to POSIXct

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Sep 2 17:21:10 CEST 2003


You converted to POSIXct in your local time zone.  It has a tz argument: 
use it if you want GMT.

On Tue, 2 Sep 2003, Aarts, Geert wrote:

> I would like to calculate the difference between two points in time. To
> convert a 'time (GMT)'-character with the format "1/1/1999 01:01:01"
> into an object of class "POSIXct"', I first use the strptime() as
> suggested in the details help(as.POSIXct).

> e.g.
> starttime<-strptime("1/1/1999 01:01:01",format="%d/%m/%Y %H:%M:%S")
> endtime<-strptime("1/8/1999 01:01:01",format="%d/%m/%Y %H:%M:%S")
> > starttime
> [1] "1999-01-01 01:01:01"
> > endtime
> [1] "1999-08-01 01:01:01"
> 
> If I substract these from each other, 
> endtime-starttime
> >Time difference of 211.9583 days
> Which should mean that the default is in day light saving.

It's your local timezone, as the help page says.

> Next I convert these times into as.POSIXct and substract them
>  as.POSIXct(starttime)
> [1] "1999-01-01 01:01:01 GMT Standard Time"
> as.POSIXct(endtime)
> [1] "1999-08-01 01:01:01 GMT Standard Time"
> as.POSIXct(endtime)-as.POSIXct(starttime)
> Time difference of 211.9583 days
> 
> So although the calculation suggest time is in daylight saving, it
> actually states that it is in "GMT Standard Time" Can somebody tell me
> whether my thoughts are correct and if not, tell me why this is
> happening?

Because you asked for it to be done!

> PS.
> > as.POSIXct(endtime, tz="GMT")-as.POSIXct(starttime, tz="GMT")
> Time difference of 212 days
> So this seems to work correctly.

So does the rest of your code.


-- 
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-help mailing list