From ebb8b316015b171d94d5f267abce7914f11bebb9 Mon Sep 17 00:00:00 2001 From: aikaterna <20862007+aikaterna@users.noreply.github.com> Date: Wed, 29 Jan 2020 08:36:27 -0800 Subject: [PATCH] [Timezone] Add time compare --- timezone/timezone.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/timezone/timezone.py b/timezone/timezone.py index 7f1e730..df8d450 100644 --- a/timezone/timezone.py +++ b/timezone/timezone.py @@ -141,3 +141,33 @@ class Timezone(commands.Cog): ) else: await ctx.send("That user hasn't set their timezone.") + + @time.command() + async def compare(self, ctx, user: discord.Member = None): + """Compare your saved timezone with another user's timezone.""" + usertime = await self.config.user(ctx.message.author).usertime() + othertime = await self.config.user(user).usertime() + + if not usertime: + return await ctx.send( + "You haven't set your timezone. Do `[p]time me Continent/City`: " + "see " + ) + if not othertime: + return await ctx.send(f"That user's timezone isn't set yet.") + + user_now = datetime.now(pytz.timezone(usertime)) + user_diff = user_now.utcoffset().total_seconds() / 60 / 60 + other_now = datetime.now(pytz.timezone(othertime)) + other_diff = other_now.utcoffset().total_seconds() / 60 / 60 + time_diff = int(abs(user_diff - other_diff)) + fmt = "**%H:%M %Z (UTC %z)**" + other_time = other_now.strftime(fmt) + plural = "" if time_diff == 1 else "s" + time_amt = "the same time zone as you" if time_diff == 0 else f"{time_diff} hour{plural}" + position = "ahead of" if user_diff < other_diff else "behind" + position_text = "" if time_diff == 0 else f" {position} you" + + await ctx.send( + f"{user.display_name}'s time is {other_time} which is {time_amt}{position_text}." + )