Total python novice here. Trying to make a discord bot to help organize friends in different timezones to play together. The following code gives correct time info using the UTC argument, but not EST. The EST times are always 56 minutes ahead.
# 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('######')
Using the command " !convert 2018-07-25 3:00 PM UTC " properly outputs this:
2018-07-25 04:00 PM (London) 2018-07-25 11:00 AM (Eastern) 2018-07-25 10:00 AM (Central) 2018-07-25 09:00 AM (Mountain) 2018-07-25 08:00 AM (Pacific)
But the command " !convert 2018-07-25 3:00 PM EST " outputs this:
2018-07-25 08:56 PM (London) 2018-07-25 03:00 PM (Eastern) 2018-07-25 02:56 PM (Central) 2018-07-25 01:56 PM (Mountain) 2018-07-25 12:56 PM (Pacific)
I want my friends to be able to put in any time in any timezone. I removed the other lines where the tz_str is checked to focus on EST for troubleshooting. What am I doing wrong?