Yyymm unix date difference

#####DATE1=201609
#### DATE2=201508

How to calculate the difference between these two dates and get the output as a month counter i.e.

201609-201508=13month
+4
source share
3 answers

Calculating the time difference is usually a challenge even for one type of calendar (and there are many ). Many programming languages ​​have built-in support for date and time processing operations, including the calculation of time differences. But the most useful feature available in popular shells is a team datethat lacks this feature, unfortunately.

Therefore, we must write a script in another language or make some assumptions, such as the number of days in a year.

, Perl :

perl -e $(cat <<'PerlScript'
use Time::Piece;
my $t1 = Time::Piece->strptime($ARGV[0], '%Y%m');
my $t2 = Time::Piece->strptime($ARGV[1], '%Y%m');
printf "%d months\n", ($t1 - $t2)->months;
PerlScript
) 201609 201508

Time::Piece Time::Seconds, ,

24 , 7 , 365.24225 12 .

.

script:

DATE1=201609
DATE2=201508

printf '(%d - %d) / 2629744.2\n' \
  $(date -d ${DATE1}01 +%s) \
  $(date -d ${DATE2}01 +%s) | bc

2629744.2 - , .. 3600 * 24 * (365.24225 / 12).

. . , bc.

script 13. . , , Bash, Korn Zsh. , printf $( ... ):

months=$(printf '(%d - %d) / 2629744.2\n' \
  $(date -d ${DATE1}01 +%s) \
  $(date -d ${DATE2}01 +%s) | bc)

printf '%d - %d = %d months\n' $DATE1 $DATE2 $months
+2

-

[vipin@hadoop ~]$ cat time.awk
{
diff1=((substr($1,1,4)) - (substr($2,1,4)))
diff2=((substr($1,5,2)) - (substr($2,5,2)))
if(diff1 > 0)
        {
        if(diff2 > 0)
                {
                print (diff1+diff2)
                }
        else if (diff2 = 0)
                {
                print (diff1+diff2)
                }
                else
                {
                diff2=(12+((substr($1,5,2)-(substr($2,5,2)))))
                diff1=(diff1-1)
                print (diff1+diff2)
                }
        }
else if(diff1 == 0)
        {
        if(diff2 > 0)
                {
                print (diff1+diff2)
                }
        else if (diff2 == 0)
                {
                print (diff1+diff2)
                }
        else
                {
                 print "Argument 2 is greater than 1"
                }
        }
else
        {
        print "Argument 2 is greater than 1"
        }
}

-

[vipin@hadoop ~]$ cat time1.txt && awk -f time.awk time1.txt
201611 201601
10
[vipin@hadoop ~]$ cat time2.txt && awk -f time.awk time2.txt
201601 201611
Argument 2 is greater than 1
[vipin@hadoop ~]$ cat time3.txt && awk -f time.awk time3.txt
201511 201601
Argument 2 is greater than 1
[vipin@hadoop ~]$ cat time4.txt && awk -f time.awk time4.txt
201611 201611
0
0

- , , , , : YYYYMM 0:

$ ym2m() { 
    if [[ $1 =~ ^([0-9]{4})([0-9]{2})$ ]]; then 
        echo $(( 10#${BASH_REMATCH[1]} * 12 + 10#${BASH_REMATCH[2]} ))
    else 
        return 1
    fi
}

$ ym2m 201609
24201

$ ym2m 201508
24188

$ echo $(( $(ym2m 201609) - $(ym2m 201508) ))
13

:

  • bash 4.3 ( )
  • ym2m = > "--"
  • 10#number , , "08" "09" .
0

Source: https://habr.com/ru/post/1660622/


All Articles