Get the time zone based on information about the country of the locale (OS independent)

Another date / time related issue; -)

Before aiming and shooting

Things are spoiled by a combination of Germany + MS Windows + R , since the following leads to an invalid time zone:

> Sys.timezone() [1] "MST" Warning message: In as.POSIXlt.POSIXct(Sys.time()) : unknown timezone 'MET-1MST' 

This is definitely not an R error, it is Windows. Hence, first of all, the question :-)

Question

Is there a simple / alternative and OS-independent way to request your current country through locale information and then view the appropriate time zone (format "<country>/<city>" , for example, "Europe/Berlin" for Germany)?

I should also add that I would like the solution to be independent of Internet resources, as indicated in this post / response .

Problem context

Suppose you don’t know how to specify your time zone. You may have heard something about CET / CEST , etc., but AFAIK, which actually does not give you the opportunity to use the basic functionality of R (at least, being in Germany ;-)).

You can get a list of available "<country>/<city>" pairs from the /share/zoneinfo/zone.tab file in the /share/zoneinfo/zone.tab directory. However, to find the time zone that corresponds to the current country in which you are located, you need to know the ISO country code.

Of course, we usually do for our own country, but suppose we do not (I would like to get a common approach). What will you do next?

Below is my “four-step” solution, but I'm not very happy because

  • he relies on another Contrib package ( ISOcodes )
  • I can’t check if it works for other locales, because I don’t know what the information will look like if you are in India, Russia, Australia, etc.

Has anyone got a better idea? It would also be great if some of you in countries other than Germany could do this and post your Sys.getlocale() locale Sys.getlocale() .

Step 1: Get Locale Information

 loc <- strsplit(unlist(strsplit(Sys.getlocale(), split=";")), split="=") foo <- function(x) { out <- list(x[2]) names(out) <- x[1] out } loc <- sapply(loc, foo) > loc $LC_COLLATE [1] "German_Germany.1252" $LC_CTYPE [1] "German_Germany.1252" $LC_MONETARY [1] "German_Germany.1252" $LC_NUMERIC [1] "C" $LC_TIME [1] "German_Germany.1252" 

Step 2: get the name of the country from the locale information

 country.this <- unlist(strsplit(loc$LC_TIME, split="_|\\."))[2] > country.this [1] "Germany" 

Step 3: Get the ISO Country Code

Use country.this to find the associated country code in the ISO_3166_1 dataset ISO_3166_1

 require("ISOcodes") data("ISO_3166_1") iso <- ISO_3166_1 idx <- which(iso$Name %in% country.this) code <- iso[idx, "Alpha_2"] > code [1] "DE" 

Step 4: get the time zone

Use code to find the time zone in a data frame that can be obtained from the RHOME/share/zoneinfo/zone.tab file

 path <- file.path(Sys.getenv("R_HOME"), "share/zoneinfo/zone.tab") tzones <- read.delim( path, row.names=NULL, header=FALSE, col.names=c("country", "coords", "name", "comments"), as.is=TRUE, fill=TRUE, comment.char = "#" ) > tzones[which(tzones$country == code), "name"] [4] "Europe/Berlin" 
+4
source share
2 answers

In particular, regarding your question:

Is there a simple / alternative and OS-independent way to query your current country through local information, and then search for the appropriate time zone?

No no. This is due to the fact that there are several countries with several time zones. It is not possible to find the time zone from only one country.

This is why TZDB identifiers are of the form Area/Location , and not just a list of country codes.

+2
source

Some simplifications to your workflow.

You can only get the temporary part of the locale using

 Sys.getlocale("LC_TIME") 

which avoids dividing lines into lines.

The lubridate package contains a function for retrieving Olson-style time zone names, so you don’t have to worry about reading and analyzing zone.tab .

 library(lubridate) olson_time_zones() 
0
source

All Articles