A little help with time calculations

Discussion in 'Python' started by iminal, Oct 18, 2005.

  1. iminal

    iminal Guest

    I am trying to make a very simple program and am very new to the whole
    programming thing. my program is supposed to ask a user for any time in
    the for format XX:XX:XX and then ask for a time corrrection to add or
    subtract to this. my only problem is that once the user inputs the time
    and the correction its adding it like it was 100 not to 60 any help?
     
    iminal, Oct 18, 2005
    #1
    1. Advertising

  2. iminal wrote:
    > I am trying to make a very simple program and am very new to the whole
    > programming thing. my program is supposed to ask a user for any time in
    > the for format XX:XX:XX and then ask for a time corrrection to add or
    > subtract to this. my only problem is that once the user inputs the time
    > and the correction its adding it like it was 100 not to 60 any help?


    Without code, nobody can help you.

    Regards,

    Diez
     
    Diez B. Roggisch, Oct 18, 2005
    #2
    1. Advertising

  3. iminal

    Steve Holden Guest

    iminal wrote:
    > I am trying to make a very simple program and am very new to the whole
    > programming thing. my program is supposed to ask a user for any time in
    > the for format XX:XX:XX and then ask for a time corrrection to add or
    > subtract to this. my only problem is that once the user inputs the time
    > and the correction its adding it like it was 100 not to 60 any help?
    >

    If you're new to programming you may not yet have realised that many
    problems come down to finding appropriate representations for things.

    Since you want to do arithmetic on times, why not store them as seconds?
    Then you just need to work out how to convert times to seconds, and
    seconds to times - Python's arithmetic will do the rest.

    See if you can make anything of these functions (which I haven't tested,
    so you're allowed to complain if they don't work ;-):

    def timetosecs(s):
    hms = s.split(":") # [hh, mm, ss]
    secs = 0
    for t in hms:
    secs = secs * 60 + int(t)
    return secs

    def secstotime(secs):
    hms = []
    while secs:
    hms.append(str(secs % 60))
    secs = secs // 60
    return ":".join(hms)

    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/
     
    Steve Holden, Oct 18, 2005
    #3
  4. iminal

    iminal Guest

    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

    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

    thanks so far
     
    iminal, Oct 18, 2005
    #4
  5. iminal

    Steve Holden Guest

    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/
     
    Steve Holden, Oct 19, 2005
    #5
  6. iminal

    iminal Guest

    thanks
     
    iminal, Oct 19, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThaDoctor
    Replies:
    3
    Views:
    387
    Alan Woodland
    Sep 28, 2007
  2. Ross
    Replies:
    8
    Views:
    247
    Steve Holden
    Jan 8, 2009
  3. androtech

    Week calculations (Date/Time)

    androtech, Jan 15, 2004, in forum: ASP General
    Replies:
    2
    Views:
    268
    dlbjr
    Jan 15, 2004
  4. Replies:
    8
    Views:
    141
    Dr John Stockton
    Sep 17, 2005
  5. Daniel
    Replies:
    1
    Views:
    214
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page