Skip to content
Prev 69552 / 398525 Next

time zones, daylight saving etc.

Hi,

seq.dates() in the chron package does not allow creating sequences by
minutes, so you'd have to roll your own sequence generator.

Looks like the tzone attribute of the times is lost when using min(),
max() and seq().  You can apply it back manually, but it does not
affect the calculation, since POSIXct times are stored as seconds
since 1/1/1970 (?DateTimeClasses).

## These dates/times just span the move from NZDT to NZST:
dt.dates <- paste(rep(15:16, c(5,7)), "03", "2003", sep="/")
dt.times <- paste(c(19:23, 0:6), "05", sep=":")
dt <- paste(dt.dates, dt.times)

## No shift in times, or worrying about daylight savings; appropriate
## iff the device doing the recording was not itself adjusting for
## daylight savings, presumably.
datetime <- as.POSIXct(strptime(dt, "%d/%m/%Y %H:%M"), "GMT")

## Create two objects with all the times in your range, one with the
## tzone attribute set back to GMT (to match datetimes), and one
## without this.
mindata1 <- mindata2 <- seq(from=min(datetime), to=max(datetime),
                            by="mins")
attr(mindata2, "tzone") <- "GMT"

fmt <- "%Y %m %d %H %M"
## These both do the matching correctly:
match(format(datetime, fmt), format(mindata1, fmt, tz="GMT"))
match(format(datetime, fmt), format(mindata2, fmt, tz="GMT"))

## However, the first of these will not, as it gets the timezone all
## wrong, since it's neither specified in the call to format(), or as
## an attribute of the POSIXct object.
match(format(datetime, fmt), format(mindata1, fmt))
match(format(datetime, fmt), format(mindata2, fmt))

## It is also possible to run match() directly off the POSIXct object,
## but I'm not sure how this will interact with things like leap
## seconds:
match(datetime, mindata1)

Time zones do my head in, so you probably want to check this all
pretty carefully.  Looks like there's lots of gotchas (e.g. subsetting
a POSIXct object strips the tzone attribute).

Cheers,
Rich
On 5/12/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: