|
@@ -20,27 +20,21 @@ |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# SOFTWARE. |
|
|
# SOFTWARE. |
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta |
|
|
|
|
|
|
|
|
from datetime import datetime |
|
|
from math import floor |
|
|
from math import floor |
|
|
from time import time |
|
|
from time import time |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
import pytz |
|
|
|
|
|
except ImportError: |
|
|
|
|
|
pytz = None |
|
|
|
|
|
|
|
|
from earwigbot.commands import BaseCommand |
|
|
from earwigbot.commands import BaseCommand |
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
class Command(BaseCommand): |
|
|
"""Report the current time in any timezone (UTC default), or in beats.""" |
|
|
"""Report the current time in any timezone (UTC default), or in beats.""" |
|
|
name = "time" |
|
|
name = "time" |
|
|
commands = ["time", "beats", "swatch"] |
|
|
commands = ["time", "beats", "swatch"] |
|
|
timezones = [ |
|
|
|
|
|
"UTC": 0, |
|
|
|
|
|
"EST": -5, |
|
|
|
|
|
"EDT": -4, |
|
|
|
|
|
"CST": -6, |
|
|
|
|
|
"CDT": -5, |
|
|
|
|
|
"MST": -7, |
|
|
|
|
|
"MDT": -6, |
|
|
|
|
|
"PST": -8, |
|
|
|
|
|
"PDT": -7, |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def process(self, data): |
|
|
def process(self, data): |
|
|
if data.command in ["beats", "swatch"]: |
|
|
if data.command in ["beats", "swatch"]: |
|
@@ -61,10 +55,14 @@ class Command(BaseCommand): |
|
|
self.reply(data, "@{0:0>3}".format(beats)) |
|
|
self.reply(data, "@{0:0>3}".format(beats)) |
|
|
|
|
|
|
|
|
def do_time(self, data, timezone): |
|
|
def do_time(self, data, timezone): |
|
|
now = datetime.utcnow() |
|
|
|
|
|
|
|
|
if not pytz: |
|
|
|
|
|
msg = "this command requires the 'pytz' module: http://pytz.sourceforge.net/" |
|
|
|
|
|
self.reply(data, msg) |
|
|
|
|
|
return |
|
|
try: |
|
|
try: |
|
|
now += timedelta(hours=self.timezones[timezone]) # Timezone offset |
|
|
|
|
|
except KeyError: |
|
|
|
|
|
|
|
|
tzinfo = pytz.timezone(timezone) |
|
|
|
|
|
except pytz.exceptions.UnknownTimeZoneError: |
|
|
self.reply(data, "unknown timezone: {0}.".format(timezone)) |
|
|
self.reply(data, "unknown timezone: {0}.".format(timezone)) |
|
|
return |
|
|
return |
|
|
self.reply(data, now.strftime("%Y-%m-%d %H:%M:%S") + " " + timezone) |
|
|
|
|
|
|
|
|
now = pytz.utc.localize(datetime.utcnow()).astimezone(tzinfo) |
|
|
|
|
|
self.reply(data, now.strftime("%Y-%m-%d %H:%M:%S %Z")) |