如何解析日期的时区

3
我们如何让日期工具解析时区字符串?
$>date -d "2018-02-21T02:22:33.221" "+%Y-%m-%dT%H:%M:%S.%3N %Z"
2018-02-20T14:22:33.221 EST

但是。
$>date -d "2018-02-21T02:22:33.221 EST" "+%Y-%m-%dT%H:%M:%S.%3N %Z"
date: invalid date `2018-02-21T02:22:33.221 EST'

这是本地信息:
$>uname -r
2.6.32-696.13.2.el6.x86_64

$>date
Tue Jan 16 09:58:52 EST 2018

$>date --version
date (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.

你的系统所在的时区是什么? - glenn jackman
1
你的问题需要说明你正在使用RHL。祝好运。 - shellter
在我的Ubuntu 16.04上可以运行,分隔符为"T"或空格。date --versiondate (GNU coreutils) 8.25;你的呢? - Murphy
顺便说一句,这不是一个编程问题,最好在https://unix.stackexchange.com上提问;那里才有真正的专家。不要重复发帖,请移动问题。 - Murphy
@ Murphy,当你在Bash上编程时,这就是一个编程问题 :) 但Unix.stackexchange.com也许是一个不错的主意...但是如何迁移问题呢? - Deian
告诉一位管理员(答案已经过期,但据我所知仍然有效):https://meta.stackoverflow.com/questions/266749/migration-of-code-questions-from-stack-overflow-to-code-review - Murphy
2个回答

3

ISO8601 允许您将 T 替换为空格。只需这样做,您的命令就可以正常工作:

# date -d "2018-02-21T02:22:33.221 EST" "+%Y-%m-%dT%H:%M:%S.%3N %Z"
date: invalid date `2018-02-21T02:22:33.221 EST'

但是:
# date -d "2018-02-21 02:22:33.221 EST" "+%Y-%m-%d %H:%M:%S.%3N %Z"
2018-02-21 10:22:33.221 MSK

1

date -d 不是 date +format 的反向操作,它只能接受一些来自 man date 的字符串。

  -d, --date=STRING
          display time described by STRING, not `now'
DATE STRING
   The  --date=STRING  is  a  mostly  free  format  human  readable  date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or
   "2004-02-29 16:21:42" or even "next Thursday".  A date string may contain items indicating calendar  date,  time  of  day,
   time  zone,  day  of week, relative time, relative date, and numbers.  An empty string indicates the beginning of the day.
   The date string format is more complex than is easily documented here but is fully described in the info documentation.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接