How do I fix my timezone issues on my R Shiny App? Unrecognized time zone 'GMT+5'

38 Views Asked by At

I have an R Shiny application that is giving me many timezone errors that cause the app to be delayed on startup. The errors say:

Warning in strptime(.enclose(x), .enclose(fmt), tz) : unknown timezone 'GMT+5'
Warning in as.POSIXct.POSIXlt(x) : unknown timezone 'GMT+5'
Warning in as.POSIXct.POSIXlt(x) : unknown timezone 'GMT+5'
Warning in as.POSIXct.POSIXlt(x) : unknown timezone 'GMT+5'
Warning in as.POSIXlt.POSIXct(x, tz) : unknown timezone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in as.POSIXlt.POSIXct(x, tz) : unknown timezone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in as.POSIXlt.POSIXct(x, tz) : unknown timezone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in strptime(.enclose(x), .enclose(fmt), tz) : unknown timezone 'GMT+5'....repeat

I also deploy this application to the web where it also throws these errors:

Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'
Warning in with_tz.default(x, tz) : Unrecognized time zone 'GMT+5'...repeat

Here is a snippet of my code, where I set the timezone of the data like this:

 data$x <- parse_date_time(data$x, orders = c("dmy HM", "dmy H", "dmY HM", "dmY"), tz="GMT+5")
 data2$x <- parse_date_time(data2$x, orders = c("dmy HM", "dmy H", "dmY HM", "dmY"), tz="GMT+5")

I'm using packages shinydashboard, dygraphs, readr, xts, lubridate, tidyr, dplyr on a Windows PC.

2

There are 2 best solutions below

3
r2evans On

The correct timezone is "Etc/GMT+5", noting the prepended Etc/.

lubridate::with_tz(Sys.time(), tz = "GMT+5")
# Warning in with_tz.default(Sys.time(), tz = "GMT+5") :
#   Unrecognized time zone 'GMT+5'
# [1] "2024-03-21 18:03:07 GMT"
lubridate::with_tz(Sys.time(), tz = "Etc/GMT+5")
# [1] "2024-03-21 18:03:50 -05"
2
Matt Johnson-Pint On

The tz parameter passed to parse_date_time should follow the guidance for usage of time zones in R.

Specifically, you should be using IANA time zone identifiers (aka "Olson" time zones). You can find a convenient list of them here.

Note that by default on Windows, R will use its own copy of the time zone database, at the location specified by file.path(R.home("share"), "zoneinfo"). This is covered in the R documentation linked above.

With specific regard to a fixed offset of GMT+5, you could specify "Etc/GMT-5". Note that the sign is inverted from the usual convention (ie., a - indicates the local time is ahead of GMT). However, such fixed offsets are usually reserved for edge cases such as onboard time for ships at sea.

Instead, you should choose a location-based time zone identifier from the list. For example, "Asia/Almaty". Location-based identifiers are preferred because many time zones alternate between multiple offsets for daylight saving time, and they are all subject to potential changes over time. Using a location-based time zone identifier will ensure that your application can keep up with changes as time zone data is updated on your system.