2

Below is my R-code that is behaving weirdly. I expect a time of 22:00 as entered but i get 23:00.

as.POSIXct(chron(dates="01/04/06",times="22:00:00"),tz="CET")
[1] "2006-01-04 23:00:00 CET"

In the next line of my code i use the results to select a window from an xts/zoo object: Therefore just ignoring the error and instead entering 21:00 (in above) wasnt useful since it returns the wrong data. Windowing with the result of the code above returns the correct values.

head(qs<-as.zoo(window(Q,start=as.POSIXct(chron(dates="01/04/06",times="22:00:00"),tz="CET"),end=as.POSIXct(chron(dates="01/05/06",times="21:00:00"),tz="CET"))))

Here is a sample set of the data (Q):

Stage.Qm  Flow.Qm Stage.QmDB Flow.QmDB Stage.Q1000 Flow.Q1000 Stage.Q1000DB Flow.Q1000DB
2006-01-04 23:00:00 541.1589 5.636957   541.1592  5.646017    541.5708   20.44692      541.5708     20.44692
2006-01-04 23:01:00 541.1589 5.637268   541.1592  5.645087    541.5701   20.41321      541.5701     20.41321
2006-01-04 23:02:00 541.1589 5.638604   541.1588  5.635806    541.5701   20.40946      541.5701     20.40946
2006-01-04 23:03:00 541.1589 5.638979   541.1588  5.635694    541.5704   20.42712      541.5704     20.42712
2006-01-04 23:04:00 541.1589 5.639619   541.1590  5.640691    541.5710   20.45848      541.5710     20.45848
2006-01-04 23:05:00 541.1590 5.640662   541.1591  5.641682    541.5715   20.47893      541.5715     20.4789
Roland
  • 127,288
  • 10
  • 191
  • 288
jjunju
  • 505
  • 1
  • 5
  • 18
  • 3
    In the documentation you can read: "The current implementation of chron objects does not handle time zones nor daylight savings time." Thus, a solution would be to not use `chron` here. – Roland Mar 26 '14 at 15:38
  • Thanks. I have changed to `as.POSIXct(strptime("2006-01-04 22:00:00", "%Y-%m-%d %H:%M:%S"))` and this gives the desired `[1] "2006-01-04 22:00:00 CET"` – jjunju Mar 26 '14 at 15:51
  • 1. You don't need `strptime` there. 2. I recommend always setting the time zone explicitly. 3. Feel free to write an answer to your question. – Roland Mar 26 '14 at 15:53
  • Can you please use my code (in the comment above) and give an example. Thanks :) – jjunju Mar 26 '14 at 15:54

1 Answers1

5

In the documentation you can read: "The current implementation of chron objects does not handle time zones nor daylight savings time." Thus, a solution would be to not use chron here.

Just use as.POSIXct.default:

as.POSIXct("2006-01-04 22:00:00", "%Y-%m-%d %H:%M:%S", tz="CET")
[1] "2006-01-04 22:00:00 CET"
Roland
  • 127,288
  • 10
  • 191
  • 288
  • i have a somewhat related question that is not answerd by the above. can you tell me what is happening here? `> q1 2015-12-31 22:00:00 40.99648 > q2 2015-12-31 22:00:00 40.99648 > cbind(q1,q2) q1 q2 2015-12-31 23:00:00 40.99648 40.99648 ` the time changes when i do cbind or when i do nay operation on q1 and q2 by 1 hour including saving into a list. – jjunju Jun 15 '16 at 14:33
  • @jjunju If you have a new question, please ask a new question. Don't forget to provide a [reproducible example](http://stackoverflow.com/a/5963610/1412059) when you do that. – Roland Jun 15 '16 at 15:20