Adding two stored Times HH:mm:ss together

I have historical data that I pulled using SQL which I I'm looping through for the accumulative time down for the multiple chutes on our system item[1]. Which I don't think I have a problem with. I have both the current alarm time and the historical alarm times. I just wanted to combine the two.

For example:

nd_time[count2] is the current alarm duration. It is a string and contains "00:10:50"
item[1] is the historical time it's unicode and contains "01:09:54"

If I just try adding them together it just adds the strings to "00:10:5001:09:54"
I can convert them both to datetime.datetime like I've done below but then I get a type error when using datetime.combine:

TypeError("time argument must be a time instance")

I want to combine "00:10:50" and "01:09:54" and get "01:20:44"

	for item in history_data:
		desc2[count2] = "CHUTE_" + str(count2+1)
		if temp['UDT_tag'] == desc2[count]:
			currenttime= datetime.strptime(nd_time[count2], "%H:%M:%S")
			accutime = datetime.strptime(item[1], "%H:%M:%S")
			totaltime[count] = datetime.combine(currenttime, accutime)
			print("Item :" + totaltime[count])
		count = count+ 1

Seems like something simple I'm missing.

Thank you.

Don't store dates as strings?

All of that aside, I strongly recommend not using the python Datetime module, for anything. Instead use java and or Ignition's own date functions.

Of course it does. To the interpreter these are just strings, and so the result you get is the concatenation of them.

I think you have a fundamental misunderstanding of what datetime.combine() actually does. It takes a date and a time and combines them into a single datetime object representing a date and time. In this case you have two times, and (I think) you want to add them together.

Okay, assuming you're stuck for some reason with strings in the database instead of datetime objects, how do you solve your issue? First convert the string time values into something that we can work with as a representation of a date.

This code will convert a string with your format into a date object.

currenttime = system.date.parse(nd_time[count2], "HH:MM:SS")
accutime = system.date.parse(item[1], "HH:MM:SS")

Then you can use the date functions as appropriate to determine the new date you're looking for.

totaltime[count] = system.date.addMillis(currenttime, system.date.toMillis(accutime))

You can then use system.date.format() to format the date object as a string for printing.

Here is a link to the Ignition Date functions:
https://docs.inductiveautomation.com/display/DOC81/system.date

2 Likes

If there's a date component to the historical, I'd strongly recommend adding that into the mix as well, to better handle items going past midnight.


While we don't recommend using Jythons datetime.datetime objects, I've not yet had an issue using timedelta to put durations into an HMS format.

from datetime import timedelta

def getSec(time_str):
    """Get seconds from time."""
    h, m, s = time_str.split(':')
    return int(h) * 3600 + int(m) * 60 + int(s)
    
def secToHMS(sec):
	return(timedelta(seconds=sec))

t1 = getSec("00:10:50")
t2 = getSec("01:09:54")

print secToHMS(t1+t2)

Output

 
1:20:44
>>>
2 Likes

Thank you so much. I was pretty confused by what I was trying to do. So many different date time options.

On this line

totaltime[count] = system.date.addMillis(currenttime, system.date.toMillis(accutime))

I get "TypeError: addMillis(): 2nd arg can't be coerced to int" when running this

Could probably just do:
totaltime[count] = currenttime.time + accutime.time

If you just want the delta milliseconds.

1 Like

Interesting.

If you insert a print statement prior to that line:

print system.date.toMillis(accutime),type(accutime)

What happens?

Here's the result:

28879200058 <type 'java.util.Date'>

I would expect it to work. Odd.

Try @PGriffith's suggestion.

I used PGriffith's suggestion and it's working well. Thanks to everyone for the suggestions and quick responses.