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? 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 -- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Mathematical Modelling, Statistics and Bio-Informatics tel : +32 (0)9 264 61 79 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
Prof Brian Ripley
2017-Jun-23 15:25 UTC
[Rd] LC_TIME not set correctly by Sys.setlocale() ?
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
Dear prof. Ripley, thank you for your answer. A few additional questions below. On Fri, Jun 23, 2017 at 5:25 PM, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:> On 23/06/2017 15:35, Joris Meys wrote: > >> Related to the following question on Stackoverflow: >> https://stackoverflow.com/questions/44723690/unexpected-beha >> vior-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. >I have done so in the example, and I am aware that the underlying function is strptime. I just wondered why setting LC_ALL did not refresh the mapping, as LC_ALL -afaik- also updates LC_TIME to a new locale> > 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.This explains the behaviour. Is there a reason why this substitute is only updating the mapping of day/month names when LC_TIME is set explicitly and not when LC_ALL (and hence also LC_TIME) is set? Regards Joris -- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Mathematical Modelling, Statistics and Bio-Informatics tel : +32 (0)9 264 61 79 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
Seemingly Similar Threads
- LC_TIME not set correctly by Sys.setlocale() ?
- cygwin1.dll problems when installing packages from source
- cygwin1.dll problems when installing packages from source
- cygwin1.dll problems when installing packages from source
- cygwin1.dll problems when installing packages from source