Calculating elapsed time

C

Clement Ow

I have a program where i put the time at the beginning:
puts $t1=Time.now

and then after running some commands, i put the time again:
puts $t2=Time.now

and then have a method to calculate the time elapsed:
def calTime
starttime_sec= $t1.strftime("%S").to_i
starttime_min= $t1.strftime("%M").to_i
endtime_sec= $t2.strftime("%S").to_i
endtime_min= $t2.strftime("%M").to_i
diff_sec= endtime_sec - starttime_sec
diff_sec1= diff_sec.to_s
diff_min= endtime_min - starttime_min
diff_min1= diff_min.to_s
puts "\nElapsed time:\n "+ "Min-> " +diff_min1 + " Sec-> "+ diff_sec1

However, the calculation has limitations because if the start time is
say, 4:50 and the end time is 5:10, it will return an errorneous value
like -49 for the Min value, which is obviously not what we want..

Any alternative method or any corrections to the method are welcome. ;)
 
I

Iñaki Baz Castillo

MjAwOC80LzE2LCBDbGVtZW50IE93IDxjbGVtZW50Lm93QGFzaWEuYm5wcGFyaWJhcy5jb20+Ogo+
ICBBbnkgYWx0ZXJuYXRpdmUgbWV0aG9kIG9yIGFueSBjb3JyZWN0aW9ucyB0byB0aGUgbWV0aG9k
IGFyZSB3ZWxjb21lLiA7KQoKCnJlcXVpcmUgJ2JlbmNobWFyaycKCnRpbWUgPSBCZW5jaG1hcmsu
cmVhbHRpbWUgewogZG9fYWxsX3N0dWZmCn0KCnB1dHMgIkVsYXBzZWQgdGltZTogI3t0aW1lfSIK
Cgo7KQoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0Pgo=
 
J

Jesús Gabriel y Galán

I have a program where i put the time at the beginning:
puts $t1=Time.now

and then after running some commands, i put the time again:
puts $t2=Time.now

and then have a method to calculate the time elapsed:
def calTime
starttime_sec= $t1.strftime("%S").to_i
starttime_min= $t1.strftime("%M").to_i
endtime_sec= $t2.strftime("%S").to_i
endtime_min= $t2.strftime("%M").to_i
diff_sec= endtime_sec - starttime_sec
diff_sec1= diff_sec.to_s
diff_min= endtime_min - starttime_min
diff_min1= diff_min.to_s
puts "\nElapsed time:\n "+ "Min-> " +diff_min1 + " Sec-> "+ diff_sec1

However, the calculation has limitations because if the start time is
say, 4:50 and the end time is 5:10, it will return an errorneous value
like -49 for the Min value, which is obviously not what we want..

Any alternative method or any corrections to the method are welcome. ;)

Alternative:

irb(main):001:0> start = Time.now
=> Wed Apr 16 12:06:23 +0200 2008
irb(main):003:0> finish = Time.now
=> Wed Apr 16 12:07:24 +0200 2008
irb(main):005:0> elapsed = (finish - start).to_i
=> 65
irb(main):012:0> mins = elapsed / 60
=> 1
irb(main):013:0> secs = elapsed % 60
=> 5

Jesus.
 
R

Rob Biedenharn

Alternative:

irb(main):001:0> start =3D Time.now
=3D> Wed Apr 16 12:06:23 +0200 2008
irb(main):003:0> finish =3D Time.now
=3D> Wed Apr 16 12:07:24 +0200 2008
irb(main):005:0> elapsed =3D (finish - start).to_i
=3D> 65
irb(main):012:0> mins =3D elapsed / 60
=3D> 1
irb(main):013:0> secs =3D elapsed % 60
=3D> 5

Jesus.


or:
irb> start =3D Time.now
=3D> Wed Apr 16 10:41:03 -0400 2008
irb> finish =3D Time.now
=3D> Wed Apr 16 10:45:53 -0400 2008
irb> elapsed =3D finish.to_f - start.to_f # note to_f to keep =20
fractional seconds
=3D> 289.695813894272
irb> mins, secs =3D elapsed.divmod 60.0
=3D> [4, 49.6958138942719]
irb> puts("%3d:%04.2f"%[mins.to_i, secs])
4:49.70
=3D> nil

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)=
 
J

Jesús Gabriel y Galán

or:
irb> start = Time.now
=> Wed Apr 16 10:41:03 -0400 2008
irb> finish = Time.now
=> Wed Apr 16 10:45:53 -0400 2008
irb> elapsed = finish.to_f - start.to_f # note to_f to keep fractional
seconds

Yep, I assumed he wanted to stop at the seconds level.
=> 289.695813894272
irb> mins, secs = elapsed.divmod 60.0
=> [4, 49.6958138942719]
irb> puts("%3d:%04.2f"%[mins.to_i, secs])
4:49.70
=> nil

I always forget about divmod, it's a cool little method :)

Jesus.
 
C

Clement Ow

Thanks guys of the prompt reply! However, i'll need help the
explanations though.. Really appreciate that ;)

Jesús Gabriel y Galán said:
or:
irb> start = Time.now
=> Wed Apr 16 10:41:03 -0400 2008
irb> finish = Time.now
=> Wed Apr 16 10:45:53 -0400 2008
irb> elapsed = finish.to_f - start.to_f # note to_f to keep fractional
seconds

Yep, I assumed he wanted to stop at the seconds level.
=> 289.695813894272
irb> mins, secs = elapsed.divmod 60.0
=> [4, 49.6958138942719]
irb> puts("%3d:%04.2f"%[mins.to_i, secs])
how this line work? "%3d" and %04.2f" means selecting min and secs
respectively?
 
D

Daniel Finnie

Hi,

Ruby substitutes the elements in the array for the percent signs in
the string. The "3d" and "04.2f" tell Ruby how to format those
numbers:=3D> "The number is: 1.23"

The documentation for sprintf has more info:
daniel@daniel-desktop:~/lightconsole$ ri sprintf
--------------------------------------------------------- Kernel#sprintf
format(format_string [, arguments...] ) =3D> string
sprintf(format_string [, arguments...] ) =3D> string
------------------------------------------------------------------------
Returns the string resulting from applying format_string to any
additional arguments. Within the format string, any characters
other than format sequences are copied to the result. A format
sequence consists of a percent sign, followed by optional flags,
width, and precision indicators, then terminated with a field type
character. The field type controls how the corresponding sprintf
argument is to be interpreted, while the flags modify that
interpretation. The field type characters are listed in the table
at the end of this section. The flag characters are:

Flag | Applies to | Meaning
---------+--------------+-----------------------------------------
space | bdeEfgGiouxX | Leave a space at the start of
| | positive numbers.
---------+--------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+--------------+-----------------------------------------
# | beEfgGoxX | Use an alternative format. For the
| | conversions `o', `x', `X', and `b',
| | prefix the result with ``0'', ``0x'', ``0X=
'',
| | and ``0b'', respectively. For `e',
| | `E', `f', `g', and 'G', force a decimal
| | point to be added, even if no digits follo=
w.
| | For `g' and 'G', do not remove trailing ze=
ros.
---------+--------------+-----------------------------------------
+ | bdeEfgGiouxX | Add a leading plus sign to positive number=
s.
---------+--------------+-----------------------------------------
- | all | Left-justify the result of this conversion=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,293
Latest member
BonnieHamb

Latest Threads

Top