日期命令 --iso-8601 选项

Ang*_*elo 139 date

这个答案和评论提到--rfc-3339了一个“隐藏”--iso-8601选项,我已经使用了很长时间,现在似乎没有记录。

该选项文档何时从--help文本中删除?

该选项会很快消失吗?

Ant*_*hon 113

该选项date于 1999 年(4 月 8 日)在 coreutils (可能就是您所拥有的)中引入。

该文档在2005 年被删除,在提交中没有太多解释。

2011,对于--iso-8601的帮助下重新与下面的解释:

We deprecated and undocumented the --iso-8601 (-I) option mostly
because date could not parse that particular format.  Now that
it can, it's time to restore the documentation.
* src/date.c (usage): Document it.
* doc/coreutils.texi (Options for date): Reinstate documentation.
Reported by Hubert Depesz Lubaczewski in http://bugs.gnu.org/7444.
Run Code Online (Sandbox Code Playgroud)

看起来帮助已在 5.90 版中取出并在 8.15 版中放回(它不在我的 8.13 版中),上面的评论表明它现在又回来了,不太可能很快消失。

在 8.31 版(由 Solus 2020 年 7 月提供)中,两个选项的手册页描述是:

   -I[FMT], --iso-8601[=FMT]
          output date/time in ISO 8601 format.  FMT='date' for date only (the default), 'hours', 'minutes', 'sec?
          onds', or 'ns' for date and time to the indicated precision.  Example: 2006-08-14T02:34:56-06:00

   --rfc-3339=FMT
          output date/time in RFC 3339 format.  FMT='date', 'seconds', or 'ns' for date and time to the indicated
          precision.  Example: 2006-08-14 02:34:56-06:00
Run Code Online (Sandbox Code Playgroud)


Pád*_*ady 49

--help 最近实际上得到了更新,因此该选项绝对不会消失:

-I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.
                             FMT='date' for date only (the default),
                             'hours', 'minutes', 'seconds', or 'ns'
                             for date and time to the indicated precision.
                             Example: 2006-08-14T02:34:56-06:00

     -R, --rfc-2822        output date and time in RFC 2822 format.
                             Example: Mon, 14 Aug 2006 02:34:56 -0600

         --rfc-3339=FMT    output date/time in RFC 3339 format.
                             FMT='date', 'seconds', or 'ns'
                             for date and time to the indicated precision.
                             Example: 2006-08-14 02:34:56-06:00
Run Code Online (Sandbox Code Playgroud)

请注意,由于 coreutils-8.27--rfc-2822已被弃用,以支持更通用的--rfc-email

     -R, --rfc-email       output date and time in RFC 5322 format.
                             Example: Mon, 14 Aug 2006 02:34:56 -0600
Run Code Online (Sandbox Code Playgroud)

  • 如果有一个没有时区的`-I` 样式会很好。对于时区无关紧要的日志记录等会更有用 (4认同)
  • @naught101您可以通过指定组成部分来消除时区偏移:`date +"%Y-%m-%dT%H:%M:%S`。 (2认同)

alf*_*era 34

I'm running Linux Mint and the option is available:

$ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.3 Rosa
Release:    17.3
Codename:   rosa
Run Code Online (Sandbox Code Playgroud)

The execution of the command:

$ date --iso-8601=seconds
2016-12-14T09:53:25-0400
Run Code Online (Sandbox Code Playgroud)

An alternative that appends the Z at the end:

$ echo "$(date --iso-8601=seconds)Z"
2021-06-14T23:26:06+00:00Z
Run Code Online (Sandbox Code Playgroud)

  • 或者只是`date -Iseconds` (12认同)
  • 但它不会在末尾附加“Z”,因此这不是完全完整的 ISO 时间戳 (11认同)
  • “但它不会将 Z 附加到末尾,因此这不是完全完整的 ISO 时间戳” Z 是 UTC 时区。`+00:00` 与 `Z` 相同。`+00:00Z` 不正确。 (7认同)
  • `date -In` (与 `date -Ins` 相同)或 `date -Is` 与 (`date -Iseconds`) 相同等。 (6认同)
  • 对于文件名兼容格式,我使用 `$(date -u +"%Y-%m-%dT%H-%M-%SZ")` (3认同)

rou*_*ble 20

对于独立于平台、(几乎)完全兼容的 ISO 8601 日期,请使用以下内容:

date +"%Y-%m-%dT%H:%M:%S%z"
Run Code Online (Sandbox Code Playgroud)

这将导致如下时间: 2021-01-16T23:09:44-0500

这应该适用于 BSD 和 Linux。

从技术上讲,为了完全合规,它应该是:

date +"%Y-%m-%dT%H:%M:%S%:z"
Run Code Online (Sandbox Code Playgroud)

(注意偏移量有一个冒号它。)这将导致一个时间,如:2021-01-16T23:09:44-05:00

但这在 Mac OSX 上不起作用(在撰写本文时)。

对于UTC时间(零偏移),您可以使用(注意-u):

date -u +"%Y-%m-%dT%H:%M:%SZ"
Run Code Online (Sandbox Code Playgroud)

这将导致如下时间: 2021-01-17T04:16:14Z

  • @jgreve 不,你刚刚表明 Mac 的“date”不理解“%:z”格式控制。彻底检查你的输出,它用 `:` 分隔符写了文字 `:z` 而不是你的时区,例如 `date +%:z` -> `+02:00` --- shell 通常也没有什么可做的这样做是因为“date”通常是外部可执行文件,而不是 shell 的内置文件。 (5认同)
  • Mac OSX 现在似乎很高兴使用 zsh 和 bash: `% date +"%Y-%m-%dT%H:%M:%S%:z"` 产生 `2022-03-22T11:05:42:z ` 在 `Darwin 21.3.0 Darwin 内核版本 21.3.0` 上。 (4认同)
  • MacOS 12.6 支持“date -Iseconds”,但不知道何时更改! (2认同)