Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

datetime - R error: unknown timezone with as.POSIXct()

I am trying to convert a unix epoch timestamps to a date-time object using as.POSIXct()

I need to specify timezones (either Europe/London or UTC) when I call as.POSIXct().

If I run

> t<-as.POSIXct(1445329330, tz="Europe/London", origin="1970-01-01")
> t

R returns "2015-10-20 09:22:10 BST" Warning messages: 1: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/London' 2: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/London'

I have tried specifying tz="BST", but this also returns warnings

Warning messages:
1: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'BST
          '
2: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/London'
3: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'BST
          '
4: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/London'

I have looked up the zoneinfo/zone.tab as per Joshua Ulrich's post and "Europe/London" does appear in the zone.tab file, while "BST" does not. So I think that Europe/London should be a valid tz option. Is this correct?

Does anyone have suggestions as to why I am getting warnings, and why the specified timezone is not being assigned to the as.POSIXct object?

It should be noted that my scripts which call as.POSIXct() were running without warnings prior to updating MacOS to High Sierra. Could the OS update lead to these warnings? When I run Sys.timezone() it returns NA

Many thanks in advance

Iris

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am having a similar issue on macOS High Sierra 10.13.1. As soon as, I try to do anything with dates, I get the following error.

> as.POSIXct("2017-10-01", format = "%Y-%m-%d")
[1] "2017-10-01 GMT"
Warning message:
In strptime(x, format, tz = tz) :
  unknown timezone 'zone/tz/2017c.1.0/zoneinfo/Pacific/Auckland'

The warning goes away if I set the environment variable to my timezone and I get the date back with the correct timezone.

> Sys.setenv(TZ = "Pacific/Auckland")
> as.POSIXct("2017-10-01")
[1] "2017-10-01 NZDT"

So, I have been setting the environment variable every time I need to do something to do with dates.

However, I have found this link talking about the same thing. Peter Dalgaard from R Core Team has replied saying this was a bug in macOS 10.13 Beta and that it is up to Apple to sort it out.

I am thinking to put Sys.setenv(TZ = "Pacific/Auckland") into .Rprofile so that it sets the time zone every time I start RStudio. I hope this helps.

Here is a link that might be useful if you want to try the .Rprofile approach I mentioned.

Update: It seems like this has been resolved in R 3.4.3. You can read more about it in the R news. Below is the related part of the release notes.

INSTALLATION on a UNIX-ALIKE

A workaround has been added for the changes in location of time-zone files in macOS 10.13 'High Sierra' and again in 10.13.1, so the default time zone is deduced correctly from the system setting when R is configured with --with-internal-tzcode (the default on macOS).

I can confirm that the new version of R resolves the issues with date/time objects.

> Sys.timezone()
[1] "Pacific/Auckland"
> Sys.time()
[1] "2017-12-30 16:22:32 NZDT"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...