Python - 使用am/pm将时间转换为不同的时区

Mav*_*ick 5 python datetime

在Python中,使用此函数(使用东部标准时间(EST)与am/pm,应以3种时间格式输出.中央时间(CT),山地时间(MT)和太平洋时间(PT)都在正确的上午/下午):

def time_in_24h(time,time_day): #time is eastern time
''' (number, str)--> number
This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time. 
Returns time in the 24-hour format.
'''
cCentral = 'Central Time(CT)' #-1 hour offset
mMountain = 'Mountain Time(MT)'#-2 hours offset
pPacific = 'Pacific Time(PT)'#-3 hours offset
eEst = 'EST is'

if time_day!='pm' and time_day!='am':

    print('Input is not in the right format')  #this is where input is not in the right format
    return 0

else:
    print(time, time_day, eEst, time-1, cCentral)
    print(time, time_day, eEst, time-2, mMountain)
    print(time, time_day, eEst, time-3, pPacific)
Run Code Online (Sandbox Code Playgroud)

使用此命令:

time_in_24h(7,'am')
Run Code Online (Sandbox Code Playgroud)

我得到这个输出:

7 am EST is 6 Central Time(CT)
7 am EST is 5 Mountain Time(MT)
7 am EST is 4 Pacific Time(PT)
Run Code Online (Sandbox Code Playgroud)

我试图根据EST的输入,am/pm到中央时间(CT),山地时间(MT)和太平洋时间(PT)输出正确的3次,所有这些都在正确的上午/下午.如何根据偏移量输出正确的上午/下午?例如,美国东部时间下午2点输入,应该输出:

1 pm EST is 12 pm Central Time(CT)
1 pm EST is 11 am Mountain Time(MT)
1 pm EST is 10 am Pacific Time(PT)
Run Code Online (Sandbox Code Playgroud)

如您所见,pm/am根据偏移进行更改并且不一致.根据时间(上午/下午)变化,最好的处理方法是什么?任何用python构建的东西都可以解决这个问题吗?解决我的两难困境?我全力以赴,真的坚持了这一点.

Tha*_*tos 4

对于中部地区,只需检查东部时间是否为中午 12 点即可。如果是的话,调整到上午 11 点。同样,如果东部是午夜,则调整为晚上 11 点。这需要一点时间if时间,但你可以做到。

\n\n

问题是,还是错了。日期时间数学很难,而且您已经忘记了 DST。

\n\n

通常,每年一次,东部时间 (EST) 凌晨 1 点,中部时间 (CDT) 凌晨 1 点。(同样,东部也有一次是凌晨 3 点,中部也是凌晨 1 点。)

\n\n

根据您的函数当前接受的输入量,您无法解释这一点:您需要知道完整的日期和时间。完成后,最简单的方法是将完整的东部(美国/纽约)日期和时间转换为 UTC,然后将其转换为中部(美国/芝加哥)、山区(美国/丹佛)和太平洋(美国/洛杉矶) )。

\n\n

America/\xe2\x80\xa6 位是大多数计算机上的 TZ 数据库使用的时区名称。America/New_York 是“东部”时间:它指定 DST 何时、何时不是,以及与 UTC 的偏移量。它甚至还有历史数据,因为夏令时开始和结束的日期已经改变。(在全球范围内,实际上变化非常频繁。Governments\xe2\x80\xa6)

\n\n

保留不支持 DST 的版本:

\n\n

首先,让我们运行两个辅助函数:to_24hour将 a 转换(hour, ampm)为 24 小时格式,以及to_12hour将 a 转换为 24 小时格式。这些将使事情更容易思考,因为我们可以在 24 小时内更轻松地进行减法。

\n\n
def to_24hour(hour, ampm):\n    """Convert a 12-hour time and "am" or "pm" to a 24-hour value."""\n    if ampm == \'am\':\n        return 0 if hour == 12 else hour\n    else:\n        return 12 if hour == 12 else hour + 12\n\ndef to_12hour(hour):\n    """Convert a 24-hour clock value to a 12-hour one."""\n    if hour == 0:\n        return (12, \'am\')\n    elif hour < 12:\n        return (hour, \'am\')\n    elif hour == 12:\n        return (12, \'pm\')\n    else:\n        return (hour - 12, \'pm\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

一旦我们有了这些,事情就变得简单了:

\n\n
def time_in_24h(time,time_day): #time is eastern time\n    \'\'\' (number, str)--> number\n    This function converts a 12-hour time, represented by am/pm,to its equivalent 24-hour time. \n    Returns time in the 24-hour format.\n    \'\'\'\n    cCentral = \'Central Time(CT)\' #-1 hour offset\n    mMountain = \'Mountain Time(MT)\'#-2 hours offset\n    pPacific = \'Pacific Time(PT)\'#-3 hours offset\n    eEst = \'EST is\'\n\n    if time_day!=\'pm\' and time_day!=\'am\':\n\n        print(\'Input is not in the right format\')  #this is where input is not in the right format\n        return 0\n\n    else:\n        est_24hour = to_24hour(time, time_day)\n        hour, ampm = to_12hour((est_24hour - 1 + 24) % 24)\n        print(time, time_day, eEst, hour, ampm, cCentral)\n        hour, ampm = to_12hour((est_24hour - 2 + 24) % 24)\n        print(time, time_day, eEst, hour, ampm, mMountain)\n        hour, ampm = to_12hour((est_24hour - 3 + 24) % 24)\n        print(time, time_day, eEst, hour, ampm, pPacific)\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\xa6 进行这些调整:

\n\n
>>> time_in_24h(1, \'pm\')\n1 pm EST is 12 pm Central Time(CT)\n1 pm EST is 11 am Mountain Time(MT)\n1 pm EST is 10 am Pacific Time(PT)\n
Run Code Online (Sandbox Code Playgroud)\n\n

最后一点。你的文档字符串如下:

\n\n
\n

(数字,字符串)--> 数字

\n\n

此函数将用 am/pm 表示的 12 小时时间转换为其等效的 24 小时时间。

\n\n

返回 24 小时格式的时间。

\n
\n\n

当然,这就是事实to_24hour

\n\n

具有时区意识

\n\n

Python 对于时区并没有真正的帮助;贾斯汀·巴伯(Justin Barber)在另一篇回答中建议pytz;这是一个不错的选择。

\n\n

如果我们知道某个日期或时间在东部:

\n\n
now = datetime.datetime(2014, 3, 14, 12, 34)\n
Run Code Online (Sandbox Code Playgroud)\n\n

获取时区:

\n\n
et = pytz.timezone(\'America/New_York\')\nct = pytz.timezone(\'America/Chicago\')\nmt = pytz.timezone(\'America/Denver\')\npt = pytz.timezone(\'America/Los_Angeles\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

我们可以通过以下方式转换为 Central 和其他:

\n\n
now_et = et.normalize(et.localize(now))\nnow_ct = ct.normalize(now_et.astimezone(ct))\nnow_mt = mt.normalize(now_et.astimezone(mt))\nnow_pt = pt.normalize(now_et.astimezone(pt))\n
Run Code Online (Sandbox Code Playgroud)\n\n

(正如评论指出的,奇怪的是,当更改可能跨越 DST 边界时pytz需要调用,这基本上是您所做的任何事情。)normalize

\n\n

然后:

\n\n
print(\'{} Eastern in various other timezones:\'.format(now_et.strftime(\'%I %p\')))\nprint(\'Central : {}\'.format(now_ct.strftime(\'%I %p\')))\nprint(\'Mountain: {}\'.format(now_mt.strftime(\'%I %p\')))\nprint(\'Pacific : {}\'.format(now_pt.strftime(\'%I %p\')))\n
Run Code Online (Sandbox Code Playgroud)\n