且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将12小时时间格式转换为24小时格式(保存当天的记录)在python中

更新时间:2023-02-26 18:30:45


我试图获得两个电台之间的时差,而使用12小时格式会造成混乱,我尝试使用以下脚本将整个列表转换为24小时格式

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

这里是 time_in_transit 是从第一站开始的站点之间的累积时间具有到达时间,即 time_in_transit [i] i 第一站(的车站与第一站之间的时差)。它被计算为一系列部分和(如 itertools.accumulate() )相邻站之间的时差,即:

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:


  • 站是由他们的索引在 Train_arrival_time 列表(与 time_in_transit 列表相比移动一个)和/或其到达时间

  • (time24h - previous_time)是时间的输出列相邻站之间的差异 - 请查看以下输出中的相邻列。

  • time_in_transit [-1] 是系列中的前一个项目(最后一个) - 它与Python中的 time_in_transit [len(time_in_transit)-1] 相同

  • 当前项目是当前差异+累计总和之和 - 查看总计列i

  • 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.

有没有办法避免得到UTC的日期?

Is there any way to avoid getting the UTC date?

datetime.strptime()的结果是一个天真的datetime对象,不对应任何时区。如果你不知道一个特定的日期(年,月,日),那么没有必要谈论时区。

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).


我是困扰我如何在不同的日子得到两个电台之间的时差;即日09:30(第1天)和下午12:15(第2天)

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

要查找 i之间的时差 -th和 j 第三站:

To find the time difference between i-th and j-th stations:

time_difference = time_in_transit[j] - time_in_transit[i]