0

My expertise lack when it comes to understanding this time format. I am guessing the ,XXX is XXX/1000 of a second?

Nevertheless I am trying to add a text files that contains time stamp like these and sum up the total.

Below is an example,

00:03:33,950
00:03:34,590

This is what I have so far but I'm not sure how to add up the last part

Hours = s.split(":")[0]
Minutes = s.split(":")[1]
Seconds = (s.split(":")[2]).split(",")[0]
Total_seconds = (Hours * 3600) + (Minutes * 60) + (Seconds)

Total_Time = str(datetime.timedelta(seconds=Total_seconds))
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Joey Joestar
  • 205
  • 1
  • 11

2 Answers2

0

Reed this documentation about time.strftime() format

For example

from time import gmtime, strftime
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

--'Thu, 28 Jun 2001 14:17:15 +0000'--
Kitch
  • 1
  • 1
  • 2
  • I'm not sure how this answers this question. ```strftime("00:03:33,950", gmtime())``` would give me ```00:03:33,950```... – Joey Joestar Jul 04 '21 at 00:34
0

Actually, you're halfway there.

All you have to do is to to convert your strs into int and pass them as parameters to the appropriate timedelta keywords.

from datetime import timedelta

Hours = int(s.split(":")[0])
Minutes = int(s.split(":")[1])
Seconds = int((s.split(":")[2]).split(",")[0])
Milliseconds = int((s.split(":")[2]).split(",")[1])

duration = timedelta(hours=Hours, minutes=Minutes, seconds=Seconds, milliseconds=Milliseconds)

After adding all the durations you need, str() the final timedelta object.

>>> durations_1 = timedelta(hours=2,milliseconds=750)
>>> durations_2 = timedelta(milliseconds=251)
>>> durations_sum = durations_1 + durations_2
>>> str(durations_sum)
'2:00:01.001000'
>>> str(durations_sum).replace('.',',')
'2:00:01,001000'
dhiaagr
  • 31
  • 5