Skip to contents

Takes any number of datetime columns and changes their timezones either totally at random, or from a user-provided list of timezones.

Usage

messy_datetime_tzones(data, cols = NULL, tzones = OlsonNames(), force = FALSE)

Arguments

data

input dataframe

cols

set of columns to apply transformation to. If NULL will apply to all POSIXt columns.

tzones

Valid time zones to sample from. By default samples from all OlsonNames(), but can be set to options more relevant to the data.

force

By default (force = FALSE) the datetimes will have their actual hour/minute values changed along with the timezones. If force = TRUE, which requires lubridate, the datetime values will remain the same and only the timezone will differ.

Value

a dataframe the same size as the input data.

See also

Other Messy date(time) functions: messy_datetime_formats(), split_datetimes()

Author

Jack Davison

Examples

data <- data.frame(dates = rep(Sys.time(), 10))

data$dates
#>  [1] "2024-12-03 23:03:51 UTC" "2024-12-03 23:03:51 UTC"
#>  [3] "2024-12-03 23:03:51 UTC" "2024-12-03 23:03:51 UTC"
#>  [5] "2024-12-03 23:03:51 UTC" "2024-12-03 23:03:51 UTC"
#>  [7] "2024-12-03 23:03:51 UTC" "2024-12-03 23:03:51 UTC"
#>  [9] "2024-12-03 23:03:51 UTC" "2024-12-03 23:03:51 UTC"
attr(data$dates, "tzone")
#> NULL

messy <- messy_datetime_tzones(data, tzones = "Poland")
messy$dates
#>  [1] "2024-12-04 00:03:51 CET" "2024-12-04 00:03:51 CET"
#>  [3] "2024-12-04 00:03:51 CET" "2024-12-04 00:03:51 CET"
#>  [5] "2024-12-04 00:03:51 CET" "2024-12-04 00:03:51 CET"
#>  [7] "2024-12-04 00:03:51 CET" "2024-12-04 00:03:51 CET"
#>  [9] "2024-12-04 00:03:51 CET" "2024-12-04 00:03:51 CET"
attr(messy$dates, "tzone")
#> [1] "Poland"