0

I had some code that takes in a time and then takes a way a set number of seconds. All works fine apart from an edge case where when you take away the number of seconds you end up at midnight. In the code I am using when this situation occurs, the seconds part of the time disappears. The subsequent code then fails because it is expecting the time in a specific format.

Any ideas how to deal with this situation so the subsequent code doesn't give unexpected results.

Ignore the time zones in the following - I'm just interested in the disappearing seconds.

BaseTime <- "2015-03-25 00:01:00"
adjustment <- 30
GMT1 <- strptime(BaseTime,"%Y-%m-%d %H:%M:%S") 
GMTadj <- GMT1 - adjustment
GMTadj
# [1] "2015-03-25 00:00:30 EDT"

GMT <- as.POSIXct(strptime(as.character(GMTadj),"%Y-%m-%d %H:%M:%S"),tz = "GMT") GMT
# [1] "2015-03-25 00:00:30 GMT"

adjustment <- 60
GMT1 <- strptime(BaseTime,"%Y-%m-%d %H:%M:%S")
GMTadj <- GMT1 - adjustment
GMTadj
# [1] "2015-03-25 EDT"

GMT <- as.POSIXct(strptime(as.character(GMTadj),"%Y-%m-%d %H:%M:%S"),tz = "GMT")
GMT
# [1] NA
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Why are you taking a date type and converting to character just to convert back to date type? If you need to format a date as a character in a particular format, use `strftime()` or `format()` rather than `as.character()`. – MrFlick May 15 '15 at 04:12
  • This extra line of code did the trick GMTadj <- format(GMTadj,"%Y-%m-%d %H:%M:%S") – user2589499 May 15 '15 at 04:29
  • thanks Mr Flick - but that didn't really answer the question. The answer is to do with time zones. – user2589499 May 15 '15 at 04:31
  • 2
    Then what is the question? If you have `x<-1.123` and then do `x-.123`, R prints `1` rather than `1.000` values are formatted for the console or as characters as efficiently as possible. Just as R doesn't always print a bunch of trailing 0's with numbers, it chooses not to print dates with times when there are 0 seconds since midnight. You can override this with explicit formatting if you need a particular form for the character versions of objects. A character value is distinct from the actual representation of a POSIXct object. – MrFlick May 15 '15 at 05:27
  • The question was 'Any ideas how to deal with this situation so the subsequent code doesn't give unexpected results.' ie how to prevent the NA appearing in the example given. I found a solution by forcing the seconds back by using format. I have thousands of datetimes passing through this function and it only spits the dummy when it becomes exactly midnight, so the efficiency/inconsistency of R is not that helpful in this case - unless there is a more robust form of the function I should be using - hence why I was asking the question. – user2589499 May 15 '15 at 07:33

1 Answers1

1

As MrFlick said, if you don't like the default format when converting POSIXct to character, then you need to explicitly specify the format in the as.character call.

R> as.character(GMTadj, "%Y-%m-%d %H:%M:%S")
[1] "2015-03-25 00:00:00"
Community
  • 1
  • 1
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thanks. That is what I was after. You only find you need such strict code when it crashes - which in my case was one record in 120 million. – user2589499 May 16 '15 at 06:25