using the "POSIXct" function I get a date format from 00:00 H to 23:00 H for the same day, however I need to have the format from 01:00 H to 24:00 H for the same day. Is there an alternative to obtain this format?
[1] "2016-01-01 01:00:00 -05" "2016-01-01 02:00:00 -05" "2016-01-01 03:00:00 -05" [4] "2016-01-01 04:00:00 -05" "2016-01-01 05:00:00 -05" "2016-01-01 06:00:00 -05" [7] "2016-01-01 07:00:00 -05" "2016-01-01 08:00:00 -05" "2016-01-01 09:00:00 -05" [10] "2016-01-01 10:00:00 -05" "2016-01-01 11:00:00 -05" "2016-01-01 12:00:00 -05" [13] "2016-01-01 13:00:00 -05" "2016-01-01 14:00:00 -05" "2016-01-01 15:00:00 -05" [16] "2016-01-01 16:00:00 -05" "2016-01-01 17:00:00 -05" "2016-01-01 18:00:00 -05" [19] "2016-01-01 19:00:00 -05" "2016-01-01 20:00:00 -05" "2016-01-01 21:00:00 -05" [22] "2016-01-01 22:00:00 -05" "2016-01-01 23:00:00 -05" "2016-01-02 00:00:00 -05" [25] "2016-01-02 01:00:00 -05" "2016-01-02 02:00:00 -05" "2016-01-02 03:00:00 -05"
to
[1] "2016-01-01 01:00:00 -05" "2016-01-01 02:00:00 -05" "2016-01-01 03:00:00 -05" [4] "2016-01-01 04:00:00 -05" "2016-01-01 05:00:00 -05" "2016-01-01 06:00:00 -05" [7] "2016-01-01 07:00:00 -05" "2016-01-01 08:00:00 -05" "2016-01-01 09:00:00 -05" [10] "2016-01-01 10:00:00 -05" "2016-01-01 11:00:00 -05" "2016-01-01 12:00:00 -05" [13] "2016-01-01 13:00:00 -05" "2016-01-01 14:00:00 -05" "2016-01-01 15:00:00 -05" [16] "2016-01-01 16:00:00 -05" "2016-01-01 17:00:00 -05" "2016-01-01 18:00:00 -05" [19] "2016-01-01 19:00:00 -05" "2016-01-01 20:00:00 -05" "2016-01-01 21:00:00 -05" [22] "2016-01-01 22:00:00 -05" "2016-01-01 23:00:00 -05" "2016-01-01 24:00:00 -05" [25] "2016-01-02 01:00:00 -05" "2016-01-02 02:00:00 -05" "2016-01-02 03:00:00 -05"
CodePudding user response:
Here's a super-class thought:
format.myPOSIX <- function(x, tz = "", usetz = FALSE, ...) {
midnight <- format.POSIXct(x, format = "%H") == "00"
dayminus1 <- x - 86400
x[midnight] <- dayminus1[midnight]
gsub(" 00:", " 24:", format.POSIXct(x, tz = tz, usetz = usetz))
}
print.myPOSIX <- function(x, tz = "", usetz = TRUE, ...) {
print(format.myPOSIX(x, tz = tz, usetz = usetz))
}
Demonstration:
vec <- seq(as.POSIXct("2020-01-01 22:00:00"), as.POSIXct("2020-01-02 02:00:00"), by = "hour")
vec
# [1] "2020-01-01 22:00:00 EST" "2020-01-01 23:00:00 EST"
# [3] "2020-01-02 00:00:00 EST" "2020-01-02 01:00:00 EST"
# [5] "2020-01-02 02:00:00 EST"
class(vec) <- c("myPOSIX", class(vec))
vec
# [1] "2020-01-01 22:00:00 EST" "2020-01-01 23:00:00 EST"
# [3] "2020-01-01 24:00:00 EST" "2020-01-02 01:00:00 EST"
# [5] "2020-01-02 02:00:00 EST"
data.frame(datetime = vec)
# datetime
# 1 2020-01-01 22:00:00
# 2 2020-01-01 23:00:00
# 3 2020-01-01 24:00:00
# 4 2020-01-02 01:00:00
# 5 2020-01-02 02:00:00
tibble::tibble(datetime = vec)
# # A tibble: 5 x 1
# datetime
# <dttm>
# 1 2020-01-01 22:00:00 2020-01-01 22:00:00
# 2 2020-01-01 23:00:00 2020-01-01 23:00:00
# 3 2020-01-01 24:00:00 2020-01-01 24:00:00
# 4 2020-01-02 01:00:00 2020-01-02 01:00:00
# 5 2020-01-02 02:00:00 2020-01-02 02:00:00