1

I'm trying to get the midpoint of two date_times rounded to nearest hour with hms preserved in output.

For example, here I want the midpoint to be 2016-01-15 00:00:00

library(lubridate)
Sys.setenv(TZ="Europe/London") #needing to establish TZ/use other TZs later on

start_datetime <- as.POSIXct("2016-01-13 08:00:00", tz = "Europe/London")
end_datetime   <- as.POSIXct("2016-01-16 15:00:00", tz = "Europe/London")

# using R's internal setting of 1970-01-01, gets 2016-01-14 23:30:00, 
# which seems right
midpoint <- as.POSIXct((as.numeric(end_datetime) + 
                      as.numeric(start_datetime)) / 2, origin = '1970-01-01') 

#gets 2016-01-15 only (no hms):
midpoint_nearesthour_fail1 <- round_date(midpoint, "hours") #lubridate function

#gets 2016-01-15 only (no hms):
midpoint_nearesthour_fail2 <- round(midpoint, "hours")  #base R function

#gets 2016-01-14 23:30:00 as above (as expected, with no rounding)
#but interestingly, if all three "Europe/London" replaced with "America/Los_Angeles",:
#gets: 2016-01-15 07:30:00
midpoint_nearesthour_fail3 <- as_datetime(midpoint) 
dbo
  • 1,174
  • 1
  • 11
  • 19
  • 2
    I think it's 'just' how `print.POSIXct` handles hours-min-sec at midnight. Try `as.POSIXct("2018-12-02 00:00:00")`; `format(as.POSIXct("2018-12-02 00:00:00"), "%Y-%m-%d %H:%M:%S")`. – Henrik Dec 02 '18 at 20:10
  • Related: [How can I keep midnight (00:00h) using strptime() in R?](https://stackoverflow.com/questions/37089536/how-can-i-keep-midnight-0000h-using-strptime-in-r) – Henrik Dec 02 '18 at 20:20
  • Out of curiousity, I had to check how `difftime` behaved here; seemed to work: `format(round(start_datetime + difftime(end_datetime, start_datetime) / 2, "hours"), "%F %T")` – Henrik Dec 02 '18 at 20:26
  • @Henrik, though I see it puts quotes on the output, and I wonder if using that quoted date to filter other data.frames with POSIXct formatted columns without quotes should still work? – dbo Dec 02 '18 at 20:33
  • 1
    The result of `format` is a `character` representation, which IMHO is 'only' useful for the final output/printing format in a report. As long as you 'do things' with the times in R, it's much more convenient to keep the `POSIXct` class. About printing, compare an object with the 'midnight time' only: `as.POSIXct("2018-12-02 00:00:00")` with a vector with additional values: `as.POSIXct("2018-12-02 00:00:00") + -1:1` – Henrik Dec 02 '18 at 20:41

0 Answers0