3 Replies - 1407 Views - Last Post: 15 February 2013 - 12:40 AM Rate Topic: -----

#1 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Finding elapsed time in minutes

Posted 14 February 2013 - 01:12 AM

Hello, I am trying to find the elapsed time between two times that are in a specific format. They are in HHMM format.

NOTE: I am not trying to find the elapsed time of my program running. I found tons of examples of that on Google, but it didn't really help me out.

Here is my code to receive and validate the times (to make sure they are real times):

import time

timex = '0103'
timex2 = '2003'
timeResult = None

for format in ['%H%M']:
  try:
    timeResult = time.strptime(timex, format)
    timeResult2 = time.strptime(timex2, format)
  except:
    pass

if timeResult is None:
  print 'Malformed time.'
else:
  print 'time is fine.'



If I try to do a command such as:
elapsedTime = timeResult2 - timeResult


...it will tell me that - is an invalid operation on a string.

Is there any way that I can extract the individual %H and %M values from the original string?

Is This A Good Question/Topic? 0
  • +

Replies To: Finding elapsed time in minutes

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Finding elapsed time in minutes

Posted 14 February 2013 - 02:56 AM

Why does this require the time module, as opposed to subtraction and a little logic?

Anyway if you have a string of the format "HHMM" called time1,
time1[:2]
will give you "HH" and
time1[2:]
will give you "MM". But perhaps I am misunderstanding the question.

If you want to perform subtraction you will have to convert the strings to ints using int("string").

-Mek
Was This Post Helpful? 0
  • +
  • -

#3 Static Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Re: Finding elapsed time in minutes

Posted 14 February 2013 - 11:30 AM

Major facepalm. I always forget about slicing in python. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Finding elapsed time in minutes

Posted 15 February 2013 - 12:40 AM

It happens. Also the problem is definitely doable with the time module as well, and is shorter in fact. I just think that you learn more from doing it the manual way.

This is how to do it with the time module:
import time
def get_time(time1,time2):
    #I set the year to default to "2013" as time.mktime can fail if
    #year is before a system's epoch date.
    first = time.mktime(time.strptime("2013"+time1, "%Y%H%M"))
    second = time.mktime(time.strptime("2013"+time2, "%Y%H%M"))
    total_min = int(abs((first-second)/60.0))
    print("{} minutes have elapsed (using time module).\n".format(total_min))

get_time("0103","2003")

The above code of course doesn't perform any error checking on the inputs so if they aren't in the right format to begin with it won't work right; but that is easily enough added too.

-Mek
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1