可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
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"
回答3:
Looking at the output it is not actually giving a warning about 'Europe/London', just about other variations ('BST' and 'default/Europe/London').
Could it be that it are errors of previous commands still lingering? Do you get the same if run you the as.POSIXct(1445329330, tz="Europe/London", origin="1970-01-01")
again, or even restart R?
I still get the error about BST even if using the correct timezone
> as.POSIXct(1445329330, tz="BST", origin="1970-01-01") [1] "2015-10-20 08:22:10 GMT" Warning message: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'BST' > as.POSIXct(1445329330, tz="Europe/London", origin="1970-01-01") [1] "2015-10-20 09:22:10 BST" Warning message: In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'BST'