calcul on time 
Author Message
 calcul on time

Hi,

Thanks for your aswers about "calcul on date".

Now I have to compute the elapsed time in minutes between 2 given times.
(hh:mm:ss)
The 2 given times are string.

  eg. time_1 is 20:57:10
      time_2 is 21:05:22
      the result should be 00:08:12

  eg. time_1 is 23:59:02
      time_2 is 00:01:05
      the result should be 00:02:03

Any idea ?

Many thanks in advance.

Pierre.



Wed, 02 May 2001 03:00:00 GMT  
 calcul on time
On Sat, 14 Nov 1998 09:33:55 +0100, Pierre Bachelart <"pierre

Quote:

>Hi,

>Thanks for your aswers about "calcul on date".

>Now I have to compute the elapsed time in minutes between 2 given times.
>(hh:mm:ss)
>The 2 given times are string.

>  eg. time_1 is 20:57:10
>      time_2 is 21:05:22
>      the result should be 00:08:12

>  eg. time_1 is 23:59:02
>      time_2 is 00:01:05
>      the result should be 00:02:03

BEGIN {
  print time_diff("10:00:00","11:00:00")
  print time_diff("10:10:10","11:11:11")
  print time_diff("20:57:10","21:05:22")
  print time_diff("23:59:02","00:01:05")
Quote:
}

function time_diff ( time1, time2,      a1, diff ) {
  printf ( "%s - %s = ", time2, time1 )       # trace only
  split(time1,a1,":")
  time1 = a1[1]*3600+a1[2]*60+a1[3]     # time1 in seconds
  split(time2,a1,":")
  time2 = a1[1]*3600+a1[2]*60+a1[3]     # time2 in seconds
  if ( time2 < time1 ) time2 += 86400        # endtime rolled into next day
  diff = time2 - time1
  return sprintf("%02d:%02d:%02d", diff/3600, (diff/60)%60, diff%60)

Quote:
}

--
Steve


Wed, 02 May 2001 03:00:00 GMT  
 calcul on time
This works using gawk 3.0.3. It should also work with nawk, mawk or tawk
but not "old awk" which did not have user-defined functions.

BEGIN {

  time_1="20:57:10"
  time_2="21:05:22"
  print hms(time_1,time_2)

  time_1="23:59:02"
  time_2="00:01:05"
  print hms(time_1,time_2)

Quote:
}

function hms(t1,t2,  s1,s2,ds,h,m,s) {
  split(t1,tmp,":")
  s1=tmp[1]*3600+tmp[2]*60+tmp[3]
  split(t2,tmp,":")
  s2=tmp[1]*3600+tmp[2]*60+tmp[3]
  ds=s2-s1
  while (ds<0) ds+=86400
  h=int(ds/3600)
  m=int((ds-h*3600)/60)
  s=ds-3600*h-60*m
  return sprintf("%02d:%02d:%02d",h,m,s)

Quote:
}

gawk -f hms.awk
00:08:12
00:02:03



Quote:
>Hi,

>Thanks for your aswers about "calcul on date".

>Now I have to compute the elapsed time in minutes between 2 given times.
>(hh:mm:ss)
>The 2 given times are string.

>  eg. time_1 is 20:57:10
>      time_2 is 21:05:22
>      the result should be 00:08:12

>  eg. time_1 is 23:59:02
>      time_2 is 00:01:05
>      the result should be 00:02:03

>Any idea ?

>Many thanks in advance.

>Pierre.

--
Alan Linton


Wed, 02 May 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. calcul on date

2. Calcul matriciel

3. the calcul of 3 of a number

4. Bug in time.c (was Time.times problems)

5. time and scheduling (was: bug report: [ #447945 ] time.time() is not non-decreasing)

6. time zones, daylight saving time, and universal time

7. Manugistics run-time [was: STSC run-time]

8. GMT time vs. local time

9. Comparing system time and log file time

10. converting time in a string to a time field

11. Create Time / date or Modified Time / date of a txt file

12. Run-time & Design-time window size problem

 

 
Powered by phpBB® Forum Software