Skip to content

Different behaviour for unrecognized timezones between with_tz and force_tz #1186

@atsyplenkov

Description

@atsyplenkov

When an unrecognised timezone is passed to the tzone argument of the with_tz() and force_tz(), the behaviour of functions is different. First just returns a warning and does nothing, while the latter aborts.

I am just curious if there is any hidden logic for this. Why cannot both of them just throw warnings or errors?

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(quickcheck)

df <- data.frame(
  time = posixct_() |> show_example()
)

# Behaviour is similar
force_tz(df$time, tzone = "US/Alaska")
#> [1] "2116-11-19 08:52:58 AKST" "0571-11-09 16:05:56 LMT" 
#> [3] "0988-09-08 09:54:51 LMT"
with_tz(df$time, tzone = "US/Alaska")
#> [1] "2116-11-18 10:52:58 AKST" "0571-11-09 18:27:16 LMT" 
#> [3] "0988-09-08 12:16:11 LMT"

# Both aborts but with different error messages
force_tz(df$time, tzone = NA)
#> Error in if (length(tz) == 1L && tz == tzout) {: missing value where TRUE/FALSE needed
with_tz(df$time, tzone = NA)
#> Error: Invalid input type, expected 'character' actual 'logical'

# Behaviour is different
force_tz(df$time, tzone = "foo")
#> Error in C_force_tz(to_posixct(time), tz, roll_dst): CCTZ: Unrecognized output timezone: "foo"
with_tz(df$time, tzone = "foo")
#> Warning in with_tz.default(df$time, tzone = "foo"): Unrecognized time zone
#> 'foo'
#> Warning in as.POSIXlt.POSIXct(x, tz): unknown timezone 'foo'
#> [1] "2116-11-18 19:52:58 GMT" "0571-11-09 04:26:52 GMT"
#> [3] "0988-09-07 22:15:47 GMT"

Created on 2025-03-31 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions