此处共有python新手。尝试制作一个不和谐的机器人来帮助组织不同时区的朋友一起玩。以下代码使用UTC参数而不是EST给出正确的时间信息。 EST的时间总是提前56分钟。
# tz_bot
import re
import discord
from discord.ext import commands
from datetime import datetime
from pytz import timezone
description = '''A bot for converting timezone info. Use the format:
!convert 2018-07-01 3:54 PM UTC '''
bot = commands.Bot(command_prefix='!', description=description)
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
@bot.command()
async def current():
format = "%Y-%m-%d %I:%M %p"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
# await bot.say (now_utc.strftime(format) + " (UTC)")
# Convert to Europe/London time zone
now_london = now_utc.astimezone(timezone('Europe/London'))
await bot.say (now_london.strftime(format) + " (London)")
# Convert to US/Eastern time zone
now_us_east = now_utc.astimezone(timezone('US/Eastern'))
await bot.say (now_us_east.strftime(format) + " (Eastern)")
# Convert to US/Central time zone
now_central = now_utc.astimezone(timezone('US/Central'))
await bot.say (now_central.strftime(format) + " (Central)")
# Convert to US/Mountain time zone
now_mountain = now_utc.astimezone(timezone('US/Mountain'))
await bot.say (now_mountain.strftime(format) + " (Mountain)")
# Convert to US/Pacific time zone
now_pacific = now_utc.astimezone(timezone('US/Pacific'))
await bot.say (now_pacific.strftime(format) + " (Pacific)")
@bot.command()
async def convert(day_str, time_str, am_pm_str, tz_str):
date_str= "%s+%s+%s" % (day_str,time_str,am_pm_str)
datetime_obj = datetime.strptime(date_str, "%Y-%m-%d+%I:%M+%p")
format = "%Y-%m-%d %I:%M %p"
#now_time = datetime_obj.replace(tzinfo=timezone('UTC'))
if re.match(r'^EST', tz_str):
now_time = datetime_obj.replace(tzinfo=timezone('America/New_York'))
#elif re.match(r'^est', tz_str):
# now_time = datetime_obj.replace(tzinfo=timezone('America/New_York'))
#elif re.match(r'^EDT', tz_str):
# now_time = datetime_obj.replace(tzinfo=timezone('America/New_York'))
#elif re.match(r'^edt', tz_str):
# now_time = datetime_obj.replace(tzinfo=timezone('America/New_York'))
#elif re.match(r'^CST', tz_str):
# now_time = datetime_obj.replace(tzinfo=timezone('America/Chicago'))
else:
now_time = datetime_obj.replace(tzinfo=timezone('UTC'))
# Conver to London time zone
now_london = now_time.astimezone(timezone('Europe/London'))
await bot.say (now_london.strftime(format) + " (London)")
# Convert to US/Eastern time zone
now_us_east = now_time.astimezone(timezone('America/New_York'))
await bot.say (now_us_east.strftime(format) + " (Eastern)")
# Convert to US/Central time zone
now_central = now_time.astimezone(timezone('America/Chicago'))
await bot.say (now_central.strftime(format) + " (Central)")
# Convert to US/Mountain time zone
now_mountain = now_time.astimezone(timezone('America/Denver'))
await bot.say (now_mountain.strftime(format) + " (Mountain)")
# Convert to US/Pacific time zone
now_pacific = now_time.astimezone(timezone('America/Los_Angeles'))
await bot.say (now_pacific.strftime(format) + " (Pacific)")
bot.run('######')
使用命令“!convert 2018-07-25 3:00 PM UTC”正确输出以下内容:
2018-07-25 04:00 PM(伦敦) 2018-07-25 11:00 AM(东部) 2018-07-25 10:00 AM(中部) 2018-07-25 09:00 AM(山) 2018-07-25 08:00 AM(太平洋)
但是命令“!convert 2018-07-25 3:00 PM EST”会输出以下内容:
2018-07-25 08:56 PM(伦敦) 2018-07-25 03:00 PM(东部) 2018-07-25 02:56 PM(中部) 2018-07-25 01:56 PM(山) 2018-07-25 12:56 PM(太平洋)
我希望我的朋友能够在任何时区的任何时间投入。我删除了检查tz_str以专注于EST进行故障排除的其他行。我在做什么错了?