Reputation: 305
I am writing python code that handles a text file and, for each line of input, calculates the time involved with the action described in that line, then reports a "running time". Required output like
lapse time
start 12:00
action1 0:37 12:37
action2 1:01 13:38
action3 0:30 14:08
I came to some possible approaches but all looked awkward. Most of all I am confused about using either "time" or "strftime" or "datetime" or perhaps even something else. I am sure a simple elegant solution must exist.
NB timezones and DST are of no concern, if I can get to show UTC that is sufficient, at least for a beginning. In a later stage the last column (UTC) might be augmented by a column "local time" but that is secondary.
Edited, for apparent lack of clarity, to add example pseudo-code:
TIME='12:00'
PCS_PER_HOUR=750
with infile:
read line of data
process data, including calc of PCS_DONE
LAPSE=PCS_DONE/PCS_PER_HOUR
TIME += LAPSE
print(' ..... ' ,format(data, data, data, LAPSE, TIME))
My question is how to make the "TIME='12:00" into something that python can easily handle, and increment with a calculated integer number of seconds or minutes or whatever.
Upvotes: 0
Views: 441
Reputation: 43573
Use datetime.timedelta
.
Set the start time.
In [1]: import datetime
The initial time is 12 hours.
In [2]: start = datetime.timedelta(hours=12)
In [3]: start
Out[3]: datetime.timedelta(0, 43200)
Let's say that LAPSE
is 0.23 hours (pieces divided by pieces per hour yields hours).
In [4]: action1 = datetime.timedelta(hours=0.23)
Now you can format the times like you want. The standard representation is hours, minutes and seconds:
In [5]: str(start)
Out[5]: '12:00:00'
In [6]: str(start+action1)
Out[6]: '12:13:48'
Upvotes: 1