0

I am trying to add minutes to another timestamp. My code looks like below

from datetime import datetime
from datetime import timedelta 

start_time = '06:52:00'
easy_pace_time = '00:08:15'
tempo_pace_time = '00:07:12'
easy_pace_miles = 2
tempo_pace_miles = 3 
FMT = '%H:%M:%S'

easy_strip = datetime.strptime( easy_pace_time, FMT)
easy_strip_seconds = (easy_strip.minute * 60) + easy_strip.second
easy_pace_total = easy_pace_miles * easy_strip_seconds

tempo_strip = datetime.strptime( tempo_pace_time, FMT)
tempo_strip_seconds = ( tempo_strip.minute * 60 ) + tempo_strip.second
tempo_pace_toal = tempo_pace_miles * tempo_strip_seconds

total_running_time = (( easy_pace_total + tempo_pace_toal ) / 60 )
print(total_running_time)

print(str ( timedelta(minutes=total_running_time) ))

# Tried this as well but failed with as directly two timestamps can not be added in Python
#return_time = datetime.strptime(start_time, FMT) + datetime.strptime( str ( timedelta(minutes=total_running_time) ), FMT)
#print(return_time)

datetime.strptime(start_time, FMT) + datetime.timedelta( minutes = total_running_time)

And while executing this I am getting error as

Traceback (most recent call last):
  File "C:/Users//Dropbox/Python_code/ThinkPython/timpo_pace_cal.py", line 30, in <module>
    datetime.strptime(start_time, FMT) + datetime.timedelta( minutes = total_running_time)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Raja G
  • 5,973
  • 14
  • 49
  • 82
  • 2
    Look at your imports at the top; you used `from datetime import datetime` and `from datetime import timedelta`. That should give you a clue as to how to solve this; `datetime.timedelta` is not an attribute on the module here. – Martijn Pieters Sep 27 '18 at 11:26
  • Next, you probably don't want to use `datetime` here just to handle *durations*. `datetime` is great for *points in time*, not for an amount of time passing (`timedelta` objects are there primarily for shifting the points in time). – Martijn Pieters Sep 27 '18 at 11:27
  • You'd be better off splitting your inputs on the `:`, converting the digits into integers, and multiplying those integers to make a total number of seconds: `def parse_duration(duration):`, `parts = [int(p) for p in duration.split(':')]`, `return parts[0] * 3600 + parts[1] * 60 + parts[2]`. – Martijn Pieters Sep 27 '18 at 11:27
  • @MartijnPieters, yes your comments make sense. I misunderstood that. Thank you. – Raja G Sep 27 '18 at 11:27
  • And an added motivation to not use `datetime`: because the module expects `time` to be a point in time, the format is limited to a 24h clock. Any duration beyond 24h can't be parsed! I don't think you'll run into that here when taking timings for individual road / cross-country / track races or training runs (humans generally can't run for 24h straight) but it could be a problem if you were to track total time running, for example. – Martijn Pieters Sep 27 '18 at 11:32
  • @MartijnPieters, could you suggest me with which module I should choose for doing hours calculations ? – Raja G Sep 27 '18 at 11:34
  • Nope, sorry. I'd just parse the duration display into seconds as shown. – Martijn Pieters Sep 27 '18 at 12:14

1 Answers1

2

Change

datetime.strptime(start_time, FMT) + datetime.timedelta( minutes = total_running_time)

to

datetime.strptime(start_time, FMT) + timedelta( minutes = total_running_time)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • working. But I am thinking how timedelta came to available directly ? May be I miscalculated scope of datetime & timedelta ? – Raja G Sep 27 '18 at 11:26
  • Because you have imported it in the top of your script....`from datetime import timedelta` – Rakesh Sep 27 '18 at 11:27