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

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

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"

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.

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'

Related

How to change default sys.time options?

This is probably a very basic question but since I did not deal with dates and times in R, I didn't know how to solve it. The problem is when I open a new R session and run sys.time() it gives me wrong time. I found a solution to that Sys.setenv(TZ="TURKEY") and changed it. However, when I close R and open it again. It goes back to wrong result again. How do I make this permanent?
> Sys.time()
[1] "2022-04-01 11:16:37 GMT"
Warning messages:
1: In normalizePath(dirname(f)) : path[1]="NA": No such file or directory
2: In as.POSIXlt.POSIXct(x, tz) :
unknown timezone 'zone/tz/2022a.1.0/zoneinfo/Europe/Istanbul'
> Sys.setenv(TZ="TURKEY")
> Sys.time()
[1] "2022-04-01 14:17:39 EEST"
You can set TZ="TURKEY" in your .Renvironment file. This will load each time you (re)start R, and thus you will always get the timezone you want.
.Renvironment files can be created for each R project, or globally for all your projects. You can see this answer for how to create a global .Renvironment file .

Sys.Date() showing the wrong date

I do not know what happened but am getting irritated with this. I am currently in the GMT-8 Time Zone. When I type Sys.Date() it returns the tomorrow's date.
As of now the current date & time is 12/7/17 10:41 PM:
I type Sys.time() and this is what i get:
Sys.time()
[1] "2017-12-08 14:37:22 GMT"
I then try to setup my time zone: Sys.setenv(TZ=Sys.timezone())
It clears without errors... and then I type Sys.Date() again and get
Sys.Date()
[1] "2017-12-08"
It should be 12/7/17 !!
Any help is appreciated.
On my mac system, I saw this with R 3.4.1 after updating to Mac OS 10.13.2:
Sys.time()
[1] "2017-12-08 20:52:01 GMT"
Warning:
In as.POSIXlt.POSIXct(x, tz) :
unknown timezone 'zone/tz/2017c.1.0/zoneinfo/Europe/Berlin'
I could fix that by setting Sys.setenv(TZ = "Europe/Berlin"). Valid timezone names for the US can be found using grep('^US/', OlsonNames(), value = TRUE).
After upgrading to R 3.4.3 I see this:
Sys.time()
[1] "2017-12-08 21:57:15 CET"
Sys.timezone()
#[1] "Europe/Berlin"
So this looks like an issue due to an OS update. In fact, the release notes for R 3.4.3 say this:
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).

Changing String to Date and Time in R

I have a vector of character class strings.
I want to covert this to Sys.time() format. How can I do that?
When I use the as.POSIXct(z,format= "%d/%m/%Y %H:%M:%S")
I get the following warning:
Warning message:
In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
I don't want this to convert to NAs. What should I do?
Kindly use
strptime("2014-06-05 15:33:55 IST","%Y-%m-%d %H:%M:%S",tz="Asia/Kolkata")
For your problem
data$time <- strptime(data$time, "%Y-%m-%d %H:%M:%S",tz="Asia/Kolkata")
class(data$time)
It should be of type POSIXlt
I know this is an old question but for the benefit of those Googling this problem, I had a similar problem recently when using rbind.fill. I discovered that one of my dates was in European format, 31/1/2017 instead of 1/31/2017 like all the others. It was causing R to blank out the entire column after performing rbind.fill. Fixing the date fixed the issue.
Hope this helps someone, I spent hours trying to figure out why this was happening. R is really picky sometimes.

lubridate error message 'incompatible method's when trying date arithmetic

I'm trying to learn lubridate. The lubridate documentation shows:
date <- ceiling_date(date, "month") - days(1)
[1] "2010-05-31 UTC
for date arithmetic. But if I try
mytoday <- now()
first_of_month <- floor_date(mytoday, "month")
first_of_month_last_year <- first_of_month - years(1)
to use date arithmetic to get the first of the month a year earlier I get an error message
Warning message:
Incompatible methods ("-.POSIXt", "Ops.ordered") for "-"
Any ideas what I'm doing wrong? Thanks.
Ran into the same problem.
In my case it was that I had two date/time related packages installed (chron and lubridate), which appears to conflict with each other to some degree.
I solved this by removing chron as I wasn't really using it.
If you are using multiple date/time related packages, I would suggest removing these packages, then reinstalling them, with lubridate last. However, you may run into other conflicts.
Hope this helps.

Sys.setlocale: request to set locale ... cannot be honored

I'm using strptime(...) in a function of my package. I need to parse a string using specific local settings and used Sys.setlocale as a workaround to get english localization settings. To reduce side effects, the previous local setting is restored afterwards.
The basic code fragment of the function looks as follows:
#parameter settings
sometext <- "Mon, 14 Mar 2011 23:42:16 GMT"
timeFormat <- "%a, %d %b %Y %H:%M:%S"
timeZone <- "GMT"
#get current locale
loc <- Sys.getlocale("LC_TIME")
#set british localization
dummy <- Sys.setlocale("LC_TIME", "en_GB.UTF-8")
#parse datetime string
time <- strptime(sometext, format = timeFormat, tz= timeZone)
#set local back
dummy <- Sys.setlocale("LC_TIME", loc)
Unfortunately, a colleague of mine gets the following warning when using this function:
In Sys.setlocale("LC_TIME", "en_GB.UTF-8") :
OS reports request to set locale to "en_GB.UTF-8" cannot be honored
On my computer everything works fine.
Is there a better (and independent from installed R localization) way of performing this task? Generally I would like to use strptime as it allows a very flexible way of parsing datetime strings.
I am quite sure that the "en_GB.UTF-8" locale is not installed on your college's computer. The easiest way could be to install it :) Well, this is not trivial with every OS.
Other option could be to use a standard locale which can be found on every computer. As your added example shows no special format, you could try with setting LC_TIME to C, which works under Linux and Windows also. With that locale, your given example will work like a charm. See:
> Sys.setlocale("LC_TIME", "C")
> strptime("Mon, 14 Mar 2011 23:42:16 GMT", format = "%a, %d %b %Y %H:%M:%S", tz="GMT")
[1] "2011-03-14 23:42:16 GMT"
Or otherwise you should transform your data - e.g.: write a short function to substitute all week- and months' name to standard strings and restructure your imported strings to standard ones.
I have tried your code on my Windows machine and get the same error. For reference, the results of Sys.getlocale("LC_TIME"):
> Sys.getlocale("LC_TIME")
[1] "English_United Kingdom.1252"
I suspect this might be a fairly standard locale.
But I also suspect that the better way of approaching this problem is to use some of the functions in package lubridate, which makes it easy to work with dates.
You don't give enough details in your question what you are tring to do, but I am guessing that "sometext" is in a specific expected format, such as DMY or YMD. Lubridate provides functions to parse dates in any specified format, e.g. dmy(), ymd(), mdy() - you get the picture.
If you provide more details about your real problem, we might be able to help more specifically.

Resources