核心代码

当你遇到一个date命令不给力的系统时,可以试试这几个小函数。

#日期转天数
function date2days {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}
date2days `echo "2010-08-18 18:59:19" | sed 's/-/ /g;s/:/ /g'`

#天数转日期
function days2date {
  echo "$1" | awk '{
    a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d-%02d-%02d\n",yy,mm,dd)
  }'
}
days2date 14839

#日期转分钟
function date2minutes {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    j=j*1440+$4*60+$5
    print j
  }'
}
date2minutes `echo "2010-08-18 18:59:19" | sed 's/-/ /g;s/:/ /g'`

#分钟转日期
function minutes2date {
  echo "$1" | awk '{
    i=$1; nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24); i=int(i/24);
    a=i+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d-%02d-%02d %02d:%02d\n",yy,mm,dd,hh,nn)
  }'
}
minutes2date 21369299

#日期转秒数
function date2seconds {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    j=j*86400+$4*3600+$5*60+$6
    print j
  }'
}
date2seconds `echo "2010-08-18 18:59:19" | sed 's/-/ /g;s/:/ /g'`

#秒数转日期
function seconds2date {
  echo "$1" | awk '{
    i=$1; ss=i%60; i=int(i/60); nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24); i=int(i/24);
    a=i+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d-%02d-%02d %02d:%02d:%02d\n",yy,mm,dd,hh,nn,ss)
  }'
}
seconds2date 1282157959

#日期转毫秒
function date2milliseconds {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    j=j*86400+$4*3600+$5*60+$6
    printf ("%d%s\n",j,$7)
  }'
}
date2milliseconds `echo "2010-08-18 18:59:19.073" | sed 's/-/ /g;s/:/ /g;s/\./ /g'`

#毫秒转日期
function milliseconds2date {
  echo "$1" | awk '{
    i=$1; ms=i%1000; i=int(i/1000); ss=i%60; i=int(i/60); nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24); i=int(i/24);
    a=i+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d-%02d-%02d %02d:%02d:%02d.%03d\n",yy,mm,dd,hh,nn,ss,ms)
  }'
}
milliseconds2date 1282157959073

应用实例:

计算今天的N天之后的日期

#!/bin/bash
function date2days {
  echo "$1 $2 $3" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}

function days2date {
  echo "$1" | awk '{
    a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d%02d%02d\n",yy,mm,dd)
  }'
}

year=`date +%Y`; month=`date +%m`; day=`date +%d`
days=`date2days $year $month $day`
N=5
let days-=$N
days2date $days

计算某天的N天之后的日期

#!/bin/bash
function date2days {
  echo "$1 $2 $3" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}

function days2date {
  echo "$1" | awk '{
    a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d%02d%02d\n",yy,mm,dd)
  }'
}

year=2010; month=01; day=20
days=`date2days $year $month $day`
let days+=5
days2date $days

计算上一个星期的全部日期

#!/bin/bash
function date2days {
  echo "$1 $2 $3" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}

function days2date {
  echo "$1" | awk '{
    a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d%02d%02d\n",yy,mm,dd)
  }'
}

function date2week {
  echo "$1 $2 $3" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    dow=(int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472629)%7;
    print dow
  }'
}

year=`date +%Y`; month=`date +%m`; day=`date +%d`
days=`date2days $year $month $day`
week=`date2week $year $month $day`
let dateEnd=$days-$week-1
let dateBegin=$dateEnd-6
for ((i=$dateBegin;i<=$dateEnd;i++)); do
  days2date $i
done

日期时间转换成毫秒

function date2milliseconds {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    j=j*86400+$4*3600+$5*60+$6
    print j$7
  }'
}

date2milliseconds `echo "2010-08-18 18:59:19.073" | /usr/xpg4/bin/awk -F'[:.-]+' '$1=$1'`

日期时间转换成秒

function date2seconds {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    j=j*86400+$4*3600+$5*60+$6
    print j
  }'
}

date2seconds `echo "2010-07-21 00:00:00" | sed 's/-/ /g;s/:/ /g'`

判断一个数字是否为合法日期

function date2days {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}

function days2date {
  echo "$1" | awk '{
    a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d%02d%02d\n",yy,mm,dd)
  }'
}

num1=20105050
num2=20101001
arg1=`echo "$num1" | sed -r 's/(....)(..)(..)/\1 \2 \3/g'`
arg2=`echo "$num2" | sed -r 's/(....)(..)(..)/\1 \2 \3/g'`
days1=`date2days $arg1`
date1=`days2date $days1`
days2=`date2days $arg2`
date2=`days2date $days2`
[ "$num1" -eq "$date1" ] && echo "$num1 is valid date" || echo "$num1 is invalid date"
[ "$num2" -eq "$date2" ] && echo "$num2 is valid date" || echo "$num2 is invalid date"

计算10分钟之前的时间

function date2minutes {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    j=j*1440+$4*60+$5
    print j
  }'
}
function minutes2date {
  echo "$1" | awk '{
    i=$1; nn=i%60; i=int(i/60); hh=i%24; dd=int(i/24); i=int(i/24);
    a=i+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d-%02d-%02d %02d:%02d\n",yy,mm,dd,hh,nn)
  }'
}

now=`date "+%Y-%m-%d %H:%M" | sed 's/-/ /g;s/:/ /g'`
minutes=`date2minutes $now`
let minutes-=10
minutes2date $minutes

计算指定日期和当前系统日期之家相差多少天

#!/bin/bash
function date2days {
  echo "$*" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}

echo "Enter your date:"
read input
InpuDays=$(date2days ${input:0:4} ${input:4:2} ${input:6:2})
SysDays=$(date2days `date +"%Y %m %d"`)
let result=$InpuDays-$SysDays
echo $result
#./test.sh
Enter your date:
20110605
25

上个星期周一的日期

#!/bin/bash
function date2days {
  echo "$1 $2 $3" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633;
    print j
  }'
}

function days2date {
  echo "$1" | awk '{
    a=$1+2472632; b=int((4*a+3)/146097); c=int((-b*146097)/4)+a; 
    d=int((4*c+3)/1461); e=int((-1461*d)/4)+c; m=int((5*e+2)/153);
    dd=-int((153*m+2)/5)+e+1; mm=int(-m/10)*12+m+3; yy=b*100+d-4800+int(m/10);
    printf ("%4d%02d%02d\n",yy,mm,dd)
  }'
}

function date2week {
  echo "$1 $2 $3" | awk '{
    z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3;
    dow=(int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472629)%7;
    print dow
  }'
}

year=`date +%Y`; month=`date +%m`; day=`date +%d`
days=`date2days $year $month $day`
week=`date2week $year $month $day`
let date=$days-$week-7
days2date $date

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。