Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Adding time to date time in UNIX shell scipting

$
0
0
I needed some help in adding a duration (in seconds) to a start time (in hhmmss format) and a start date (in mmddyy format) in order to get an end date and end time. The concept of a leap year is also to be considered while incrementing the day. The code/ function that I have formed so far is as follows, but there appears to be an error in the function calling. I also wanted to check if this script designed by me is full proof.

Code:

MIN_TIME = start_time;
                        secs=substr(MIN_TIME,5,2);
            mins=substr(MIN_TIME,3,2);
            hrs=substr(MIN_TIME,1,2);
            durn=p;
            d=substr(MIN_DATE,3,2);
            m=substr(MIN_DATE,1,2);
            y=substr(MIN_DATE,5,2);
            date=d;
            mone=m;
            ye=y;
            durn=p;

 function incrementday (d, m, y)

      {  if(d<28)                                      #increment day
        { date=d+1;
        mone=m;
        ye=y;}

        else if(d == 28)
        { if(m==2)
            { if(y%4==0)
                {date=d+1; mone=m; ye=y;}
          else {date=1;
            mone=3;
                ye=y;}}
        else {date = d+1;
        mone=m;
        ye=y;}}
   
    else if(d==29)
    { if(m==2)
        {date=1;
        mone=03;
        ye=y;}
      else
          {date=d+1;
          mone=m;
          ye=y;}}

    else if(d==30)
    { if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
        {date=d+1;
        ye=y;
        mone=m;}
    else {date=01;
        mone=m+1;
        ye=y;}}

    else {if(m==12)
        {date=01;
        mone=01;
        ye=y+1;
        }
        else {date=01;
            mone=m+1; ye=y;
        }}  }                            #incremented day
         

               
                        sece=secs+durn;
                       
                        if ((sece>=60) && (sece<3600))  # greater than a minute but less than an hour
                          { mine=mins+sece/60;
                                if (mine>=60)
                                    {hre=hrs+1;
                                            if (hre>=24)
                                                {hre=hre-24;
                                                                                                incrementday($d $m $y); }
       
                                    mine=mine-60;}
                            sece=sece%60;}

                        else if ((sece>=3600) && (sece<86400))  #greater than an hour but less than a day
                          { mine=mins+sece/60;
                            if(mine>=60)
                              {hre=hrs+mine/60;
                                if(hre>=24)
                                    {hre=hre-24;
                                                              incrementday($d $m $y); }       
                                mine=mine%60;}
                            sece=sece%60;}

                        else if (sece>=86400)    #greater than a day
                          {mine=mins+sece/60;
                            if (mine>=60)
                                {hre=hrs+mine/60;
                                    {hre=hre-24;
                                                              incrementday($d $m $y);}
                                mine=mine%60;
                          sece=sece%60;}


Viewing all articles
Browse latest Browse all 16232

Trending Articles