# [R] Chron, as.POSIXct problem

Gabor Grothendieck ggrothendieck at myway.com
Wed Nov 12 17:39:23 CET 2003

```

You are being hit by a timezone problem.  Its not really shifting
the days by one.  Its working in the GMT timezone, not yours.

If you can accept a date format that chron supports then this is the
easiest solution since chron does not support timezones and so can't
give you such problems in the first place.  For example,
the following stays in chron the whole time:

format(datesTest, format="m/day/year")
[1] "Oct/01/1952" "Oct/02/1952" "Oct/03/1952"

If you must convert to POSIXt to take advantage of a format
only supported by POSIXt then use POSIXlt and specify the timezone explictly:

format(as.POSIXlt(datesTest,tz="GMT"), "%m/%d/%Y")
[1] "10/01/1952" "10/02/1952" "10/03/1952"

Its because of subtle problems like this that I think that some
sort of naive (i.e. non-timezone) time such as chron or
an alternative, should be in the base to encourage wider use.

---
Date: Wed, 12 Nov 2003 10:05:39 -0500
From: Brian Beckage <bbeckage at uvm.edu>
To: <r-help at stat.math.ethz.ch>
Subject: [R] Chron, as.POSIXct problem

Dear R list,

I noticed the following 'problem' when changing the format of dates
created with seq.dates() (from the Chron library) using as.POSIXct()
(R 1.8.0 on OSX 10.2.8):

> datesTest<-seq.dates(from="10/01/1952", length=3, by="days");
> datesTest
[1] 10/01/52 10/02/52 10/03/52

# Now changing the format to show year as 1952.

> datesTest<-format(as.POSIXct(datesTest), "%m/%d/%Y")
> datesTest
[1] "09/30/1952" "10/01/1952" "10/02/1952"
>

The dates were shifted by one day. The work around is simple enough, e.g.,

> datesTest<-format(as.POSIXct(datesTest+1), "%m/%d/%Y")
[1] "10/01/1952" "10/02/1952" "10/03/1952"

but I wonder if this is the intended behavior?

Brian

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

```