[R] strptime() keeps emitting warnings after establishing a handler with tryCatch()

David Kepplinger d@v|d@kepp||nger @end|ng |rom gm@||@com
Sun Nov 1 00:55:42 CET 2020


Dear list members,

I have come about a peculiar behavior in R (4.0.2) which I would
describe as a bug.
On macOS, where `strptime()` raises a warning for invalid timezone
identifiers, the following code will continue to raise the original
warning with every subsequent call to `strptime()`:

```
# attach a handler for warnings for this call only:
tryCatch(strptime('2020-10-31 18:30', format = '%Y-%m-%d %H:%M', tz =
'Wrong Timezone!'),
                  warning = function (w) { stop("Error") })
# but every subsequent call will emit the original warning ("unknown
timezone 'Wrong Timezone!'")
strptime('2020-10-31 18:30', format = '%Y-%m-%d %H:%M', tz = 'Europe/Vienna')
strptime('2020-10-31 18:30', format = '%Y-%m-%d %H:%M', tz = 'GMT')
```

The output of the code above in R 4.0.2 on macOS is:

> tryCatch(strptime('2020-10-31 18:30', format = '%Y-%m-%d %H:%M', tz = 'Wrong Timezone!'),
+                   warning = function (w) { stop("Error") })
Error in value[[3L]](cond) : Error
>
> strptime('2020-10-31 18:30', format = '%Y-%m-%d %H:%M', tz = 'Europe/Vienna')
[1] "2020-10-31 18:30:00 CET"
Warning message:
In strptime("2020-10-31 18:30", format = "%Y-%m-%d %H:%M", tz =
"Europe/Vienna") :
  unknown timezone 'Wrong Timezone!'
> strptime('2020-10-31 18:30', format = '%Y-%m-%d %H:%M', tz = 'GMT')
[1] "2020-10-31 18:30:00 GMT"
Warning messages:
1: In strptime("2020-10-31 18:30", format = "%Y-%m-%d %H:%M", tz = "GMT") :
  unknown timezone 'Wrong Timezone!'

The corresponding R session info is:

> sessionInfo()
R version 4.0.2 Patched (2020-07-13 r78838)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_4.0.2


I get the same odd behavior when attaching calling handlers with
`withCallingHandlers()`.
On RHEL 7 and Cent OS 6, which both don't issue warnings for invalid
timezones, the above code works.

I don't see anything wrong with the code itself, but maybe I am
missing something. Any input would be appreciated.

Best wishes,
David



More information about the R-help mailing list