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.
POSIXct stopped working and cannot update timezone
In your last line, you have
as.POSIXct("10/1/2019", "%m/%d/%Y")
The arguments to as.POSIXct
are (x, tz, ...)
. To specify a format, you need to name it, i.e.
df[as.POSIXct(strptime(df$dates, "%m/%d/%Y")) <
as.POSIXct("10/1/2019", format = "%m/%d/%Y"), ]
or even better,
df[as.POSIXct(df$dates, format = "%m/%d/%Y") <
as.POSIXct("10/1/2019", format = "%m/%d/%Y"), ]
Understanding timezone strings in R
CEST probably stands for Central European Summer Time.
So during daylight saving time, CET becomes CEST, and in winter it does not:
as.POSIXct(c("2016-1-1 13:00", "2016-3-1 13:00",
"2016-5-1 13:00", "2016-6-1 13:00",
"2016-9-1 13:00","2016-11-1 13:00"), tz="CET")
returns:
"2016-01-01 13:00:00 CET" "2016-03-01 13:00:00 CET" "2016-05-01 13:00:00 CEST"
"2016-06-01 13:00:00 CEST" "2016-09-01 13:00:00 CEST" "2016-11-01 13:00:00 CET"
However, as @Matt_Johnson explained, CEST is not on official timezone,
so as.POSIXct("2016-1-1 13:00, tz="CEST")
fails.
What does remain strange, that CEST is acceptable in a string, even if the time is outside daylight saving time:
as.POSIXct("2016-1-1 13:00 CEST")
[1] "2016-01-01 13:00:00 CET"
The help files from as.POSIXct
and strptime
don't offer any explaination here.
Determine and set timezone in POSIXct, POSIXlt, strptime, etc. in R
If you do not use a timezone specifically, POSIXct and POSIXlt will reference to your local timezone. However, this is not entirely reliable. POSIXlt will not display the timezone in the output string.
Note, the tzone argument is not set.
t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z")
t.ct
t.lt
attr(t.ct,"tzone") #""
attr(t.lt,"tzone") #NULL
If you do want to avoid ambiguous behaviour, you have to specifiy a time zone. The output string will still be different (by default POSIXlt shows no timezone), but the attribute is the same
t.ct <- as.POSIXct("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki")
t.lt <- as.POSIXlt("2009-01-05 14:19 +1200", format="%Y-%m-%d %H:%M %z", tz="Europe/Helsinki")
t.ct
t.lt
attr(t.ct,"tzone") #Europe/Helsinki
attr(t.lt,"tzone") #Europe/Helsinki
Now, if you want to change time zones after the original assignment:
attr(t.ct, "tzone") <- "UTC" #this will SHIFT the time zone to UTC
attr(t.lt, "tzone") <- "UTC" #this will REPLACE the time zone to UTC
t.ct
t.lt
As for your problem with strftime
and %z
, this does not give you the time zone attribute. The difference in your case, probably comes from a combination of string formatting, object conversions and time zone formating, IMO. But maybe somebody more knowledgable, can clarify this.
How to extract the correct timezones from POSIXct and POSIXlt objects?
@Koshke showed you already
- the difference in internal representation of both date types, and
- that internally, both timezone specifications are the same.
You can get the timezone out in a standardized manner using attr()
. This will get the timezone in the form specified in the zone.tab
file, which is used by R to define the timezones (More info in ?timezones
).
eg :
> attr(time1,"tzone")
[1] "Europe/London"
> attr(time2,"tzone")
[1] "Europe/London"
I am quite amazed though that POSIXct uses different indications for the timezones than POSIXlt, whereas the attributes are equal. Apparently, this "BST" only pops up when the POSIXct is printed. Before it gets printed, POSIXct gets converted again to POSIXlt, and the tzone attribute gets amended with synonyms :
> attr(as.POSIXlt(time2),"tzone")
[1] "Europe/london" "GMT" "BST"
This happens somewhere downstream of the internal R function as.POSIXlt
, which I'm not able to look at for the moment due to more acute problems to solve. But feel free to go through it and see what exactly is going on there.
On a sidenote, "BST" is not recognized as a timezone (and it is not mentioned in zone.tab either) on my Windows 7 / R 2.13.0 install.
Related Topics
Two Horizontal Bar Charts with Shared Axis in Ggplot2 (Similar to Population Pyramid)
Dplyr Summarise Multiple Columns Using T.Test
R - Ggplot2 - Highlighting Selected Points and Strange Behavior
Knitr: Include Figures in Report *And* Output Figures to Separate Files
Looping Through List of Data Frames in R
Passing Parameters to R Markdown
Use an Image as Area Fill in an R Plot
Assigning Null to a List Element in R
Remove 'Search' Option But Leave 'Search Columns' Option
Sum of Two Columns of Data Frame with Na Values
Coloring Boxplot Outlier Points in Ggplot2
Create Barplot from Data.Frame
Collapse All Columns by an Id Column
How to Create a Bar Plot for Two Variables Mirrored Across the X-Axis in R
Add a New Column Between Other Dataframe Columns