A little help with time calculations

Steve Holden steve at holdenweb.com
Wed Oct 19 03:38:16 EDT 2005


iminal wrote:
> what i have so far is :
> 
>     # Get values needed to make time calculations
>     CT = input("input your chronometer time (ex. 07:21:46): ")
>     CE = input("input your chronometer correction (ex. 00:01:32): ")
>     CEfastslow = raw_input("is your chronometer correction fast or
> slow: ")
> 
>     #decide either to subtract or add CE from/to CT
>     if CEfastslow == "fast":
>         CEfastslow = CT - CE
>     if CEfastslow == "slow":
>         CEfastslow = CT + CE
> 
> but this just doesnt deal with the numbers in time format its acting
> like they are just regualr integers adding them up like regular numbers
> 
Well, how is the interpreter supposed to know that they are times? 
Remember that the Python language doesn't have times as a basic data 
type, and input(...) treats what you enter as Python data (unlike 
raw_input()).

> i am trying to figure out what u posted and it seems a little
> complicated im trying to add it in somehow and figure out exactly what
> its doing but still looking for a little easier of a way
> 
Well, the code I posted was untested, and I find two things wrong with 
it straight away: Firstly, it won't include leading zeros when 
converting seconds to a time, and secondly it puts the hours, minutes 
and seconds in the wrong order.

> thanks so far
> 
The idea, though, is to read strings lime "07:20:44" and convert them 
into something that Python *can* do arithmetic on. I defined a function, 
timetosecs, that would let you do this.

So your program should look something like:

# Put function definitions here ...
CT = raw_input("input your chronometer time (ex. 07:21:46): ")
CE = raw_input("input your chronometer correction (ex. 00:01:32): ")
CEfastslow = raw_input("is your chronometer correction fast or slow: ")

Tsecs = timetosecs(CT)
Esecs = timetosecs(CE)

#decide either to subtract or add CE from/to CT
if CEfastslow == "fast":
     CEfastslow = Tsecs - Esecs
if CEfastslow == "slow":
     CEfastslow = Tsecs + Esecs

print "New time:", secstotime(CEfastslow)

Hope this gets you a bit closer to a solution.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list