Sys.setlocale:设置语言环境的请求......无法兑现 [英] Sys.setlocale: request to set locale ... cannot be honored

查看:84
本文介绍了Sys.setlocale:设置语言环境的请求......无法兑现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的包的一个函数中使用 strptime(...).我需要使用特定的本地设置解析字符串,并使用 Sys.setlocale 作为解决方法来获取英文本地化设置.为了减少副作用,之后会恢复之前的本地设置.该函数的基本代码片段如下所示:

I'm using strptime(...) in a function of my package. I need to parse a string using specific local settings and used Sys.setlocale as a workaround to get english localization settings. To reduce side effects, the previous local setting is restored afterwards. The basic code fragment of the function looks as follows:

#parameter settings
sometext <- "Mon, 14 Mar 2011 23:42:16 GMT"
timeFormat <- "%a, %d %b %Y %H:%M:%S"
timeZone <- "GMT"
#get current locale
loc <- Sys.getlocale("LC_TIME")
#set british localization
dummy <- Sys.setlocale("LC_TIME", "en_GB.UTF-8")
#parse datetime string
time <- strptime(sometext, format = timeFormat, tz= timeZone)
#set local back
dummy <- Sys.setlocale("LC_TIME", loc)

不幸的是,我的一位同事在使用此功能时收到以下警告:

Unfortunately, a colleague of mine gets the following warning when using this function:

In Sys.setlocale("LC_TIME", "en_GB.UTF-8") :
OS reports request to set locale to "en_GB.UTF-8" cannot be honored

在我的电脑上一切正常.是否有更好的(并且独立于已安装的 R 本地化)方法来执行此任务?一般来说,我喜欢使用 strptime,因为它可以非常灵活地解析日期时间字符串.

On my computer everything works fine. Is there a better (and independent from installed R localization) way of performing this task? Generally I would like to use strptime as it allows a very flexible way of parsing datetime strings.

推荐答案

我很确定你们大学的计算机上没有安装en_GB.UTF-8"语言环境.最简单的方法可能是安装它 :) 嗯,这对每个操作系统来说都不是小事.

I am quite sure that the "en_GB.UTF-8" locale is not installed on your college's computer. The easiest way could be to install it :) Well, this is not trivial with every OS.

其他选项可能是使用可以在每台计算机上找到的标准语言环境.由于您添加的示例没有显示特殊格式,您可以尝试将 LC_TIME 设置为 C,这也适用于 Linux 和 Windows.使用该语言环境,您给定的示例将像魅力一样发挥作用.见:

Other option could be to use a standard locale which can be found on every computer. As your added example shows no special format, you could try with setting LC_TIME to C, which works under Linux and Windows also. With that locale, your given example will work like a charm. See:

> Sys.setlocale("LC_TIME", "C")
> strptime("Mon, 14 Mar 2011 23:42:16 GMT", format = "%a, %d %b %Y %H:%M:%S", tz="GMT")
[1] "2011-03-14 23:42:16 GMT"

否则您应该转换您的数据 - 例如:编写一个简短的函数,将所有周和月的名称替换为标准字符串,并将您导入的字符串重组为标准字符串.

Or otherwise you should transform your data - e.g.: write a short function to substitute all week- and months' name to standard strings and restructure your imported strings to standard ones.

这篇关于Sys.setlocale:设置语言环境的请求......无法兑现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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