Sys.setlocale:设置区域设置的请求...无法满足
我在我的包的函数中使用 strptime(...)
。我需要使用特定的本地设置解析字符串,并使用 Sys.setlocale 作为获取英语本地化设置的解决方法。为了减少副作用,之后会恢复之前的本地设置。 该函数的基本代码片段如下所示:
#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)
不幸的是,我的一位同事在使用此函数时收到以下警告:
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,因为它允许以非常灵活的方式解析日期时间字符串。
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我非常确定您大学的计算机上没有安装“en_GB.UTF-8”语言环境。最简单的方法可能是安装它:) 嗯,这对于每个操作系统来说都不是小事。
其他选项可能是使用每台计算机上都可以找到的标准区域设置。由于您添加的示例没有显示特殊格式,您可以尝试将
LC_TIME
设置为C
,这也适用于 Linux 和 Windows。通过该区域设置,您给出的示例将非常有效。请参阅:否则您应该转换您的数据 - 例如:编写一个简短的函数将所有周和月的名称替换为标准字符串,并将导入的字符串重新构造为标准字符串。
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.
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
toC
, which works under Linux and Windows also. With that locale, your given example will work like a charm. See: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.
我在我的 Windows 机器上尝试了你的代码并得到了同样的错误。作为参考,Sys.getlocale("LC_TIME") 的结果:
我怀疑这可能是一个相当标准的语言环境。
但我也怀疑解决这个问题的更好方法是使用 lubridate 包中的一些函数,这使得处理日期变得很容易。
您在问题中没有提供足够的详细信息,您想要做什么,但我猜测“sometext”采用特定的预期格式,例如 DMY 或 YMD。 Lubridate 提供了解析任何指定格式的日期的函数,例如 dmy()、ymd()、mdy() - 您明白了。
如果您提供有关实际问题的更多详细信息,我们也许能够提供更具体的帮助。
I have tried your code on my Windows machine and get the same error. For reference, the results of Sys.getlocale("LC_TIME"):
I suspect this might be a fairly standard locale.
But I also suspect that the better way of approaching this problem is to use some of the functions in package lubridate, which makes it easy to work with dates.
You don't give enough details in your question what you are tring to do, but I am guessing that "sometext" is in a specific expected format, such as DMY or YMD. Lubridate provides functions to parse dates in any specified format, e.g. dmy(), ymd(), mdy() - you get the picture.
If you provide more details about your real problem, we might be able to help more specifically.