Sys.setlocale:请求设置区域设置...不能被兑现 [英] Sys.setlocale: request to set locale ... cannot be honored

查看:1806
本文介绍了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,因为它允许非常灵活的解析datetime字符串的方式。

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天全站免登陆