bash OS X 上的 `date` 命令没有 ISO 8601 `-I` 选项吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7216358/
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 20:58:40  来源:igfitidea点击:

`date` command on OS X doesn't have ISO 8601 `-I` option?

bashmacosiso8601

提问by Aseem Kishore

In a Bash script, I want to print the current datetime in ISO 8601format (preferably UTC), and it seems that this should be as simple as date -I:

在 Bash 脚本中,我想以ISO 8601格式(最好是 UTC)打印当前日期时间,看起来这应该很简单date -I

http://ss64.com/bash/date.html

http://ss64.com/bash/date.html

But this doesn't seem to work on my Mac:

但这似乎不适用于我的 Mac:

$ date -I
date: illegal option -- I
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

And indeed, man datedoesn't list this option.

事实上,man date没有列出这个选项。

Anyone know why this is, or any other (easy) way for me to print the date in ISO 8601 format? Thanks!

任何人都知道这是为什么,或任何其他(简单)方法让我以 ISO 8601 格式打印日期?谢谢!

回答by amit_g

You could use

你可以用

date "+%Y-%m-%d"

Or for a fully ISO-8601 compliant date, use one of the following formats:

或者对于完全符合 ISO-8601 的 date,请使用以下格式之一:

date -u +"%Y-%m-%dT%H:%M:%SZ"

Output:

输出:

2011-08-27T23:22:37Z

or

或者

date +%Y-%m-%dT%H:%M:%S%z

Output:

输出:

2011-08-27T15:22:37-0800

回答by Lri

In GNU date date -Iis the same as date +%F, and -Isecondsand -Iminutesalso include time with UTC offset.

在 GNU 中日期date -I与 相同date +%F-Iseconds并且-Iminutes还包括具有 UTC 偏移量的时间。

$ date +%F # -I or +%Y-%m-%d
2013-05-03
$ date +%FT%T%z # -Iseconds or +%Y-%m-%dT%H:%M:%S%z
2013-05-03T15:59:24+0300
$ date +%FT%H:%M # -Iminutes or +%Y-%m-%dT%H:%M%z
2013-05-03T15:59+0300

-uis like TZ=UTC. +00:00can be replaced with Z.

-u就像TZ=UTC. +00:00可以替换为Z

$ date -u +%FT%TZ
2013-05-03T12:59:24Z

These are also valid ISO 8601 date or time formats:

这些也是有效的 ISO 8601 日期或时间格式:

20130503T15 (%Y%m%dT%M)
2013-05 (%Y%m)
2013-W18 (%Y-W%V)
2013-W18-5 (%Y-W%V-%u)
2013W185 (%YW%V%u)
2013-123 (%Y-%j, ordinal date)
2013 (%Y)
1559 (%H%M)
15 (%H)
15:59:24+03 (UTC offset doesn't have to include minutes)

These are not:

这些不是:

2013-05-03 15:59 (T is required in the extended format)
201305 (it could be confused with the YYMMDD format)
20130503T15:59 (basic and exteded formats can't be mixed)

回答by SomeGuy

A short alternative that works on both GNU and BSD date is:

适用于 GNU 和 BSD 日期的简短替代方法是:

date -u +%FT%T%z

回答by slm

The coreutils package provides GNU versions of tools. To install:

coreutils 包提供了 GNU 版本的工具。安装:

$ brew install coreutils

You can see what's provided:

您可以查看提供的内容:

$ brew list coreutils

Notice it comes with date:

注意它带有日期:

$ brew list coreutils | grep date

This is the standard GNU date command so it'll take the -I switch:

这是标准的 GNU date 命令,因此它将采用 -I 开关:

$ gdate -I
2016-08-09

回答by Tom Zych

Just use normal dateformatting options:

只需使用正常的date格式选项:

date '+%Y-%m-%d'

Edit: to include time and UTC, these are equivalent:

编辑:要包括时间和 UTC,这些是等效的:

date -u -Iseconds

date -u '+%Y-%m-%dT%k:%M:%S%z'

回答by tripleee

It's not a feature of Bash, it's a feature of the datebinary. On Linux you would typically have the GNU coreutils version of date, whereas on OSX you would have the BSD legacy utilities. The GNU version can certainly be installed as an optional package, or you can roll your own replacement - I believe it should be a simple one-liner e.g. in Perl.

这不是 Bash 的特性,而是date二进制文件的特性。在 Linux 上,您通常拥有 GNU coreutils 版本date,而在 OSX 上,您将拥有 BSD 遗留实用程序。GNU 版本当然可以作为一个可选包安装​​,或者你可以推出自己的替代品——我相信它应该是一个简单的单线,例如在 Perl 中。

回答by ruby_slinger

Taking the other answers one step further, you could add a function to your ~/.bashrc or ~/.zshrc to add the date -Iflag:

将其他答案更进一步,您可以向 ~/.bashrc 或 ~/.zshrc 添加一个函数以添加日期-I标志:

date() {
  if [ "" = "-I" ]; then
    command date "+%Y-%m-%dT%H:%M:%S%z"
  else
  command date "$@"
  fi
}

回答by tonyk

There's a precompiled coreutilspackage for Mac OS X available at:

有一个coreutils适用于 Mac OS X的预编译包,位于:

http://rudix.org/packages-abc.html#coreutils.

http://rudix.org/packages-abc.html#coreutils