[Rd] LC_TIME not set correctly by Sys.setlocale() ?

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 23 17:25:52 CEST 2017


On 23/06/2017 15:35, Joris Meys wrote:
> Related to the following question on Stackoverflow:
> https://stackoverflow.com/questions/44723690/unexpected-behavior-of-sys-setlocale#44723690
> 
> It appears as if Sys.setlocale() does not update LC_TIME correctly for use
> in date formatting. Although R reports that LC_TIME is changed to the new
> setting after use of Sys.setlocale(), as.Date() still uses the old
> settings. The only way to update this is by specifically using LC_TIME.
> 
> Is this a bug or am I overlooking something?

Try setting the LC_TIME category explicitly.  The mapping of day/month 
names used by strptime (not really as.Date, and not taken from LC_TIME) 
is then reset.

Since Windows does not have a usable strptime C function, a substitute 
is used and its handing of non-English names is not done through the 
OS's locale mechanism.

> Example:
> 
>> Sys.setlocale(locale = "French_Belgium")
> [1]
> "LC_COLLATE=French_Belgium.1252;LC_CTYPE=French_Belgium.1252;LC_MONETARY=French_Belgium.1252;LC_NUMERIC=C;LC_TIME=French_Belgium.1252"
> 
>> date <- "Dec-11"
> 
>> as.Date(date, format = "%b-%d")
> [1] NA # expected
> 
>> Sys.setlocale(locale = "English_United Kingdom")
> [1] "LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
> Kingdom.1252;LC_MONETARY=English_United
> Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"
> 
>> as.Date(date, format = "%b-%d")
> [1] NA # not expected, should be a correct date
> 
>> Sys.setlocale("LC_TIME", "English_United Kingdom")
> [1] "English_United Kingdom.1252"
> 
>> as.Date(date, format = "%b-%d")
> [1] "2017-12-11" # expected
> 
>> Sys.setlocale(locale = "French_Belgium")
> [1]
> "LC_COLLATE=French_Belgium.1252;LC_CTYPE=French_Belgium.1252;LC_MONETARY=French_Belgium.1252;LC_NUMERIC=C;LC_TIME=French_Belgium.1252"
> 
>> as.Date(date, format = "%b-%d")
> [1] "2017-12-11" # not expected, should be NA
> 
>> Sys.setlocale("LC_TIME", "French_Belgium")
> [1] "French_Belgium.1252"
> 
>> as.Date(date, format = "%b-%d")
> [1] NA # expected
> 
>> sessionInfo()
> R version 3.4.0 (2017-04-21)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 7 x64 (build 7601) Service Pack 1
> 
> Matrix products: default
> 
> locale:
> [1] LC_COLLATE=French_Belgium.1252  LC_CTYPE=French_Belgium.1252
> [3] LC_MONETARY=French_Belgium.1252 LC_NUMERIC=C
> [5] LC_TIME=French_Belgium.1252
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> other attached packages:
> [1] shiny_1.0.3
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.4.0  R6_2.2.1        htmltools_0.3.6 tools_3.4.0
> Rcpp_0.12.10
> [6] digest_0.6.12   xtable_1.8-2    httpuv_1.3.3    mime_0.5
> 
> 
> 


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Emeritus Professor of Applied Statistics, University of Oxford



More information about the R-devel mailing list