[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calculating elapsed time

Clement Ow

4/16/2008 9:56:00 AM

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. ;)
--
Posted via http://www.ruby-....

7 Answers

Iñaki Baz Castillo

4/16/2008 10:05:00 AM

0

MjAwOC80LzE2LCBDbGVtZW50IE93IDxjbGVtZW50Lm93QGFzaWEuYm5wcGFyaWJhcy5jb20+Ogo+
ICBBbnkgYWx0ZXJuYXRpdmUgbWV0aG9kIG9yIGFueSBjb3JyZWN0aW9ucyB0byB0aGUgbWV0aG9k
IGFyZSB3ZWxjb21lLiA7KQoKCnJlcXVpcmUgJ2JlbmNobWFyaycKCnRpbWUgPSBCZW5jaG1hcmsu
cmVhbHRpbWUgewogZG9fYWxsX3N0dWZmCn0KCnB1dHMgIkVsYXBzZWQgdGltZTogI3t0aW1lfSIK
Cgo7KQoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0Pgo=

Jesús Gabriel y Galán

4/16/2008 10:25:00 AM

0

On Wed, Apr 16, 2008 at 11:56 AM, Clement Ow
<clement.ow@asia.bnpparibas.com> wrote:
> 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.

Rob Biedenharn

4/16/2008 2:47:00 PM

0

On Apr 16, 2008, at 6:24 AM, Jes=FAs Gabriel y Gal=E1n wrote:
> On Wed, Apr 16, 2008 at 11:56 AM, Clement Ow
> <clement.ow@asia.bnpparibas.com> wrote:
>> I have a program where i put the time at the beginning:
>> puts $t1=3DTime.now
>>
>> and then after running some commands, i put the time again:
>> puts $t2=3DTime.now
>>
>> and then have a method to calculate the time elapsed:
>> def calTime
>> starttime_sec=3D $t1.strftime("%S").to_i
>> starttime_min=3D $t1.strftime("%M").to_i
>> endtime_sec=3D $t2.strftime("%S").to_i
>> endtime_min=3D $t2.strftime("%M").to_i
>> diff_sec=3D endtime_sec - starttime_sec
>> diff_sec1=3D diff_sec.to_s
>> diff_min=3D endtime_min - starttime_min
>> diff_min1=3D 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 =20
>> value
>> like -49 for the Min value, which is obviously not what we want..
>>
>> Any alternative method or any corrections to the method are =20
>> welcome. ;)
>
> 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://agileconsult...
Rob@AgileConsultingLLC.com=

Jesús Gabriel y Galán

4/16/2008 3:02:00 PM

0

On Wed, Apr 16, 2008 at 4:47 PM, Rob Biedenharn
<Rob@agileconsultingllc.com> wrote:

> 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.

Clement Ow

4/17/2008 1:39:00 AM

0

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

Jesús Gabriel y Galán wrote:
> On Wed, Apr 16, 2008 at 4:47 PM, Rob Biedenharn
> <Rob@agileconsultingllc.com> wrote:
>
>> 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?
>> 4:49.70
>> => nil
>
> I always forget about divmod, it's a cool little method :-)
>
> Jesus.


--
Posted via http://www.ruby-....

Daniel Finnie

4/17/2008 2:21:00 AM

0

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:
>> "The number is: %.2f" % 1.23456
=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=

Clement Ow

4/17/2008 2:34:00 AM

0


That's some information, thanks, dan!
--
Posted via http://www.ruby-....