bash 强制“日期”使用机器默认值以外的语言环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12896509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:50:08  来源:igfitidea点击:

Forcing "date" to use a locale other than the machine default

bashunixdate

提问by John Doucette

Is there a way to force the *nix command "date" to output in a specific format independent of the local? For example, if I call "date -u" today, on a US machine I get:

有没有办法强制 *nix 命令“date”以独立于本地的特定格式输出?例如,如果我今天调用“date -u”,在美国机器上我得到:

Mon Oct 15 13:15:29 UTC 2012

but on a German machine I get:

但在德国机器上我得到:

Mo 15. Okt 13:15:31 UTC 2012

回答by January

Sure, you can always specify the format yourself:

当然,您始终可以自己指定格式:

date +%a, %b %d

or you can use a temporary environmental variable:

或者您可以使用临时环境变量:

:~$ LC_ALL=de_DE.utf8 date
Mo 15. Okt 15:34:11 CEST 2012
:~$ date
Mon Oct 15 15:33:24 CEST 2012

As you see, only the first command is run with the German locale.

如您所见,只有第一个命令使用德语语言环境运行。

回答by chepner

You can modify the environment dateruns in. I don't know if this is the best way (the variables used in locale handling are numerous), but the following works:

可以修改date运行中的环境。不知道这是否是最好的方法(语言环境处理中使用的变量很多),但以下方法有效:

$ LANG=de_DE date
Mo 15 Okt 2012 09:34:12 EDT

(January beat me to this answer by a minute or so, but I'll leave my answer up in the hopes that some will clarify which variable (LC_ALL, LANG, other) is most "appropriate".)

(一月打我这个答案用一分钟左右的时间,但我会离开我的答案了,希望会有一些会澄清变量(LC_ALLLANG,等)是最“合适”。)

回答by USD Matt

Have you tried the following? (which I got from the date manual pages)

您是否尝试过以下方法?(我从日期手册页中得到的)

# date -u +"%a %b %d %T %Z %Y"

回答by Serge Stroobandt

First, make sure that the language package of the target locale is installed. On Debian-derived GNU/Linux distributions, Dutch language support is installed as follows:

首先,确保安装了目标语言环境的语言包。在 Debian 派生的 GNU/Linux 发行版上,荷兰语支持的安装方式如下:

$ sudo apt-get install language-pack-nl

This command will automatically also install language-pack-nl-base. Here is what I get by specifying LC_TIME. This is more specific than using LC_ALL. (Specifying LANGdoes not work on Xubuntu LTS.)

此命令也将自动安装language-pack-nl-base. 这是我通过指定得到的LC_TIME。这比使用更具体LC_ALL。(指定LANG在 Xubuntu LTS 上不起作用。)

$ date '+%A %e %B %Y'
Friday 16 October 2015

$ LC_TIME='nl_BE.UTF-8' date '+%A %e %B %Y'
vrijdag 16 oktober 2015

回答by Weber K.

UTC:

世界标准时间:

date -u -R

Local:

当地的:

date -R

回答by elig

First, uncomment your desired locale(s) lines in /etc/locale.genthen run locale-gen(both as root). Make sure your terminal supports the required encoding so the characters will get shown.

首先,取消注释所需的语言环境行,/etc/locale.gen然后运行locale-gen(均以 root 身份运行)。确保您的终端支持所需的编码,以便显示字符。

You can then specify a "temporary" locale for the run-time of one command like this:

然后,您可以为一个命令的运行时指定一个“临时”语言环境,如下所示:

LC_ALL="de_DE.UTF-8" date

回答by qin

LC_TIMEis enough, you can use this.

LC_TIME够了,你可以用这个。

LC_TIME="de_DE.UTF-8"  date