Skip to contents

[Maturing]

flat_posixt_* functions adjust the date or time of a POSIXt vector to a specific date or hour, with UTC as the default timezone.

These functions are particularly useful for performing time arithmetic on time objects that lack a date reference (e.g., hms objects).

Usage

flat_posixt_date(x, base = as.Date("1970-01-01"), force_tz = TRUE, tz = "UTC")

flat_posixt_hour(x, base = hms::parse_hm("00:00"), force_tz = TRUE, tz = "UTC")

Arguments

x

A posixt vector.

base

A Date (for flat_posixt_date() or hms (for flat_posixt_hour()) value (Default: as.Date("1970-01-01") (Unix Epoch) or hms::parse_hm("00:00")).

force_tz

A logical flag indicating whether to force the timezone of the output to tz (Default: TRUE).

tz

A character string indicating the timezone to use when force_tz is TRUE (Default: "UTC").

Value

A posixt vector.

See also

Other utility functions: change_date(), extract_seconds(), fix_hms(), get_last_week(), round_time()

Examples

as.POSIXct("2020-01-01 05:55:55", tz = "America/Sao_Paulo") |>
  flat_posixt_date()
#> [1] "1970-01-01 05:55:55 UTC"
#> [1] "1970-01-01 05:55:55 UTC" # Expected

c(
  as.POSIXct("2020-01-01 05:55:55", tz = "America/Sao_Paulo"),
  as.POSIXct("2020-01-01 18:40:05", tz = "America/Sao_Paulo")
) |>
 flat_posixt_date()
#> [1] "1970-01-01 05:55:55 UTC" "1970-01-01 18:40:05 UTC"
#> [1] "1970-01-01 05:55:55 UTC" "1970-01-01 18:40:05 UTC" # Expected

as.POSIXct("2020-01-01 05:55:55", tz = "America/Sao_Paulo") |>
 flat_posixt_hour(base = hms::parse_hm("00:01"))
#> [1] "2020-01-01 00:01:00 UTC"
#> [1] "2020-01-01 00:01:00 UTC" # Expected

c(
 as.POSIXct("2020-01-01 05:55:55", tz = "America/Sao_Paulo"),
 as.POSIXct("2020-01-01 18:40:05", tz = "America/Sao_Paulo")
) |>
 flat_posixt_hour(base = hms::parse_hm("00:01"))
#> [1] "2020-01-01 00:01:00 UTC" "2020-01-01 00:01:00 UTC"
#> [1] "2020-01-01 00:01:00 UTC" "2020-01-01 00:01:00 UTC" # Expected