rdatetimehdf5rhdf5

R date origin for time in seconds


I daily have a time series in seconds since the job start (00:00h UTC). As I want to plot time series of other data, I want convert time series in seconds to dates. All data comes from a hdf5 file read with package rhdf5

>tiempo=h5read("rams2.h5","t_coords")
> class(tiempo)
[1] "array"
> head(tiempo)
[1]    0 1800 3600 5400 7200 9000

Then I have tried and as.POSIXct to build a dataframe (to later use with ggplot2).

> temps<-as.data.frame(as.POSIXct(tiempo, origin = "1960-01-01 00:00"))
> class(temps)
[1] "data.frame"
> head(temps)
  as.POSIXct(tiempo, origin = "1960-01-01 00:00")
1                             1960-01-01 01:00:00
2                             1960-01-01 01:30:00
3                             1960-01-01 02:00:00
4                             1960-01-01 02:30:00
5                             1960-01-01 03:00:00
6                             1960-01-01 03:30:00

The problem comes fromt the first value of "temps"

1 1960-01-01 01:00:00

but it should be 1960-01-01 00:00:00 as this is the supplied origin and first value of "tiempo" is 0. It seems that an hour has been added to every time.

Maybe I'm missing something when setting origin? Or while reading h5 file? Any idea?

Thanks in advance

PD: I can't supply example data as the h5 file is a very huge one.

CORRECT CODE Thanks to ottlngr answer and Richard Telford comment, the problem was a time zone issue solved by adding tz= "UTC"

temps<-as.data.frame(as.POSIXct(tiempo, origin = "1960-01-01 00:00:00",tz = "UTC"))

Solution

  • Possibly you should use the tz argument in as.POSIXct(), but hard to tell without sample data. Take a look at this question, it might help you: Change timezone in a POSIXct object