>From POSIXct to numeric and back with time zone 

I am running regressions on data which has time series with different time 
resolution. Some data has hourly resolution, while most has either daily or 
weekly resolution. Aggregation is used to make the hourly data daily, while 
liner interpolation is used to find daily data from the weekly time series. 
This data manipulation requires some careful handling of date and time.

I do travel across time zones and want my code to keep working as the system 
time zone changes.

So far quick fixes have been used to handle problems. Now I am trying to get a 
grip and make a more robust solution. Google and forums have left me with an 
increasing amount of questions instead of answers. 

I have chosen one question and one problem. The question, which should be 
trivial, should allow me to solve the problem. However, I have been stuck with 
this all day so if anyone know the solution to the problem straight away, it 
will be highly appreciated.


The question: What does the tz attribute in POSIXct do?



As an example, two dates with different time zone attributes, tmp1 and tmp2, 
are compared.


> tmp1 = as.POSIXct('2000-01-30',origin = '1970-01-01', tz = "UTC")

> tmp1

[1] "2000-01-30 UTC"


> tmp2 = as.POSIXct('2000-01-30',origin = '1970-01-01', tz = "ETC")

> tmp2

[1] "2000-01-30 UTC"


The time displayed, including the time zone, is the same but the tzone 
attributes are not.


> attributes(tmp1)

$class

[1] "POSIXct" "POSIXt"


$tzone

[1] "UTC"



> attributes(tmp2)

$class

[1] "POSIXct" "POSIXt"


$tzone

[1] "ETC"


As a final check the numbers are compared


> as.numeric(tmp1)

[1] 949190400

> as.numeric(tmp2)

[1] 949190400


and they match.


I was under the impression that POSIXct always used UTC and that the tzone 
attribute was only for displaying and converting to POSIXlt but that seems 
wrong in the above example. As far as I can see, the tzone attribute is neither 
used for display, as both dates display as UTC, and not used to change to 
origin, as both numbers are the same. My question is, what does the tzone 
attribute in POSIXct actually do?


I hope increased understanding of that part will let me solve the true problem 
without further assistance.




The problem: from POSIXct to numeric and back.


> tmp3 = as.POSIXct( '2000-01-30', origin = '1970-01-01' )

tmp3

[1] "2000-01-30 CET" 


Converting it to numeric and back to POSIXct it becomes

> as.POSIXct( as.numeric( tmp3 ),origin = '1970-01-01' )

[1] "2000-01-29 23:00:00 CET"


which is "2000-01-30 UTC". By converting to numeric and back to POSIXct, an 
hour has been added. This is not the behavior I want. I am trying to sett the 
tz attribute but it does not change the added hour.


Trying to understand more of what is going on and to replicate the original 
date, I set the time zone to be CET in both conversions.


as.POSIXct( as.numeric( as.POSIXct( '2000-01-30', origin = '1970-01-01', tz = 
"CET" ) ), origin = '1970-01-01', tz = "CET" )

[1] "2000-01-29 23:00:00 CET"


Which is "2000-01-30 UTC". Choosing set the time zone to be UTC in both 
conversions,


as.POSIXct( as.numeric( as.POSIXct( '2000-01-30', origin = '1970-01-01', tz = 
"UTC" ) ),
origin = '1970-01-01', tz = "UTC" )

[1] "2000-01-30 UTC",


I want to convert the date "2000-01-30 CET" to POSIXct and then over to numeric 
before finally converting back to POSIXct without changing the date, time or 
time zone. I seem to get  "2000-01-30 UTC" regardless of what I try so I am 
definitely missing something obvious.


Best Regards


Daniel Haugstvedt

Ph.d.-student, 

NTNU, Trondheim, Norway


PS. I am aware that my spelling is poor. Any comments on how it could be 
improved are appreciated but send it to me personally and not the list.  
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to