nicolas-fricke / transfair

[DEPRECATED] Second, Rails-based approach to a web application for the Transfair-Project
https://www.transfair.co
3 stars 3 forks source link

conversion hh:mm:ss into number, complicated? #23

Open briodf opened 12 years ago

briodf commented 12 years ago

is it easy to convert 01:30:00 in 90min?

If yes, the input through the form would be in hh:mm:sss, but to calculate the price of the work I will do:

If jobprice=0: price= price/min * number_audio_min

else: price=jobprice (given by transfair sales people)

tiredpixel commented 12 years ago

To convert from seconds into H:M:S is straightforward using the Time:

seconds = 90.minutes
hms = Time.at(seconds).utc.strftime("%H:%M:%S")

It is important to remember to specify utc.

To convert the other way around, though, is more tricky. You could split based on the colons, multiply as matrices, and sum the result:

hms = "01:30:00"
seconds = hms.split(":").zip([1.hour, 1.minute, 1.second]).map { |i, j| i.to_i * j }.sum

Alternatively, you could use the Time with reference to a fixed point (this is necessary, to prevent problems with crossing days during the calculation - unlikely, but possible):

hms = "01:30:00"
seconds = Time.parse("2001-01-01 #{hms}") - Time.parse("2001-01-01")

The date chosen doesn't matter (within reason - don't go before 1970, for instance! ;) ). If you're paranoid, you could specify "2001-01-01 00:00:00" as the latter argument. You also might like to verify the behaviour of not using utc - shouldn't matter, so long as the timezone doesn't change; otherwise, add .utc to each.

Once in seconds, it's easy to convert to minutes. However, you might like to consider using Time, instead, and manipulating that as required.

markus-hinsche commented 12 years ago

Cool thanks I 'll try that soon:)