Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to add HH:MM:SS:MS 1

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
US
Hi,
Does anybody knows how to add times in Python?
Need to add HH:MM:SS:MS
The file goes like this:
5/1/2020 2:40:31 PM, 5/1/2020 2:43:11 PM, 00:02:40.5449136,
5/1/2020 2:45:26 PM, 5/1/2020 2:48:17 PM, 00:02:50.8250927,
the third column is a timduration, need to sum it up.

Thank you.
 
Hi

Tester_V said:
Does anybody knows how to add times in Python?
Fortunately Python has a dedicated [tt]timedelta[/tt] class for that purpose ( just like Java has [tt]Period[/tt] and PHP has [tt]DateInterval[/tt] ). That makes calculations simple ( and with Python's overloaded operators also intuitive ).
Unfortunately [tt]timedelta[/tt] has no dedicated method to initialize it from a string, like [tt]datetime[/tt]'s [tt].strptime()[/tt], so you have to spoonfeed it with integers :
Python:
[red]from[/red] datetime [red]import[/red] datetime[teal],[/teal] timedelta
[red]import[/red] re

with [COLOR=orange]open[/color][teal]([/teal][i][green]'Tester_V.txt'[/green][/i][teal])[/teal] as file[teal]:[/teal]
    [b]for[/b] line [b]in[/b] file[teal]:[/teal]
        part [teal]=[/teal] line[teal].[/teal][COLOR=orange]split[/color][teal]([/teal][i][green]', '[/green][/i][teal])[/teal]

        first [teal]=[/teal] datetime[teal].[/teal][COLOR=orange]strptime[/color][teal]([/teal]part[teal][[/teal][purple]0[/purple][teal]],[/teal] [i][green]'%m/%d/%Y %I:%M:%S %p'[/green][/i][teal])[/teal]
        second [teal]=[/teal] datetime[teal].[/teal][COLOR=orange]strptime[/color][teal]([/teal]part[teal][[/teal][purple]1[/purple][teal]],[/teal] [i][green]'%m/%d/%Y %I:%M:%S %p'[/green][/i][teal])[/teal]

        deltapart [teal]= [[/teal][COLOR=orange]int[/color][teal]([/teal]one[teal])[/teal] [b]for[/b] one [b]in[/b] re[teal].[/teal][COLOR=orange]findall[/color][teal]([/teal]r[i][green]'\d+'[/green][/i][teal],[/teal] part[teal][[/teal][purple]2[/purple][teal]])][/teal]

        delta [teal]=[/teal] [COLOR=orange]timedelta[/color][teal]([/teal]hours [teal]=[/teal] deltapart[teal][[/teal][purple]0[/purple][teal]],[/teal] minutes [teal]=[/teal] deltapart[teal][[/teal][purple]1[/purple][teal]],[/teal] seconds [teal]=[/teal] deltapart[teal][[/teal][purple]2[/purple][teal]],[/teal] microseconds [teal]=[/teal] deltapart[teal][[/teal][purple]3[/purple][teal]])[/teal]

        [b]print[/b][teal]([/teal][i][green]'%s + %s = %s'[/green][/i] [teal]% ([/teal]first[teal],[/teal] delta[teal],[/teal] first [teal]+[/teal] delta[teal]))[/teal]
        [b]print[/b][teal]([/teal][i][green]'%s + %s = %s'[/green][/i] [teal]% ([/teal]second[teal],[/teal] delta[teal],[/teal] second [teal]+[/teal] delta[teal]))[/teal]


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top