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

时间:2014-03-16 01:48:54

标签: 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)

使用此命令:

time_in_24h(7,'am')

我得到了这个输出:

7 am EST is 6 Central Time(CT)
7 am EST is 5 Mountain Time(MT)
7 am EST is 4 Pacific Time(PT)

我正在尝试根据EST的输入,上午/下午到中央时间(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)

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

3 个答案:

答案 0 :(得分:2)

如果您正在谈论时区,那么除了时间之外,您还必须指定日期(年,月,日)。

将给定的hoursam_or_pm转换为其他时区:

  1. 转换为24小时
  2. 添加日期
  3. 在源时区中创建时区感知日期时间对象
  4. 将其转换为给定的时区
  5. time_in_24h不应该尝试做太多。让它做它的名字所暗示的,即:将输入小时数,上午或下午转换为24小时格式:

    def time_in_24h(hours, am_or_pm):
        """Convert to 24 hours."""
        if not (1 <= hours <= 12):
            raise ValueError("hours must be in 01,..,12 range, got %r" % (hours,))
        hours %= 12 # accept 12am as 00, and 12pm as 12
        am_or_pm = am_or_pm.lower()
        if am_or_pm == 'am':
            pass
        elif am_or_pm == 'pm':
            hours += 12
        else:
            raise ValueError("am_or_pm must be 'am' or 'pm', got: %r" % (am_or_pm,))
        return hours
    

    您也可以使用strptime()实现它:

    from datetime import datetime
    
    def time_in_24h(hours, am_or_pm):
        return datetime.strptime("%02d%s" % (hours, am_or_pm), '%I%p').hour
    

    然后你可以定义print_times()函数,在3个不同的时区中打印时间:

    from datetime import datetime, time
    import pytz
    
    def astimezone(aware_dt, dest_tz):
        """Convert the time to `dest_tz` timezone"""
        return dest_tz.normalize(aware_dt.astimezone(dest_tz))
    
    def print_times(hours, am_or_pm, date=None, is_dst=None):
        source_tz = pytz.timezone('US/Eastern')
    
        # 2. add date
        if date is None: # if date is not specified; use today
            date = datetime.now(source_tz).date()
        naive_dt = datetime.combine(date, time(time_in_24h(hours, am_or_pm), 0, 0))
    
        # 3. create timezone-aware datetime object in the source timezone
        source_dt = source_tz.localize(naive_dt, is_dst=is_dst)
    
        #NOTE: these timezone names are deprecated,
        #      use Continent/City format instead
        fmt = '%I %p %Z'
        for tzname in 'US/Central', 'US/Mountain', 'US/Pacific':
            dest_dt = astimezone(source_dt, pytz.timezone(tzname))
            print("{source_dt:{fmt}} is {dest_dt:{fmt}} ({tzname})".format(
                **locals()))
    

    正确指定日期非常重要,否则您将无法知道正确的UTC偏移。该代码使用pytz模块来访问时区信息。 strftime() format codes用于打印。

    示例:

    >>> print_times(7, 'am')
    07 AM EDT is 06 AM CDT (US/Central)
    07 AM EDT is 05 AM MDT (US/Mountain)
    07 AM EDT is 04 AM PDT (US/Pacific)
    >>> print_times(1, 'pm')
    01 PM EDT is 12 PM CDT (US/Central)
    01 PM EDT is 11 AM MDT (US/Mountain)
    01 PM EDT is 10 AM PDT (US/Pacific)
    >>> print_times(9, 'pm')
    09 PM EDT is 08 PM CDT (US/Central)
    09 PM EDT is 07 PM MDT (US/Mountain)
    09 PM EDT is 06 PM PDT (US/Pacific)
    

    特别说明:

    • datetime.now(source_tz)允许在给定时区内获取正确的当前时间。如果本地时区不是datetime.now()
    • ,则此处source_tz将不正确
    • is_dst=None表示tz.localize()方法针对模糊或不存在的本地时间引发异常。否则,它只是默认为is_dst=False,可能不是您想要的
    • 如果转换发生在DST边界,则可能需要
    • tz.normalize()

答案 1 :(得分:1)

对于Central,只需检查东部时间是否是下午12点。如果是,则调整到上午11点。同样,如果东部是午夜,则调整到(11)pm。它需要一些if s,但你可以做到。

问题是,它仍然是错误的。日期时间数学很难,你已经忘记了夏令时。

通常,每年一次,东部时间(美国东部时间)凌晨1点,中部时间凌晨1点(CDT)。 (同样,它在东部时间凌晨3点,中部时间凌晨1点。)

根据您的功能目前所需的输入量,您无法解释这一点:您需要知道完整的日期和时间。完成后,最简单的方法是将完整的东部(美国/纽约)日期和时间转换为UTC,然后将其转换为中央(美国/芝加哥),山地(美国/丹佛)和太平洋(美国/ Los_Angeles)。

America / ...位是大多数机器上的TZ数据库使用的时区的名称。 America / New_York是&#34; Eastern&#34; time:它指定DST何时,何时不是,以及与UTC的偏移量是多少。它甚至有历史数据,作为夏令时开始的日期和时间。结束了。 (在全球范围内,实际上经常发生变化。政府......)

保持非DST感知版本:

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

def to_24hour(hour, ampm):
    """Convert a 12-hour time and "am" or "pm" to a 24-hour value."""
    if ampm == 'am':
        return 0 if hour == 12 else hour
    else:
        return 12 if hour == 12 else hour + 12

def to_12hour(hour):
    """Convert a 24-hour clock value to a 12-hour one."""
    if hour == 0:
        return (12, 'am')
    elif hour < 12:
        return (hour, 'am')
    elif hour == 12:
        return (12, 'pm')
    else:
        return (hour - 12, 'pm')

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

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:
        est_24hour = to_24hour(time, time_day)
        hour, ampm = to_12hour((est_24hour - 1 + 24) % 24)
        print(time, time_day, eEst, hour, ampm, cCentral)
        hour, ampm = to_12hour((est_24hour - 2 + 24) % 24)
        print(time, time_day, eEst, hour, ampm, mMountain)
        hour, ampm = to_12hour((est_24hour - 3 + 24) % 24)
        print(time, time_day, eEst, hour, ampm, pPacific)

......进行这些调整:

>>> time_in_24h(1, 'pm')
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)

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

  

(number,str) - &gt;编号

     

此功能将12小时的时间(以上午/下午为单位)转换为等效的24小时时间。

     

以24小时格式返回时间。

这当然是to_24hour所做的。

具有时区感知功能

Python对时区没有任何帮助;贾斯汀巴伯在一个单独的答案中建议pytz;这是一个不错的选择。

如果我们知道在东部有一些日期或时间:

now = datetime.datetime(2014, 3, 14, 12, 34)

抓住时区:

et = pytz.timezone('America/New_York')
ct = pytz.timezone('America/Chicago')
mt = pytz.timezone('America/Denver')
pt = pytz.timezone('America/Los_Angeles')

我们可以转换为Central和其他人:

now_et = et.normalize(et.localize(now))
now_ct = ct.normalize(now_et.astimezone(ct))
now_mt = mt.normalize(now_et.astimezone(mt))
now_pt = pt.normalize(now_et.astimezone(pt))

(正如评论所述,pytz奇怪地需要在更改可能越过DST边界时调用normalize,这基本上就是你所做的事情。)

然后:

print('{} Eastern in various other timezones:'.format(now_et.strftime('%I %p')))
print('Central : {}'.format(now_ct.strftime('%I %p')))
print('Mountain: {}'.format(now_mt.strftime('%I %p')))
print('Pacific : {}'.format(now_pt.strftime('%I %p')))

答案 2 :(得分:1)

您可以尝试pytz。您可以使用pip install pytz进行安装。您可以使用normalize方法(基于the docs)执行此类操作:

>>> from datetime import datetime
>>> from pytz import timezone
>>> import pytz
>>> central = timezone('US/Central')
>>> mountain = timezone('US/Mountain')
>>> mountain_date = mountain.localize(datetime.now())  # set time as right now mountain time
>>> fmt = '%Y-%m-%d %I:%M:%S %p'
>>> central_date = central.normalize(mountain_date.astimezone(central))
>>> print central_date.strftime(fmt), central, 'is', mountain_date.strftime(fmt), mountain
2014-03-15 07:29:17 PM US/Mountain is 2014-03-15 08:29:17 PM US/Central

所以你的功能最终会看起来像这样:

>>> def time_in_24h(hour, am_pm):
    """hour is an integer that represents eastern time on the 12-hour clock, and am_pm
       will take any string that begins with 'a' or 'p'"""
    if not am_pm.lower().startswith(('a', 'p')):  # return None if am_pm does not start with 'a' or 'p'
        return
    central = timezone('US/Central')
    mountain = timezone('US/Mountain')
    pacific = timezone('US/Pacific')
    eastern = timezone('US/Eastern')
    if hour == 12:
        mod_hour = 0 if am_pm.lower().startswith('a') else 12
    else:
        mod_hour = hour + 12 if am_pm.lower().startswith('p') else hour  # add 12 if am_pm starts with 'p', but keep original hour for display purposes
    fmt = '%H:%M %p'
    eastern_time = eastern.localize(datetime(1900, 1, 2, mod_hour))
    for tz in (central, mountain, pacific):
        d = tz.normalize(eastern_time.astimezone(tz))
        print hour, am_pm, 'EST is', d.strftime(fmt), tz  # tz displays as pytz timezone; can change


>>> time_in_24h(1, 'am')
1 am EST is 00:00 AM US/Central
1 am EST is 23:00 PM US/Mountain
1 am EST is 22:00 PM US/Pacific
>>> time_in_24h(9, 'pm')
9 pm EST is 20:00 PM US/Central
9 pm EST is 19:00 PM US/Mountain
9 pm EST is 18:00 PM US/Pacific