Several small functions used by Shell to convert and calculate date and time using Epoch

Source: Internet
Author: User
Tags date1


When you encounter a system where the date command is not powerful, you can try these few functions.

#Date to days
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'`
Copy code
#Days to date
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
Copy code
#Date to minute
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'`
Copy code
#Minute to date
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
Copy code
#Date to seconds
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'`
Copy code
#Seconds to date
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
Copy code
#Date to millisecond
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'`
Copy code
#Ms to date
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
Copy code
Applications:

Calculate today's date after N days


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

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

  17. year=`date +%Y`; month=`date +%m`; day=`date +%d`
  18. days=`date2days $year $month $day`
  19. N=5
  20. let days-=$N
  21. days2date $days
Calculate the date after N days of a day


#!/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


Calculate all dates of the previous week


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

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

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

  24. year=`date +%Y`; month=`date +%m`; day=`date +%d`
  25. days=`date2days $year $month $day`
  26. week=`date2week $year $month $day`
  27. let dateEnd=$days-$week-1
  28. let dateBegin=$dateEnd-6
  29. for ((i=$dateBegin;i<=$dateEnd;i++)); do
  30.     days2date $i
  31. done

Convert date time to milliseconds



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

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

Convert date and time to seconds



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

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

Determine if a number is a legal date



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

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

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

Calculate the time before 10 minutes



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

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

Calculate the number of days between the specified date and the current system date



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

  9. echo "Enter your date:"
  10. read input
  11. InpuDays=$(date2days ${input:0:4} ${input:4:2} ${input:6:2})
  12. SysDays=$(date2days `date +"%Y %m %d"`)
  13. let result=$InpuDays-$SysDays
  14. echo $result

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

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

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

  24. year=`date +%Y`; month=`date +%m`; day=`date +%d`
  25. days=`date2days $year $month $day`
  26. week=`date2week $year $month $day`
  27. let date=$days-$week-7
  28. days2date $date



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.