Following is a list in python which contains a list of time for a train with each index as a stoppage for the train
Train_arrival_time = ['Source', '09:30 PM', '09:56 PM', '11:23 PM', '12:01 AM', '12:12 AM', '12:44 AM', '01:55 AM', '03:18 AM', '04:58 AM', '06:18 AM', '06:33 AM', '07:23 AM', '08:45 AM', '09:14 AM', '10:17 AM', '10:59 AM', '12:15 PM', '01:30 PM', '01:49 PM', '02:55 PM', '03:16 PM', '03:58 PM', '05:15 PM', '05:38 PM', '06:45 PM', '07:20 PM', '08:07 PM', '08:38 PM', '09:25 PM', '11:28 PM', '12:50 AM', '01:21 AM', '01:53 AM', '02:45 AM', '02:57 AM', '03:45 AM', '05:20 AM', '06:00 AM', '06:30 AM']
As evident from the list, this train runs for three consecutive days (source is the starting station). I am trying to get the time difference between two stations , and since using 12-hour format will create chaos i tried using the following script to convert the whole list to 24-hour format
from datetime import *
t12 = '09:35 PM'
t24 = datetime.strptime(t12, '%I:%M %p')
which is giving
1900-01-01 21:35:00
as an output. Is there any way to avoid getting the UTC date?
Additionally, I am stuck on how shall I get the time difference between two stations on different days; viz, 09:30PM (day 1) and 12:15PM (day2)
解决方案
I am trying to get the time difference between two stations , and since using 12-hour format will create chaos i tried using the following script to convert the whole list to 24-hour format
The day doesn't matter if all you need is to find a time difference for adjacent items:
#!/usr/bin/env python
from datetime import datetime, timedelta
ZERO, DAY = timedelta(0), timedelta(days=1)
times = (datetime.strptime(time12h_string, '%I:%M %p')
for time12h_string in Train_arrival_time[1:])
previous_time = next(times)
time_in_transit = [ZERO]
for time24h in times:
time24h = datetime.combine(previous_time, time24h.time())
while time24h < previous_time: # time on the next station should not be ealier
time24h += DAY
time_in_transit.append(time24h - previous_time + time_in_transit[-1])
previous_time = time24h
Here's time_in_transit is the accumulative time between stations starting with the first station that has the arrival time i.e., time_in_transit[i] is a time in transit for the i-th station (the time difference between the station and the first station). It is computed as a series of partial sums (like itertools.accumulate()) of time differences between adjacent stations, namely:
stations are identified by their index in the Train_arrival_time list (shifted by one compared to the time_in_transit list) and/or their arrival times -- look at the Station column in the output below
(time24h - previous_time) is the time difference between adjacent stations -- look at the Adjacent column in the output below
time_in_transit[-1] is the previous item in the series (the last one) -- it is the same as time_in_transit[len(time_in_transit)-1] in Python
the current item is the sum "the current difference + the accumulated sum" -- look at the Total column in the output below.
Is there any way to avoid getting the UTC date?
The result of datetime.strptime() is a naive datetime object that does not correspond to any time zone. There is no point to talk about time zones if you don't know a specific date (year, month, day).
I am stuck on how shall I get the time difference between two stations on different days; viz, 09:30PM (day 1) and 12:15PM (day2)
It is easy to find the time between adjacent stations and the total time in transit:
print("Station | Adjacent | Total")
print("\n".join(["{} | {:>8s} | {}".format(time12h, str(curr-prev), str(curr))
for time12h, curr, prev in zip(Train_arrival_time[1:],
time_in_transit,
[ZERO]+time_in_transit)]))
Output
Station | Adjacent | Total
09:30 PM | 0:00:00 | 0:00:00
09:56 PM | 0:26:00 | 0:26:00
11:23 PM | 1:27:00 | 1:53:00
12:01 AM | 0:38:00 | 2:31:00
12:12 AM | 0:11:00 | 2:42:00
...
05:20 AM | 1:35:00 | 1 day, 7:50:00
06:00 AM | 0:40:00 | 1 day, 8:30:00
06:30 AM | 0:30:00 | 1 day, 9:00:00
To find the time difference between i-th and j-th stations:
time_difference = time_in_transit[j] - time_in_transit[i]