Python Forum
Summing up set elements(HH:MM:SS)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Summing up set elements(HH:MM:SS)
#4
Hope perfingo doesn't mind me infringing on his excellent function.

I was interested to see what happens when hour > 23 or time > 23:59:59

from datetime import timedelta

add_time = {"23:00:15", "6:15:15", "5:15:15"}
 
def convert(time_str):
    hrs, mins, secs = (int(unit) for unit in time_str.split(':'))
    return timedelta(hours=hrs, minutes=mins, seconds=secs)
 
total = sum(map(convert, add_time), timedelta())
minute  = 60
hour    = minute * 60
seconds = total.seconds
hours   = divmod(seconds, hour)
minutes = divmod(hours[1], minute)
rem_seconds = minutes[1]
print(f'the total time is {hours[0]}:{minutes[0]}:{minutes[1]}')
Sure enough, looks like you won't get more than 23 hours!
tester_V likes this post
Reply


Messages In This Thread
Summing up set elements(HH:MM:SS) - by tester_V - Dec-21-2022, 06:44 AM
RE: Summing up set elements(HH:MM:SS) - by tester_V - Dec-21-2022, 06:22 PM
RE: Summing up set elements(HH:MM:SS) - by Pedroski55 - Dec-22-2022, 09:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,170 May-17-2022, 11:38 AM
Last Post: Larz60+
  Summing up rows and columns plumberpy 3 2,286 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,621 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Grouping and summing of dataset jef 0 1,653 Oct-04-2020, 11:03 PM
Last Post: jef
  Summing a list of numbers Oldman45 6 2,910 Jul-12-2020, 05:30 PM
Last Post: Oldman45
  Merge 2 dataframes but avoid double summing of rows? vinaysalian17 0 1,857 Jun-03-2020, 01:48 AM
Last Post: vinaysalian17
  filtering and summing data in a text file apexman 3 3,144 Mar-18-2019, 09:25 PM
Last Post: ichabod801
  summing digits of a number Megabeaz 3 2,676 Jun-29-2018, 02:55 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020