Ksh Solaris Time calculation problem..Please help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh Solaris Time calculation problem..Please help
# 8  
Old 01-08-2010
Question bash shell in Solaris

Seems to me, I did this once before on bash shell on Solaris. DO you have any other flavors of date other than ksh?
# 9  
Old 01-08-2010
Okay .. I got most of the coding done

It's not that hard to code but it's pain to write a long code....

this script gives you the time difference in HH:MM format with input given as parm1 and parm2

Code:
#! /usr/bin/ksh
parm1="01/31/2010 22:14"
parm2="02/01/2010 22:37"

echo "From : $parm1"
echo "To   : $parm2"

m1=`echo $parm1 | cut -d "/" -f 1`
d1=`echo $parm1 | cut -d "/" -f 2`
y1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 1 | cut -c 3-4`
h1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 1`
mi1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 2`
s1=00
m2=`echo $parm2 | cut -d "/" -f 1`
d2=`echo $parm2 | cut -d "/" -f 2`
y2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 1 | cut -c 3-4`
h2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 1`
mi2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 2`
s2=00

if [ $y1 -eq $y2 ]; then
   if [ $m1 -eq $m2 ]; then
      if [ $d1 -eq $d2 ]; then
          seconds=$(echo "$h2*3600+$mi2*60+$s2-($h1*3600+$mi1*60+$s1)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                echo "$hh:$mm"
      else
      no_of_days_diff=`expr $d2 - $d1`
      h3=24
      mi3=00
      s3=00
"timecalc.ksh" 186 lines, 6059 characters
bash-3.00$ ./timecalc.ksh
From : 01/31/2010 22:14
To   : 02/01/2010 22:37
24:23
bash-3.00$ vi timecalc.ksh
"timecalc.ksh" 186 lines, 6059 characters
#! /usr/bin/ksh

parm1="01/31/2010 22:14"
parm2="02/01/2010 02:37"

echo "From : $parm1"
echo "To   : $parm2"

m1=`echo $parm1 | cut -d "/" -f 1`
d1=`echo $parm1 | cut -d "/" -f 2`
y1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 1 | cut -c 3-4`
h1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 1`
mi1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 2`
s1=00
m2=`echo $parm2 | cut -d "/" -f 1`
d2=`echo $parm2 | cut -d "/" -f 2`
y2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 1 | cut -c 3-4`
h2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 1`
mi2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 2`
s2=00

if [ $y1 -eq $y2 ]; then
   if [ $m1 -eq $m2 ]; then
      if [ $d1 -eq $d2 ]; then
          seconds=$(echo "$h2*3600+$mi2*60+$s2-($h1*3600+$mi1*60+$s1)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                echo "$hh:$mm"
      else
      no_of_days_diff=`expr $d2 - $d1`
      h3=24
      mi3=00
"timecalc.ksh" 187 lines, 6060 characters
bash-3.00$ ./timecalc.ksh
From : 01/31/2010 22:14
To   : 02/01/2010 02:37
4:23
bash-3.00$ cat timecalc.ksh
#! /usr/bin/ksh

parm1="01/31/2010 22:14"
parm2="02/01/2010 02:37"

echo "From : $parm1"
echo "To   : $parm2"

m1=`echo $parm1 | cut -d "/" -f 1`
d1=`echo $parm1 | cut -d "/" -f 2`
y1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 1 | cut -c 3-4`
h1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 1`
mi1=`echo $parm1 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 2`
s1=00
m2=`echo $parm2 | cut -d "/" -f 1`
d2=`echo $parm2 | cut -d "/" -f 2`
y2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 1 | cut -c 3-4`
h2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 1`
mi2=`echo $parm2 | cut -d "/" -f 3 | cut -d " " -f 2 | cut -d ":" -f 2`
s2=00

if [ $y1 -eq $y2 ]; then
   if [ $m1 -eq $m2 ]; then
      if [ $d1 -eq $d2 ]; then
          seconds=$(echo "$h2*3600+$mi2*60+$s2-($h1*3600+$mi1*60+$s1)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                echo "$hh:$mm"
      else
      no_of_days_diff=`expr $d2 - $d1`
      h3=24
      mi3=00
      s3=00
      seconds=$(echo "$h3*3600+$mi3*60+$s3-($h1*3600+$mi1*60+$s1)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                diff1_hh=$hh
                diff1_mm=$mm

                h4=00
                mi4=00
                s4=00
          seconds=$(echo "$h2*3600+$mi2*60+$s2-($h4*3600+$mi4*60+$s4)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                diff2_hh=$hh
                diff2_mm=$mm

                Total_hh=`expr $diff1_hh + $diff2_hh`
                Total_mm=`expr $diff1_mm + $diff2_mm`
                        if [ $Total_mm -gt 59 ]; then
                           Add_Min=`expr $Total_mm % 60`
                           Total_mm=$Add_Min
                           Total_hh=`expr $Total_hh + 1`
                        fi
              if [ $no_of_days_diff -gt 1 ]; then
                   Multi=`expr $no_of_days_diff - 1`
                   Add_hh=`expr $Multi \* 24`
                   Total_hh=`expr $Total_hh + $Add_hh`
              fi
              echo "$Total_hh:$Total_mm"
      fi #end of day check fi
   else
     even_flag=`expr $m1 % 2`
     if [ $even_flag -eq 0 ]; then
        m1_even=1
     else
        m1_even=0
     fi
     if [ $m1 -eq 8 ]; then
        m1_even=0
     fi
          if [ $m1 -eq 2 ]; then
             is_feb=1
             if [ `expr $m1 % 4` -eq 0 ]; then
                 is_leap=1
             else
                 is_leap=0
             fi
          else
             is_feb=0
          fi
      if [ $m1_even -eq 1 ]; then
         if [ $is_feb -eq 1 ]; then
            if [ $is_leap -eq 1 ]; then
               days_add=`expr 29 - $d1`
            else
               days_add=`expr 28 - $d1`
            fi
         else
           days_add=`expr 30 - $d1`
         fi
      else
         days_add=`expr 31 - $d1`
      fi

      Total_days_add=`expr $days_add + $d2`
      #We have the total number of days in month difference gape
      #Now count the time difference which does not include the days gape
      h3=24
      mi3=00
      s3=00
      seconds=$(echo "$h3*3600+$mi3*60+$s3-($h1*3600+$mi1*60+$s1)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                diff1_hh=$hh
                diff1_mm=$mm

                h4=00
                mi4=00
                s4=00
          seconds=$(echo "$h2*3600+$mi2*60+$s2-($h4*3600+$mi4*60+$s4)" | bc)
          if [ "$seconds" -lt 0 ] ; then
             ((seconds=seconds+86400))
          fi
                hh=$(expr $seconds / 3600)
                mm=$(expr \( $seconds - $hh \* 3600 \) / 60)
                ss=$(expr $seconds - $hh \* 3600 - $mm \* 60)
                if [ $hh -lt 10 ]; then
                     hh=0$hh
                fi
                if [ $mm -lt 10 ]; then
                     mm=0$mm
                fi
                diff2_hh=$hh
                diff2_mm=$mm

                Total_hh=`expr $diff1_hh + $diff2_hh`
                Total_mm=`expr $diff1_mm + $diff2_mm`
                        if [ $Total_mm -gt 59 ]; then
                           Add_Min=`expr $Total_mm % 60`
                           Total_mm=$Add_Min
                           Total_hh=`expr $Total_hh + 1`
                        fi
              if [ $Total_days_add -gt 1 ]; then
                   Multi=`expr $no_of_days_diff - 1`
                   Add_hh=`expr $Multi \* 24`
                   Total_hh=`expr $Total_hh + $Add_hh`
              fi
              echo "$Total_hh:$Total_mm"

      #End of month fi is in the next line
   fi
else
   echo ""
fi


Output :

Code:
From : 01/31/2010 22:14
To   : 02/01/2010 02:37
04:23



---------- Post updated at 04:06 PM ---------- Previous update was at 04:06 PM ----------

Thank you all for your help ....

Last edited by radoulov; 01-08-2010 at 05:08 PM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Time calculation

Hi Gurus, I need to get one hour before time is yyyymmddhh format. ex. date +"%Y%m%d%H" gives 2017052814 but I need 2017052813 Thankx Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: nalakaatslt
6 Replies

2. Shell Programming and Scripting

Solaris Time Problem

Hi all, I am printing yesterday date in solaris using command: TZ=GMT+24 date +%b_%d_%Ybut at 01:00 AM on jan 11, it is printing: Anyone please explain. (1 Reply)
Discussion started by: anand2308
1 Replies

3. Shell Programming and Scripting

time calculation in ksh script

I"m trying to calculate the duration of of backup within a ksh shell script but I get an error. #!/bin/ksh STTIM=`date '+%T'` EDTIM=`date '+%T'` .... .... echo "DURATION OF BACKUP: $((EDTIM - STTIM))" (5 Replies)
Discussion started by: Bperl1967
5 Replies

4. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

5. Shell Programming and Scripting

Date time calculation

hello guys, I had been to many forums and many topics in this site as well for my question but did not get any solution. My question is how i can get y'day date with time stamp today is 20100729103819 and i am looking for output as 20100728103819. in simple words as we do in oracle sysdate-1... (4 Replies)
Discussion started by: lokaish23
4 Replies

6. Shell Programming and Scripting

Time stamp calculation

Hi all; I'm relatively new to scripting,I am working on a monitoring script.....where in i have to write subroutine which does the follows: It will check the time stamp of a file ( Oracle remarchive files) and compare it with existing time.If the time difference happen to be more than 90... (6 Replies)
Discussion started by: maverick_here
6 Replies

7. UNIX for Dummies Questions & Answers

Time Calculation

I have a file with over 100,000 lines of data with looking to compare times of about 2000 lines to get a total time of a process. The lines of unique data are as follows. FINER: CacSoapServer:reserveNetworkResource got the sessionID and INFO: Created CAC session ID The command... (5 Replies)
Discussion started by: bpfoster76
5 Replies

8. Shell Programming and Scripting

Time difference calculation

Hi Team, I am currently in the process of writing a script which will take a filename in the format SKADEV.0.db2.NODE0000.CATN0000.20080714231015.001 where the sixth string(with "." as the seperator) is the time stamp of the time of creation of the file. now here is my issue . I need to be... (2 Replies)
Discussion started by: Segwar
2 Replies

9. UNIX for Dummies Questions & Answers

Average completion time calculation?

I've been trying all night to come up with a script that will take a file that contains job completion times like this as input: 18:30 17:45 16:39 18:01 17:50 ... and figure the Average completion time. I've tried several things, and I just can't seem to get it to figure correctly. I'm... (5 Replies)
Discussion started by: Seawall
5 Replies

10. UNIX for Advanced & Expert Users

time calculation

Hi, I have start time as a string like 06:04:01 and end time like 06:05:01 i need do a simple math to get the duration. What is the best way to do this in Korn Shell scripting? Thanks (2 Replies)
Discussion started by: liux99
2 Replies
Login or Register to Ask a Question