skybot icon indicating copy to clipboard operation
skybot copied to clipboard

Implemented yourtime plugin

Open Mause opened this issue 12 years ago • 5 comments

Put simply, the yourtime plugin allows users to set their current timezone via; .myzone gmt+8 and for other users to determine another users current time via .yourtime Lord_DeathMatch

Currently uses user supplied timezone without extra computation; ie, dst is not accounted for. If it must be implemented before you will merge, let me know :) (i am Lord_Deathmatch on IRC)

Mause avatar Oct 25 '12 14:10 Mause

I have looked at the PyTZ library as you requested, and have thought about implementation. From the onset, however, i believe it would be too fiddly to implement correctly. Rather than having the user recall a simple number, the user must recall and correctly spell (harder for some than you might imagine) a timezone name, such as Australia/Perth

Example code follows

from datetime import datetime
from pytz import timezone, utc

utc_dt = datetime.now(utc)
loc_dt = utc_dt.astimezone(timezone('Australia/Perth'))
fmt = 'For Lord_DeathMatch it is %A in the month of %B, %I:%M%p %Z (%z)'
print loc_dt.strftime(fmt)

which outputs; For Lord_DeathMatch it is Saturday in the month of October, 09:08PM WST (+0800)

which, compared to the current implementation is quite nice and small;

from datetime import timedelta, datetime, tzinfo

class GMT(tzinfo):
    def __init__(self, difference):
        self.difference = difference

    def utcoffset(self, dt):
        return timedelta(hours=self.difference) + self.dst(dt)

    def dst(self, dt):
        return timedelta(0)

print datetime.now(GMT(+8)).ctime()

which outputs Sat Oct 27 21:09:29 2012

Mause avatar Oct 27 '12 12:10 Mause

I have given it some thought, and i have resolved that this would be best implemented using PyTZ and a timezone listing hosted online that skybot refers users to when they input an invalid timezone.

Mause avatar Oct 28 '12 12:10 Mause

You could search PyTZ.common_timezones for a match to whatever the user entered.

Another idea is to fold this into the weather plugin, which already remembers locations that people passed it. The wunderground API results include a timezone field.

rmmh avatar Oct 28 '12 18:10 rmmh

I would really rather not restrict users to simple the common time zones; i would rather use all_timezones instead (hackers are everywhere :P)

The weather plugin integration would be a little fiddlesome; if I had to use it, I would re-work it so that it uses a command that is shared between the weather plugin and the yourtime plugin. And such a command is already implemented in the yourtime plugin.

Mause avatar Nov 02 '12 05:11 Mause

I just looked at the description for all_timezones and agree it would be more useful to use up to date timezones.

Mause avatar Nov 02 '12 05:11 Mause