如何使用日期解析时区

时间:2018-01-16 14:27:07

标签: linux bash date parsing timezone

我们如何获取日期实用程序来解析时区字符串?

$>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.

这是本地信息:

datatable(iris, filter="top", selection="multiple", escape=FALSE,
options = list(searching = FALSE))

2 个答案:

答案 0 :(得分:2)

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 :(得分:1)

date -ddate +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.