0

I have an array of times:

Time_Array = [858, 926, 953, 1015, 1055, 1123, 1150, 1214, 1238, 1336, 1413] 

The time in Time_Array is formatted as military time with either the first or second digit being an hour, and the last two being a minute.

and a reference time:

T0 = 1516

T0 is in the HHMM format.

The times in Time_Array and T0 are all strings currently.

What I'd like to do is with T0, find the total time between each object in Time_Array and T0. Ie: The total time, in minutes, between T0 and the 0th value of the Time_Array, T0 and the 1st value of Time_Array, etc. So, i'd need to convert each object to minutes, and then subtract the two.

I'm sure this is probably something rather trivial that I'm just missing, but I'm still learning a lot about python and how to treat lists when I need something from them. Any advice is greatly appreciated on this matter!

R. Davis
  • 47
  • 7
  • Try to solve the problem, and come back w/ the specifics of what problem(s) you have (and if you don't have any problems, even better!). – Scott Hunter Dec 20 '21 at 19:46
  • @Chris Initially that's what I had looked at, but the issue is that the objects are in an HMM or HHMM format, so if I can convert them to total minutes, then your suggestion would be a perfect solution to the second prong of the problem. I updated the question for clarity based on your question. I realized I didn't give enough clarification on the format of the numbers. – R. Davis Dec 20 '21 at 19:53

3 Answers3

2

Try with datetime. You need to combine a date with the time to be able to subtract:

import datetime
Time_Array = ["858", "926", "953", "1015", "1055", "1123", "1150", "1214", "1238", "1336", "1413"] 
T0 = "1516"

>>> [(datetime.datetime.combine(datetime.date.today(),datetime.time(int(T0[:-2]),int(T0[-2:])))-datetime.datetime.combine(datetime.date.today(),datetime.time(int(t[:-2]),int(t[-2:])))).total_seconds()/60 for t in Time_Array]
[378.0, 350.0, 323.0, 301.0, 261.0, 233.0, 206.0, 182.0, 158.0, 100.0, 63.0]
not_speshal
  • 22,093
  • 2
  • 15
  • 30
2

If all you need is to count the minutes, you don't need to make things complicated with datetime. Example:

mt = 1123                # military time 11:23
h, m = divmod(mt, 100)   # hours, minutes (11,23)
minutes = 60*h + m       # converted to minutes 683

The rest is a simple math, just use a function like this one:

def to_minutes(mt):
    h, m = divmod(mt, 100)
    return 60*h + m
VPfB
  • 14,927
  • 6
  • 41
  • 75
  • I agree, and this was my initial approach when I was trying to solve this problem by hand. Mathematically, it made the most sense. The issue I had with it was doing it for each element in `Time_Array`. – R. Davis Dec 20 '21 at 20:21
0

Try this with list, map, and lambda:

#Array of times
Time_Array = [858, 926, 953, 1015, 1055, 1123, 1150, 1214, 1238, 1336, 1413]

#Reference time
T0 = 1516

##Total time elapsed from array of times and reference time
Total_time_elapsed = list(map(lambda x: T0 - x, Time_Array))
print(Total_time_elapsed)

Or this (update: they pointed out an error to me, now it's correct):

#Array of times
Time_Array = ["858, 926, 953, 1015, 1055, 1123, 1150, 1214, 1238, 1336, 1413"]
    
#Reference time
T0 = "1516"

#Total time elapsed from array of times and reference time
[(datetime.datetime.combine(datetime.date.today(),

datetime.time(int(T0[:-2]),

int(T0[-2:])))-datetime.datetime.combine(datetime.date.today(),

datetime.time(int(x[:-2]),

int(x[-2:])))).total_seconds() / 60 for x in Time_Array]    
Erling Olsen
  • 1
  • 4
  • 15
  • How did you deal with the 'int' objects not being subscriptable? – R. Davis Dec 20 '21 at 20:15
  • Neither of your answers work. First one is simply subtracting integers, not times. And the second one "Or this" is just my answer copy-pasted, which also doesn't work because you have a list of integers, not strings. – not_speshal Dec 20 '21 at 21:27
  • @not_speshal I haven't copied your answer, but when I see it's almost the same. I did the same reasoning as you.Sorry if it looks similar.And I offered two solutions (you one, so I didn't copy). And then there's no need to comment saying the code doesn't work. It took me good will, patience and time to help whoever asked the question. If the code doesn't work, at least I've tried to help.As far as I can see, you wrote an answer similar to mine (which does not work) and I have not commented that it does not work,and I have not even put a negative rating on you like you did with me. Good evening – Erling Olsen Dec 20 '21 at 21:44
  • My lists are strings. My code works correctly and gives the output I posted in my answer. I can see all the edits you made on your answer. The first version you posted was copy-pasting my answer (it cannot be a coincidence that we matched every character exactly): You even had `for t in Time_Array` which you later changed to `for x in Time_Array`. After I commented, you put the second answer, which is a subtraction of integers giving you the incorrect answer: `[658, 590, 563, 501, 461, 393, 366, 302, 278, 180, 103]`. – not_speshal Dec 20 '21 at 21:51
  • @not_speshal Do you see then that the answers were different? Now I correct my answer and render the code correct. Stackoverflow is not an answer competition, but the fundamental purpose is to help both the questioner and the various readers. Now I modify the answer solving the problem for future readers as well. In this way the reader will have a correct answer. Thanks for pointing out the mistake. – Erling Olsen Dec 20 '21 at 23:06
  • Maybe you should test your answers before claiming they are correct. This is not an answer competition but is meant to provide *helpful* and *original* answers. – not_speshal Dec 20 '21 at 23:08