linux下date的陷阱

date在linux下是一个很好用的时间函数,有很多好用的方法比如:

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

我们可以很方便的计算n天,n月,n年之后的日期,但是最近在工作中遇到一个陷阱,发现date一个不准的地方,提醒大家使用的时候重要,不要那么相信date算出来的时间就是对的。我们的场景是要计算下个月的月份,但是发现在1月31日的时候使用“date -d'+1 month' +%Y%m”得到的不是2月而是3月。

具体可以看下面的例子:

ps:所有测试都是在1月31日这个前提下。

[root@hebe210 ~]# date +%Y%m%d
20130131
[root@hebe210 ~]# date -d'+1 month' +%Y%m%d
20130303
[root@hebe210 ~]# date -d'+30 day' +%Y%m%d       
20130302
[root@hebe210 ~]# date -d'+29 day' +%Y%m%d   
20130301
[root@hebe210 ~]# date -d'+28 day' +%Y%m%d 
20130228
[root@hebe210 ~]# date -d'+31 day' +%Y%m%d  
20130303

我们可以看出,在计算+1 month的时候,date得到的是3月3日,得到的并不是我们想象的2月28日这天。从上面的情况看来,date将1month转化成了31day来计算,而2月份只有28天,故结果便成了3月3日。

但是其实不是这样,我们再来验证一下,多个月的情况:

[root@hebe210 ~]# date -d'+3 month' +%Y%m%d          
20130501
[root@hebe210 ~]# date -d'+93 day' +%Y%m%d      
20130504
[root@hebe210 ~]# date -d'+4 month' +%Y%m%d 
20130531
[root@hebe210 ~]# date -d'+124 day' +%Y%m%d      
20130604

我们可以看出来,实际上并不是严格按照31天这个模式进行计算的。

根据上面的现象,我们初步判断,date是如此进行计算的:

1、+n month的情况

[root@hebe210 ~]# date -d'+1 month' +%Y%m%d       
20130303
[root@hebe210 ~]# date -d'+2 month' +%Y%m%d 
20130331
[root@hebe210 ~]# date -d'+3 month' +%Y%m%d 
20130501
[root@hebe210 ~]# date -d'+4 month' +%Y%m%d 
20130531
[root@hebe210 ~]# date -d'+5 month' +%Y%m%d 
20130701
[root@hebe210 ~]# date -d'+6 month' +%Y%m%d 
20130731
[root@hebe210 ~]# date -d'+7 month' +%Y%m%d 
20130831
[root@hebe210 ~]# date -d'+8 month' +%Y%m%d 
20131001
[root@hebe210 ~]# date -d'+9 month' +%Y%m%d 
20131031
[root@hebe210 ~]# date -d'+10 month' +%Y%m%d 
20131201
[root@hebe210 ~]# date -d'+11 month' +%Y%m%d 
20131231

都按按照这个公式进行计算 +1 month 转换成当月的天数,+n month,转化成 n个月的当月天数之和。

[root@hebe210 ~]# date -d'+1 month' +%Y%m%d
20130303
[root@hebe210 ~]# date -d'+31 day' +%Y%m%d        
20130303
[root@hebe210 ~]# date -d'+3 month' +%Y%m%d 
20130501
[root@hebe210 ~]# date -d'+90 day' +%Y%m%d   
20130501

90 day = (1月 + 2月 + 3月)=(31 + 28 + 31)=90.

2、-n month的情况

[root@hebe210 ~]# date -d'-1 month' +%Y%m%d   
20121231
[root@hebe210 ~]# date -d'-2 month' +%Y%m%d 
20121201
[root@hebe210 ~]# date -d'-3 month' +%Y%m%d 
20121031
[root@hebe210 ~]# date -d'-4 month' +%Y%m%d 
20121001
[root@hebe210 ~]# date -d'-5 month' +%Y%m%d 
20120831
[root@hebe210 ~]# date -d'-6 month' +%Y%m%d 
20120731
[root@hebe210 ~]# date -d'-7 month' +%Y%m%d  
20120701
[root@hebe210 ~]# date -d'-8 month' +%Y%m%d 
20120531
[root@hebe210 ~]# date -d'-9 month' +%Y%m%d 
20120501
[root@hebe210 ~]# date -d'-10 month' +%Y%m%d 
20120331
[root@hebe210 ~]# date -d'-11 month' +%Y%m%d 
20120302

而-1 month的计算又有些区别,-1 month 会转化成 -上个月天数 day来计算。对于 -n month也同样

[root@hebe210 ~]# date -d'-1 month' +%Y%m%d  
20121231
[root@hebe210 ~]# date -d'-31 day' +%Y%m%d     
20121231
[root@hebe210 ~]# date -d'-3 month' +%Y%m%d 
20121031
[root@hebe210 ~]# date -d'-92 day' +%Y%m%d         
20121031

92 day = (12月 + 11月 +10月)=(31 + 30 +31)=92 day

本想找源码支持一下,但是能力有限没有找到,就先这样吧。

经过网友 @memories_on 分析,似乎并不关date的事情,具体为date对于相对日期只是负责加减tm的成员, 然后调用了mktime, mktime normalize tm的成员进行的解析。

代码参考如下;

https://gist.github.com/4690456#file-gistfile1-txt-L1499

https://gist.github.com/4690456#file-gistfile1-txt-L259

最后,我们用最麻烦的2月在证明一下

[root@hebe210 ~]# date -d'20130228 +1 month' +%Y%m%d                    
20130328
[root@hebe210 ~]# date -d'20130228 +28 day' +%Y%m%d       
20130328
[root@hebe210 ~]# date -d'20130228 -1 month' +%Y%m%d       
20130128
[root@hebe210 ~]# date -d'20130228 -31 day' +%Y%m%d      
20130128
[root@hebe210 ~]# date -d'20130228 +2 month' +%Y%m%d  
20130428
[root@hebe210 ~]# date -d'20130228 +59 day' +%Y%m%d       
20130428
[root@hebe210 ~]# date -d'20130228 -2 month' +%Y%m%d  
20121228
[root@hebe210 ~]# date -d'20130228 -62 day' +%Y%m%d       
20121228

附录:

date的使用方法

Linux下date命令用法

  • date [OPTION]… [+FORMAT]
  • date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

date命令参数

  • -d, –date=STRING  显示STRING指定的时间
  • -f, –file=DATEFILE  类似–date参数显示DATEFILE文件中的每行时间
  • -ITIMESPEC, –iso-8601[=TIMESPEC]  以ISO  8601 格式显示日期/时间。TIMESPEC为”date”(只显示日期)、”hours”、”minutes”、”senconds”(显示时间精度)之一,默认为”date”。
  • -r, –reference=FILE  显示文件的最后修改时间
  • -R, –rfc-2822  以RFC-2822兼容日期格式显示时间
  • -s, –set=STRING  设置时间为STRING
  • -u, –utc, –universal  显示或设定为Coordinated Universal Time时间格式

date命令输出显示格式

  • %%    字符%
  • %a     星期的缩写(Sun..Sat)
  • %A    星期的完整名称 (Sunday..Saturday)
  • %b     月份的缩写(Jan..Dec)
  • %B     月份的完整名称(January..December)
  • %c     日期时间(Sat Nov 04 12:02:33 EST 1989)
  • %C     世纪(年份除100后去整) [00-99]
  • %d     一个月的第几天(01..31)
  • %D     日期(mm/dd/yy)
  • %e     一个月的第几天 ( 1..31)
  • %F    日期,同%Y-%m-%d
  • %g     年份(yy)
  • %G     年份(yyyy)
  • %h     同%b
  • %H    小时(00..23)
  • %I     小时(01..12)
  • %j     一年的第几天(001..366)
  • %k     小时( 0..23)
  • %l      小时( 1..12)
  • %m    月份(01..12)
  • %M    分钟(00..59)
  • %n     换行
  • %N     纳秒(000000000..999999999)
  • %p     AM or PM
  • %P     am or pm
  • %r     12小时制时间(hh:mm:ss [AP]M)
  • %R    24小时制时间(hh:mm)
  • %s     从00:00:00 1970-01-01 UTC开始的秒数
  • %S     秒(00..60)
  • %t     制表符
  • %T    24小时制时间(hh:mm:ss)
  • %u     一周的第几天(1..7);  1 表示星期一
  • %U     一年的第几周,周日为每周的第一天(00..53)
  • %V     一年的第几周,周一为每周的第一天 (01..53)
  • %w     一周的第几天 (0..6);  0 代表周日
  • %W    一年的第几周,周一为每周的第一天(00..53)
  • %x     日期(mm/dd/yy)
  • %X     时间(%H:%M:%S)
  • %y     年份(00..99)
  • %Y     年份 (1970…)
  • %z     RFC-2822 风格数字格式时区(-0500)
  • %Z     时区(e.g., EDT), 无法确定时区则为空
原文地址:https://www.cnblogs.com/billyxp/p/2886733.html