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"