zoukankan      html  css  js  c++  java
  • Linux 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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",yy,mm,dd)
        }'
    }
    
    year=`date +%Y`; month=`date +%m`; day=`date +%d`
    days=`date2days $year $month $day`
    N=5
    let days-=$N
    days2date $days
    

      

     其他:http://bbs.chinaunix.net/thread-1772999-1-1.html 

      

      

      

      

      

      

  • 相关阅读:
    Java的静态块与实例块(转)
    Programming Ability Test学习 1031. Hello World for U (20)
    Programming Ability Test学习 1011. World Cup Betting (20)
    Programming Ability Test学习 1027. Colors in Mars (20)
    Programming Ability Test学习 1064. Complete Binary Search Tree (30)
    Programming Ability Test学习 1008. Elevator (20)
    【maven详解-生命周期】Maven的生命周期和插件
    【maven详解-插件】maven插件学习之源码插件Source Xref
    $(document).ready(){}、$(fucntion(){})、(function(){})(jQuery)onload()的区别
    你还没真的努力过,就轻易输给了懒惰
  • 原文地址:https://www.cnblogs.com/sure/p/3714792.html
Copyright © 2011-2022 走看看