将12小时时间格式转换为24小时格式(保存当天的记录)在python中 [英] Convert a 12 hour time format to 24 hour time format (keeping record of the day) in python

查看:579
本文介绍了将12小时时间格式转换为24小时格式(保存当天的记录)在python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是python中的列表,其中列出了每个索引作为列车停机的列车时间列表。

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']

从列表中可以看出,这列火车连续三天运行(来源是起始站)。我试图得到两个电台之间的时差,因为使用 12小时格式会造成混乱,我尝试使用以下脚本将整个列表转换为 24小时格式

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

作为输出。有没有办法避免得到UTC的日期?
另外,我坚持在不同的日子我如何得到两个电台之间的时差?即, 09:30 PM(第1天)和12:15 PM(第2天)

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)

推荐答案


我试图获得两个电台之间的时差,而使用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]

这篇关于将12小时时间格式转换为24小时格式(保存当天的记录)在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆