您的位置:首页 > 编程语言 > Python开发

Python将时间为AM的转换为24小时制

2015-12-05 20:03 441 查看
Below is my code , totally C type programmer, no excellent skills.

import sys

#time = input().strip()
time = "00:05:45PM"

"""
I don't know this issue.
12:24:35AM -> 0:24:35
12:24:35PM -> 12:24:35
"""
print(time)

if time.find("AM") >= 0:
time = time.strip("AM")
if time == "12:00:00":
time = "00:00:00"

time = time.split(':')

hh = int(time[0])
mm = int(time[1])
ss = int(time[2])
if hh == 12 and (mm > 0 or ss > 0):
hh = 0

#print(time)
print("%02d:%02d:%02d" % (hh, mm, ss))
else:

time = time.strip("PM")
time = time.split(':')

hh = int(time[0])
mm = int(time[1])
ss = int(time[2])
#print (time[0])
#print (time[1])
#print (time[2])

if hh != 12:
hh = hh + 12

print("%02d:%02d:%02d" % (hh, mm, ss))


This is done by someone expert.

We’re in different level.

You find the Python type grammer.

string operation, list convert, map usage, and join, lambda.

ins = input().strip()

is_pm = ins[-2:].lower() == 'pm'
time_list = list(map(int, ins[:-2].split(':')))

if is_pm and time_list[0] < 12:
time_list[0] += 12

if not is_pm and time_list[0] == 12:
time_list[0] = 0

print(':'.join(map(lambda x: str(x).rjust(2, '0'), time_list)))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: