I’m doing some extracting of data from a database, and running the results through rehsape2. For some reason this is mangling the POSIXct datetime stamps into numeric. No Problem I think, you can just turn them back, except I’m an hour out.
Here’s a minimal example
foo<-as.POSIXct("2011-04-04 14:18:58")
as.numeric(foo) #gives 130192318
bar<-as.POSIXct(as.numeric(foo),
tz=Sys.timezone(),
origin=as.POSIXct(
strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC")))
as.numeric(bar) #gives 130192318 identical !
foo #Gives "2011-04-04 14:18:58 BST"
bar #Gives "2011-04-04 13:18:58 UTC"
Obviously foo and bar are numerically identical, but R thinks foo needs to be displayed as BST and bar as UTC. How do I get both displayed as BST. This doesn’t work either;
as.POSIXct(bar, tz="BST") #still gives "2011-04-04 13:18:58 UTC"
Here’s what’s going on.
baris created usingas.POSIXct.numeric, which is defined as:You supply an origin that is a
POSIXctobject. That means theas.POSIXctcall inas.POSIXct.numericdispatches toas.POSIXct.default, which is defined as:xis aPOSIXctclass object (theoriginyou supplied in your initial call), so it is simply returned and thetz=argument is ignored.UPDATE:
Here’s how you can convert
fooback toPOSIXctwith the appropriate time zone.