2

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"

  • `POSIXt` does not format midnight as `24`, so if you want 24 you'll need to either (a) convert from `POSIXt`-class to strings and reformat manually; or (b) super-class it (e.g., `class(x) <- c("myPOSIX", "POSIXt", "POSIXct")`) so that `01-02 00:` prints as `01-01 24:`, keeping it as numeric. If you want to super-class it, you'd need to provide at least `print.myPOSIX` and perhaps others. – r2evans Oct 06 '21 at 15:18
  • Hello, I have tried manual formatting obtaining a "character" class, however, I need to have it in some recognizable format as a date to apply my other functions, so ask if there is any alternative apart from "POSIXct". Greetings – marvinjonathcn Oct 06 '21 at 15:21
  • 1
    As I said, _no_. There is no current capability to remap that with existing code in R. A super-class approach is not that difficult, really, it just has a few corner cases to consider. – r2evans Oct 06 '21 at 15:24

1 Answers1

2

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
r2evans
  • 141,215
  • 6
  • 77
  • 149