date时间加减(linux,aix)

本文介绍了如何在Linux和AIX系统下处理日期时间的加减及计算时间差值。在shell脚本中,需要使用$(())进行数字运算。在AIX中,由于date命令不支持-d选项,因此需要通过字符串处理方法来比较日期。作者提供了一种将固定格式日期字符串分割并比较的方法,以判断两个时间是否相差超过24小时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求是这样的,有一个在日志中的时间,格式化为%Y%m%d-%H%M%S格式的,那现在想比较这个时间与当前时间差值是否大于一天,这个应该怎么做呢?设计到日期的减法运算,首先先man date来看一下用法吧。

DATE(1)                          User Commands                         DATE(1)

NAME
       date - print or set the system date and time
            - 打印或设置系统日期和时间

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

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.
       以给定的格式显示当前时间,或者设置系统时间。

       -d, --date=STRING
              display time described by STRING, not ‘now’
              显示字符串表示的时间,字符串不能是“now”

       -f, --file=DATEFILE
              like --date once for each line of DATEFILE


       -r, --reference=FILE
              display the last modification time of FILE
              显示文件最近一次修改的时间

       -R, --rfc-2822
              output  date  and time in RFC 2822 format.  Example: Mon, 07 Aug
              2006 12:34:56 -0600
              以RFC格式输出日期和时间,示例:Mon, 07 Aug 2006 12:34:56 -0600

       --rfc-3339=TIMESPEC
              output date and time in RFC 3339 format.  TIMESPEC=‘date’, ‘sec-
              onds’,  or  ‘ns’  for  date and time to the indicated precision.
              Date and time  components  are  separated  by  a  single  space:
              2006-08-07 12:34:56-06:00

       -s, --set=STRING
              set time described by STRING
              设置字符串表示的时间

       -u, --utc, --universal
              print or set Coordinated Universal Time
              打印或设置 世界标准时

       --help display this help and exit

       --version
              output version information and exit

       FORMAT controls the output.  Interpreted sequences are:
       格式控制输出,对应的序列如下:

       %%     a literal %

... ... ...

       %s     seconds since 1970-01-01 00:00:00 UTC  从1970-01-01 00:00:00开始之后的秒数,这个就是时间戳了。

       %S     second (00..60)
 ...  ...  ... 

       By default, date  pads  numeric  fields  with  zeroes.   The  following
       optional flags may follow ‘%’:

       -      (hyphen) do not pad the field

       _      (underscore) pad with spaces

       0      (zero) pad with zeros

       ^      use upper case if possible

       #      use opposite case if possible

       After  any  flags  comes  an optional field width, as a decimal number;
       then an optional modifier, which is either E to use the locale’s alter-
       nate  representations  if available, or O to use the locale’s alternate
       numeric symbols if available.

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  cal-
       endar  date,  time of day, time zone, day of week, relative time, rela-
       tive 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.
... ...

日期加减时间

[root@db22 tmp]# date +'%Y-%m-%d %H:%M:%S'
2017-07-04 10:36:36
[root@db22 tmp]# date -d'+2 day' +'%Y-%m-%d %H:%M:%S'
2017-07-06 10:36:39
[root@db22 tmp]# date -d '2017-07-06 10:36:39'
Thu Jul  6 10:36:39 CST 2017
[root@db22 tmp]# date -d '2017-07-06 10:36:39 -2 day'
Fri Jul  7 20:36:39 CST 2017
[root@db22 tmp]# date -d '2017-07-06 10:36:39' +'%Y-%m-%d %H:%M:%S'
2017-07-06 10:36:39
[root@db22 tmp]# date -d '2017-07-06 10:36:39 -2 day' +'%Y-%m-%d %H:%M:%S'
2017-07-07 20:36:39

日期/时间差值

[root@db22 tmp]# a=`date -d '2017-07-06 10:36:39' +'%s'`
[root@db22 tmp]# b=`date -d '2017-07-04 10:36:42' +'%s'`
[root@db22 tmp]# echo $a
1499308599
[root@db22 tmp]# echo $b
1499135802
[root@db22 tmp]# c=$a-$b
[root@db22 tmp]# echo $c
1499308599-1499135802
[root@db22 tmp]# c=$(($a-$b))
[root@db22 tmp]# echo $c
172797
[root@db22 tmp]# echo $c/60/60
172797/60/60
[root@db22 tmp]# echo $(($c/60/60))
47

这里要注意的是:shell里面的数字字符串是看做字符串处理的,若要进行运算,需要使用$(())符号。

aix中的求时间差值的处理

虽然上面的看着是怪顺的,但是很遗憾,写好了之后一运行就报”-d选项不识别”的错误,man date就能发现aix中的date命令不支持-d选项,所以,我们获取到的时间字符串就不能转换成时间戳了,于是只能换别的方式处理,网上看了一些别人的案例,但是看着比较繁琐(别人的代码总是很难看懂的样子),于是自己思考了一下做了处理,因为日期字符串的格式是固定的,所以将其分成数组进行的比较处理,就记录一下供自己以后查看,如果能给别人一种提示也蛮好的。

我的日期格式是这样子的date +"%Y-%m-%d/%H%M%S",首先将日期字符串分割成数组,然后进行比较,代码如下(我这里只要大于等于24小时就可以):
判断两个时间之间差是否大于24小时的代码:

#---从一个日志文件中读入date +"%Y-%m-%d/%H%M%S"格式的字符串
awk -v db=${db} -v tb=${tb} '$4==db && $6==tb{print $1,$5,$6,$7}' ${DBPathFile}|tail -1|read _rename_time _tb_schema  _old_tb_name  _new_tb_name
echo "_rename_time:${_rename_time}"

#linux可以使用下面的方法简单求得时间差值
#cur_tsp=`date +%s`
#_rename_tsp=`date -d"${_rename_time}" +%s`
#diff_seconds=$(($cur_tsp-${_rename_tsp}))
#_day_sec=$((24*60*60))


# aix系统中没有-d选项,所以这里使用下面的方法:
_cur_time=`date +"%Y-%m-%d/%H%M%S"`
#-------linux 中可以使用(1 2 3 4 5)这种形式组装数组
#_rename_time_arr=(`echo ${_rename_time}| awk -F '-|/|:' '{print $1,$2,$3,$4,$5,$6}'`)           
#--------aix 中不能使用以上的方式,可以使用 set -A *数组名*  1 2 3 4 5 这种方式
set -A _rename_time_arr `echo ${_rename_time}| awk -F '-|/|:' '{print $1,$2,$3,$4,$5,$6}'`
#_cur_time_arr=(`echo ${_cur_time}| awk -F '-|/|:' '{print $1,$2,$3,$4,$5,$6}'`)           
set -A _cur_time_arr `echo ${_cur_time}| awk -F '-|/|:' '{print $1,$2,$3,$4,$5,$6}'`
#echo "re:${_rename_time_arr[@]}"
#echo "cur:${_cur_time_arr[@]}"
exceed_flag=0
[ "${_cur_time_arr[0]}" -gt "${_rename_time_arr[0]}" ] || [ "${_cur_time_arr[1]}" -gt "${_rename_time_arr[1]}" ] || [ "${_cur_t        ime_arr[2]}" -ge $(("${_rename_time_arr[2]}"+2)) ] && exceed_flag=1
echo "exceed_flag :$exceed_flag"
if [ "${exceed_flag}" -eq 0 -a "${_cur_time_arr[2]}" -eq $(("${_rename_time_arr[2]}"+1)) ]
then
    #compare h,m,s
    _cur_second=$(("${_cur_time_arr[3]}"*60*60+"${_cur_time_arr[4]}"*60+"${_cur_time_arr[5]}"))
    _rename_second=$(("${_rename_time_arr[3]}"*60*60+"${_rename_time_arr[4]}"*60+"${_rename_time_arr[5]}"))
    _diff_second=$(("${_cur_second}"-"${_rename_second}"))
    echo "_diff_second:${_diff_second}"
    [ "${_diff_second}" -ge 0 ] && exceed_flag=1
fi

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值