繁体   English   中英

我需要用Python设置不同的时区

[英]I need with Python setting up different timezones

我是python的新手,试图弄清楚这个:

输入是:

1.One integer for local time in Eastern time zone
2.One string containing “am” or “pm”.
3.All other variations such as Am, Pm.
4.AM, PM should result in a message stating that input format is not right

输出是:

___ EST is ___Central Time(CT)

___ EST is ___Mountain Time(MT)

___EST is___Pacific Time(PT)

这是迄今为止的代码:

#Function 1: 

def time_in_24h(time,time_day):
    ''' (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.

    '''

    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 
        return # write the rest of the program here


#Function 2: 

def time_in_12h(time_24):
    ''' (number)--> (number,str)
    This function converts a 24-hour time to the equivalent 12-hour time. 
    Returns time in the 12-hour format with am/pm. 
    '''

    if 12< time_24 <24:
        return (time_24)-12,'pm'          

    elif: # write the rest of the program here, I am lost :(

#Function 3: Main function where an Eastern time zone is the input 

def time_zone(time,am_pm):

    time_24=time_in_24h(time,am_pm)

time_in_24h(time,time_day)

此功能将12小时时间(以am / pm为单位)转换为等效的24小时时间。 以24小时格式返回时间。

让我们通过查看示例来思考它是如何工作的:

  • “下午5点”:什么是24小时格式的5 pm “pm”的意思是“post meridiem”,或“中午之后”,所以这是中午之后的几个小时。 因此,“下午5点”的24小时格式为17小时, 5 + 12
  • “上午9点”: “我”的意思是“吃了meridiem”,或“中午前”,所以这是中午几个小时。 因此,“上午9点”的24小时格式为9小时。
  • 特殊情况“上午12点”:上午12点是午夜,所以它是12 + 12 = 24 ,尽管是“我”。
  • 特殊情况“下午12点”:中午12点是中午,所以只有12 ,尽管是“下午”。

所以,你的代码看起来像这样:

if time == 12:
    if time_day == 'am':
         return …
    else:
         return …
elif time_day == 'pm':
    return …
elif time_day == 'am':
    return …

time_in_12h(time_24)

此功能可将24小时时间转换为相当于12小时的时间。 以am / pm返回12小时格式的时间。

这与上面相同,只是相反。 只需反转上面应用的逻辑,您就可以简单地填写函数的其余部分。 特别是因为你的代码已经完成了一半的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM