Sys.setlocale的异常行为 [英] Unexpected behavior of Sys.setlocale

查看:142
本文介绍了Sys.setlocale的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见下面的代码,我必须更改语言环境才能转换日期.我的第一次尝试没有成功,但是我的第二次尝试仍然有效,尽管它看起来很多余并且不会更改Sys.getlocale的输出.

Please see the code below, I have to change my locale to be able to convert a date. My first attempt is unsuccessful, my second attempt works, though it seems redundant and doesn't change the output of Sys.getlocale.

我的操作系统是Windows 7 64位

My OS is Windows 7 64-bit

Sys.getlocale() # "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")     # NA
Sys.setlocale(locale = "UK")        # "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"
locale2 <- Sys.getlocale()
as.Date(date, format = "%b-%d")     # NA
Sys.setlocale("LC_TIME", "English_United Kingdom")
locale3 <- Sys.getlocale()          # "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")     # "2017-12-11"
locale2 == locale3                  # TRUE

我可以跳过对Sys.getlocale的第一个呼叫,日期转换将起作用:

I can skip the first call to Sys.getlocale and the date conversion will work:

Sys.getlocale() # "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")     # NA
Sys.setlocale("LC_TIME", "English_United Kingdom") # 
locale4 <- Sys.getlocale()          # "LC_COLLATE=French_Belgium.1252;LC_CTYPE=French_Belgium.1252;LC_MONETARY=French_Belgium.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"
as.Date(date, format = "%b-%d")     # "2017-12-11"

但这不起作用:

Sys.getlocale() # "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")     # NA
Sys.setlocale(locale = "English_United Kingdom") #
locale5 <- Sys.getlocale()          # "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")     # NA

这与以下问题有关:推荐答案

按照这是Windows中的预期行为.在其他系统上,用于格式化strptime()的基础函数使用操作系统特定的strptime函数,但Windows没有该函数.因此,对于非英语的日期或月份名称,R使用替代函数.由于您具有法语的标准语言环境,因此您的R设置为可以识别法语的日期和月份的名称/缩写.

This is expected behaviour in Windows. On other systems, the underlying function for formatting strptime() uses the OS specific strptime function, but Windows doesn't have one. So R uses a substitute function in the case of non-english day or month names. As you have your standard locale on French, your R is set up to recognize french day and month names/abbreviations.

此strptime的替代功能使用其自己的那些日期和月份名称的映射,但是仅当专门设置"LC_TIME"时,此映射才被刷新.至少在使用相同机制的R 3.4.0和更早版本中就是这种情况.

This substitue function for strptime uses its own mapping of those day and month names, but this mapping is refreshed ONLY when "LC_TIME" is set specifically. At least this is the case for R 3.4.0 and earlier versions using the same mechanism.

所以与我的第一印象相反,这不是错误,而是功能:-)

So contrary to my first impression, this is not a bug but a feature :-)

这篇关于Sys.setlocale的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆