用awk来计算天数差

说明

关于天数差和对日期的增减,很多其他的方法喜欢对日期的天数下手或是date -d参数,但有一次我在aix环境中没有date -d参数,但庆幸的是发现了awk 中关于unix时间的转换

原理

首先普及以下unix时间的概念,unix时间是从1970-1-1 08:00:00开始计时,往后每加一秒就+1
往前减一秒就是-1,1970-1-1 08:00:00就是0
然后使用awk的两个内置函数就能完成这个转换
例如我拿1970-1-1 08:00:00举例
把这个转换为unix时间,用mktime函数

[root@bianyi ~]# awk 'BEGIN{print mktime("1970 01 01 08 00 00")}'
0

把unix时间转换为日期时间,用strftime函数

[root@bianyi ~]# awk 'BEGIN{print strftime("%Y-%m-%d %H:%M:%S",0)}'
1970-01-01 08:00:00

好了,可以利用两个函数把日期时间的计算转换成单纯的加减法
例如我要计算2020-2-20日到2020-3-15需要多要天(特别用闰年测试)

[root@bianyi ~]# awk 'BEGIN{
>      aday=mktime("2020 02 20 00 00 00")
>      bday=mktime("2020 03 15 00 00 00")
>      days=(bday-aday)/86400
>      print "天数是: "days}'
天数是: 24

那不是闰年呢?
计算2019-2-20日到2019-3-15需要多要天

[root@bianyi ~]# awk 'BEGIN{
>      aday=mktime("2019 02 20 00 00 00")
>      bday=mktime("2019 03 15 00 00 00")
>      days=(bday-aday)/86400
>      print "天数是: "days}'
天数是: 23

如果细心看有人会问86400是啥玩意,那是一天的秒数,即60*60*24
对此,加一天,减一天怎么玩,如下

[root@bianyi ~]# awk -v day="2020 05 01 00 00 00" \
>        'BEGIN{
>         today=mktime(day)
>         tomorrow=strftime("%Y-%m-%d %H:%M:%S",today+60*60*24)
>         print "明天是: "tomorrow
>        }'
明天是: 2020-05-02 00:00:00
[root@bianyi ~]# awk -v day="2020 05 01 00 00 00" \
>        'BEGIN{
>         today=mktime(day)
>         yesterday=strftime("%Y-%m-%d %H:%M:%S",today-60*60*24)
>         print "昨天是: "yesterday
>        }'
昨天是: 2020-04-30 00:00:00

那1970-1-1 以前的日子呢,能不能负数运算,目前gawk 4.0.2这还是个bug
如下

[root@bianyi ~]# rpm -qf /usr/bin/awk
gawk-4.0.2-4.el7_3.1.x86_64
[root@bianyi ~]# awk  \
>        'BEGIN{
>         today=mktime(day)
>         yesterday=strftime("%Y-%m-%d %H:%M:%S",-1)
>         print "1970-1-1 08:00:00以前的日子 "yesterday
>        }'
awk: cmd. line:3: fatal: strftime: second argument less than 0 or too big for time_t

具体可以看以下连接https://stackoverflow.com/questions/29542459/awk-with-dates-before-1970

如果处理的数据偏偏有1970-1-1 08:00:00以前的日子,那只能升级gawk,镜像源里目前没有找到gawk高版本的,只能通过源码编译安装,以下是我编译升级到gawk-4.1.2的效果

[root@bianyi gawk-4.1.2]# awk -V
GNU Awk 4.1.2, API: 1.1
Copyright (C) 1989, 1991-2015 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
[root@bianyi gawk-4.1.2]# awk -v day="1970 01 01 00 00 00" \
>        'BEGIN{
>         today=mktime(day)
>         yesterday=strftime("%Y-%m-%d %H:%M:%S",-1)
>         print "1970-1-1 08:00:00 1秒前的时间点: "yesterday
>        }'
1970-1-1 08:00:00 1秒前的时间点: 1970-01-01 07:59:59
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。