'date -d'解析日期怎么样? [英] How does 'date -d' parse dates?

查看:142
本文介绍了'date -d'解析日期怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚(孙俊9 17:19:24 MDT 2013​​),我运行了这个命令:

 > ;日期-d下个月第一个星期日
Tue Jul 9 00:00:00 MDT 2013

这不是星期天,甚至不是下个月的第一个星期二:




  • 日期-d解释我的输入?


  • 到目前为止还有一个详细选项(甚至date -d),显示
    我的输入是如何解释的?它不是-v,man date不是
    显示一个详细的选项。


  • 我意识到可能有一些库处理date -d。其中
    可以找到该库及其文档,限制等?


  • 我意识到没有程序可以处理每种可能的格式,但是
    不明白看​​到date -d给出错误的答案。
    首选日期:不能解析下个月的第一个星期天
    错误的答案。




编辑:在fedora 11核心上运行:

 >日期--version 
日期(GNU coreutils)7.2
版权所有(C)2009免费软件基金会,
许可证GPLv3 +:GNU GPL版本3或更高版本< http:// gnu。 org / licenses / gpl.html> ;.
这是免费软件:您可以自由更改并重新分发。
在法律允许的范围内,没有任何保证。


解决方案

GNU日期选项:



-d,--date =字符串描述的字符串显示时间,而不是现在。它是一种人类可读的格式,如下周四或1个月前。日期字符串可以包含指示日历日期,时间,时区,星期几,相对时间,相对日期和数字的项目。这也被称为相对GNU日期格式。[1]以下是相关日期的几个示例:

  date --date =1天前
date - date =yesterday
date --date = '10个月前'
date --date ='2小时前'
日期--date ='第二个星期五'
我直截了当地从维基中复制了这个。 : http://en.wikipedia.org/wiki/Date_(Unix)



此日期-d为愚蠢的 - 给了我7月16日下个月的第一个星期天下个月。

  Kaizen〜$ date -d 
+ date -d'第一个星期天下个月'
Tue Jul 16 00:00:00 IST 2013

似乎该字符串的关键字是anded,然后给出结果....星期日+下个月...在今天日期之后,即10月(6月)在下个月(7月)是第16的七月。
如果我在星期一使用星期一而不是星期日,我将在7月17日作为答案。

  Kaizen〜$ date  - d下个月星期一
+ date -d'下个月星期一'
Wed Jul 10 00:00:00 IST 2013

如果我使用下个月一星期,那么它将会到下个月,并从今天的日期开始寻找星期一,比如说第10个是结果。



这样做有帮助吗?



此外,它超级不可移植,并且在所有服务器上都无法正常工作...不知道你,但是我将远离编程目的。



也有一个类似性质的超级用户问题: http://superuser.com/questions / 572088 / unix-date-command-not-working-for-few-servers 你可以检查出来


Just now ("Sun Jun 9 17:19:24 MDT 2013"), I ran this command:

> date -d "first Sunday next month" 
Tue Jul  9 00:00:00 MDT 2013 

which is neither a Sunday, and isn't even the first Tuesday of next month:

  • How is date -d interpreting my input?

  • Is there a verbose option to date (or even "date -d") that shows how my input is interpreted? It's not -v, and "man date" doesn't show a verbose option.

  • I realize there's probably some library handling "date -d". Where can I find that library and its documentation, limitations, etc?

  • I realize no program can handle every possible format, but it's unnerving to see "date -d" give the wrong answer. I'd have much preferred "date: can not parse 'first Sunday next month'" to the wrong answer.

EDIT: running this on fedora 11 core:

> date --version
date (GNU coreutils) 7.2
Copyright (C) 2009 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.

解决方案

GNU date options :

-d, --date=string display time described by string, not now. It is a human readable format such as "next Thursday" or "1 month ago". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. This is also known as relative GNU date formats.[1] Here are a few examples of relative date:

date --date="1 days ago"
date --date="yesterday"
date --date='10 month ago'
date --date='2 hour ago'
date --date='Second Friday'

i bluntly copied this from the wiki. : http://en.wikipedia.org/wiki/Date_(Unix)

this date -d is stupid -- gave me 16th July as the next first sunday next month

Kaizen ~ $ date -d "first Sunday next month"
+ date -d 'first Sunday next month'
Tue Jul 16 00:00:00 IST 2013

it seems that the string has keywords that are anded and then results given.... Sunday + next month... after todays date ie 10th (in June) in the next month (July) is 16th of July. if i did use Monday here instead of Sunday , i get 17th July as answer.

 Kaizen ~ $ date -d "next month monday"
 + date -d 'next month monday'
 Wed Jul 10 00:00:00 IST 2013

like wise if i use "next month monday" then it will go to the next month and look for monday from todays date ie 10th is the result.

does this help ?

Also it's super not portable and dosen't work alike on all servers ... dont know about you but I would stay away from it for programing purposes.

also there was a super user question of similar nature : http://superuser.com/questions/572088/unix-date-command-not-working-for-few-servers you can check that out too

这篇关于'date -d'解析日期怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆