R Error: Unknown Timezone with As.Posixct()

unknown timezone name in R strptime/as.POSIXct

?Sys.timezone has some hints, specifically to look in: "R_HOME/share/zoneinfo/zone.tab" (R_HOME is the directory R is installed in). Keep in mind that time zones are nasty and many of their nuances are operating system (and locale?) specific.

In your specific case, you want "CST6CDT" instead of "CST".

R error: unknown timezone with as.POSIXct()

It does look like you need to update your system with a timezone, even though it isn't being used.

I can't seem to set my timezone to NA, but if I set my environment with, for example Sys.setenv(TZ='Twilight Zone'), or anything that isn't on the tz list I also get the same errors that you do.

R lubridate: Why do I get a unknown timezone error?

As suggested by Matt Gibson and Roland,
the error has to do with running an older version of R.

I had installed R version 3.4.1 around August 2017,
when my computer was running macOS 10.12 Sierra.
Around November 2017, I had updated macOS to 10.13 High Sierra.
After I installed R version 3.4.3,
the unknown timezone error no longer appeared.

> library(lubridate)

Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

date

> mdy('15-01-2018')
[1] NA
Warning message:
All formats failed to parse. No formats found.

Of course, the format fails to parse,
because as Prem
and InfiniteFlashChess mentioned,
I had mixed up the month and date in the mdy function.

> mdy('01-15-2018')
[1] "2018-01-15"

Unexpected date when converting POSIXct date-time to Date - timezone issue?

Using the setup in the Note at the end we can use any of these:

# same date as print(x) shows
as.Date(as.character(x))
## [1] "2020-03-24"

# use the time zone stored in x (or system time zone if that is "")
as.Date(x, tz = attr(x, "tzone"))
## [1] "2020-03-24"

# use system time zone
as.Date(x, tz = "")
## [1] "2020-03-24"

# use system time zone
as.Date(x, tz = Sys.timezone())
## [1] "2020-03-24"

# use indicated time zone
as.Date(x, tz = "Asia/Calcutta")
## [1] "2020-03-24"

Note

We have assumed this setup.

Sys.setenv(TZ = "Asia/Calcutta")
x <- structure(1584988320, class = c("POSIXct", "POSIXt"), tzone = "")

R.version.string
## [1] "R version 4.0.2 Patched (2020-06-24 r78745)"

as.POSIXct gives an unexpected timezone

This is because as.POSIXct.Date doesn't pass ... to .POSIXct.

> as.POSIXct.Date
function (x, ...)
.POSIXct(unclass(x) * 86400)
<environment: namespace:base>

Timezone for as.POSIXct not working

If you look at the source code of as.POSIXct.Date you see this:

function (x, ...) 
.POSIXct(unclass(x) * 86400)
<bytecode: 0x00000000120de6e0>
<environment: namespace:base>

Note how no timezone is passed on to .POSIXct.

You can use the character method instead:

as.POSIXct(as.character(as.Date("2013-05-14")), tz = "GMT")
[1] "2013-05-14 GMT"


Related Topics



Leave a reply



Submit