Internal timezone is now UTC

This commit is contained in:
nemunaire 2014-09-30 23:51:14 +02:00
parent 32cb79344b
commit c5a69f1bd0
10 changed files with 54 additions and 58 deletions

9
bot.py
View File

@ -16,8 +16,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime from datetime import datetime, timedelta, timezone
from datetime import timedelta
import logging import logging
from queue import Queue from queue import Queue
import re import re
@ -277,8 +276,8 @@ class Bot(threading.Thread):
if len(self.events) > 0: if len(self.events) > 0:
logger.debug("Update timer: next event in %d seconds", logger.debug("Update timer: next event in %d seconds",
self.events[0].time_left.seconds) self.events[0].time_left.seconds)
if datetime.now() + timedelta(seconds=5) >= self.events[0].current: if datetime.now(timezone.utc) + timedelta(seconds=5) >= self.events[0].current:
while datetime.now() < self.events[0].current: while datetime.now(timezone.utc) < self.events[0].current:
time.sleep(0.6) time.sleep(0.6)
self._end_event_timer() self._end_event_timer()
else: else:
@ -292,7 +291,7 @@ class Bot(threading.Thread):
def _end_event_timer(self): def _end_event_timer(self):
"""Function called at the end of the event timer""" """Function called at the end of the event timer"""
while len(self.events) > 0 and datetime.now() >= self.events[0].current: while len(self.events) > 0 and datetime.now(timezone.utc) >= self.events[0].current:
evt = self.events.pop(0) evt = self.events.pop(0)
self.cnsr_queue.put_nowait(EventConsumer(evt)) self.cnsr_queue.put_nowait(EventConsumer(evt))
self._launch_consumers() self._launch_consumers()

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
class ModuleEvent: class ModuleEvent:
@ -77,7 +77,7 @@ class ModuleEvent:
"""Return the date of the near check""" """Return the date of the near check"""
if self.times != 0: if self.times != 0:
if self._end is None: if self._end is None:
self._end = datetime.now() + self.offset + self.interval self._end = datetime.now(timezone.utc) + self.offset + self.interval
return self._end return self._end
return None return None
@ -88,7 +88,7 @@ class ModuleEvent:
if self.times != 0: if self.times != 0:
if self._end is None: if self._end is None:
return self.current return self.current
elif self._end < datetime.now(): elif self._end < datetime.now(timezone.utc):
self._end += self.interval self._end += self.interval
return self._end return self._end
return None return None
@ -98,7 +98,7 @@ class ModuleEvent:
def time_left(self): def time_left(self):
"""Return the time left before/after the near check""" """Return the time left before/after the near check"""
if self.current is not None: if self.current is not None:
return self.current - datetime.now() return self.current - datetime.now(timezone.utc)
return 99999 #TODO: 99999 is not a valid time to return return 99999 #TODO: 99999 is not a valid time to return

View File

@ -4,7 +4,7 @@
import re import re
import sys import sys
from datetime import datetime from datetime import datetime, timezone
from hooks import hook from hooks import hook
@ -41,9 +41,7 @@ def get_variable(name, msg=None):
elif name == "chan" or name == "channel": elif name == "chan" or name == "channel":
return msg.channel return msg.channel
elif name == "date": elif name == "date":
now = datetime.now() return datetime.now(timezone.utc).strftime("%c")
return ("%d/%d/%d %d:%d:%d"%(now.day, now.month, now.year, now.hour,
now.minute, now.second))
elif name in DATAS.getNode("variables").index: elif name in DATAS.getNode("variables").index:
return DATAS.getNode("variables").index[name]["value"] return DATAS.getNode("variables").index[name]["value"]
else: else:

View File

@ -2,7 +2,7 @@
"""Wishes Happy New Year when the time comes""" """Wishes Happy New Year when the time comes"""
from datetime import datetime from datetime import datetime, timezone
from hooks import hook from hooks import hook
from tools.countdown import countdown_format from tools.countdown import countdown_format
@ -11,15 +11,15 @@ nemubotversion = 3.4
from more import Response from more import Response
yr = datetime.today().year yr = datetime.now(timezone.utc).year
yrn = datetime.today().year + 1 yrn = datetime.now(timezone.utc).year + 1
def load(context): def load(context):
d = datetime(yrn, 1, 1, 0, 0, 0) - datetime.now() d = datetime(yrn, 1, 1, 0, 0, 0) - datetime.now(timezone.utc)
add_event(ModuleEvent(interval=0, offset=d.total_seconds(), call=bonneannee)) add_event(ModuleEvent(interval=0, offset=d.total_seconds(), call=bonneannee))
def bonneannee(): def bonneannee():
txt = "Bonne année %d !" % datetime.today().year txt = "Bonne année %d !" % yrn
print (txt) print (txt)
send_response("localhost:2771", Response(txt, "#epitagueule")) send_response("localhost:2771", Response(txt, "#epitagueule"))
send_response("localhost:2771", Response(txt, "#yaka")) send_response("localhost:2771", Response(txt, "#yaka"))
@ -31,7 +31,7 @@ def bonneannee():
@hook("cmd_hook", "newyear") @hook("cmd_hook", "newyear")
@hook("cmd_hook", str(yrn), yrn) @hook("cmd_hook", str(yrn), yrn)
def cmd_newyear(msg, yr): def cmd_newyear(msg, yr):
return Response(countdown_format(datetime(yr, 1, 1, 0, 0, 1), return Response(countdown_format(datetime(yr, 1, 1, 0, 0, 1, 0, timezone.utc),
"Il reste %s avant la nouvelle année.", "Il reste %s avant la nouvelle année.",
"Nous faisons déjà la fête depuis %s !"), "Nous faisons déjà la fête depuis %s !"),
channel=msg.channel) channel=msg.channel)
@ -43,7 +43,7 @@ def cmd_timetoyear(msg, cur):
if yr == cur: if yr == cur:
return None return None
return Response(countdown_format(datetime(yr, 1, 1, 0, 0, 1), return Response(countdown_format(datetime(yr, 1, 1, 0, 0, 1, 0, timezone.utc),
"Il reste %s avant %d." % ("%s", yr), "Il reste %s avant %d." % ("%s", yr),
"Le premier janvier %d est passé depuis %s !" % (yr, "%s")), "Le premier janvier %d est passé depuis %s !" % (yr, "%s")),
channel=msg.channel) channel=msg.channel)

View File

@ -5,8 +5,7 @@
import imp import imp
import re import re
import sys import sys
from datetime import timedelta from datetime import datetime, timedelta, timezone
from datetime import datetime
import time import time
import threading import threading
import traceback import traceback
@ -32,7 +31,7 @@ def load(context):
if DATAS.index[evt].hasAttribute("end"): if DATAS.index[evt].hasAttribute("end"):
event = ModuleEvent(call=fini, call_data=dict(strend=DATAS.index[evt])) event = ModuleEvent(call=fini, call_data=dict(strend=DATAS.index[evt]))
event._end = DATAS.index[evt].getDate("end") event._end = DATAS.index[evt].getDate("end")
idt = context.add_event(event) idt = add_event(event)
if idt is not None: if idt is not None:
DATAS.index[evt]["id"] = idt DATAS.index[evt]["id"] = idt
@ -44,8 +43,8 @@ def fini(d, strend):
@hook("cmd_hook", "goûter") @hook("cmd_hook", "goûter")
def cmd_gouter(msg): def cmd_gouter(msg):
ndate = datetime.today() ndate = datetime.now(timezone.utc)
ndate = datetime(ndate.year, ndate.month, ndate.day, 16, 42) ndate = datetime(ndate.year, ndate.month, ndate.day, 16, 42, 0, 0, timezone.utc)
return Response(countdown_format(ndate, return Response(countdown_format(ndate,
"Le goûter aura lieu dans %s, préparez vos biscuits !", "Le goûter aura lieu dans %s, préparez vos biscuits !",
"Nous avons %s de retard pour le goûter :("), "Nous avons %s de retard pour le goûter :("),
@ -53,8 +52,8 @@ def cmd_gouter(msg):
@hook("cmd_hook", "week-end") @hook("cmd_hook", "week-end")
def cmd_we(msg): def cmd_we(msg):
ndate = datetime.today() + timedelta(5 - datetime.today().weekday()) ndate = datetime.now(timezone.utc) + timedelta(5 - datetime.today().weekday())
ndate = datetime(ndate.year, ndate.month, ndate.day, 0, 0, 1) ndate = datetime(ndate.year, ndate.month, ndate.day, 0, 0, 1, 0, timezone.utc)
return Response(countdown_format(ndate, return Response(countdown_format(ndate,
"Il reste %s avant le week-end, courage ;)", "Il reste %s avant le week-end, courage ;)",
"Youhou, on est en week-end depuis %s."), "Youhou, on est en week-end depuis %s."),
@ -95,14 +94,14 @@ def start_countdown(msg):
if result2 is None or result2.group(4) is None: yea = now.year if result2 is None or result2.group(4) is None: yea = now.year
else: yea = int(result2.group(4)) else: yea = int(result2.group(4))
if result2 is not None and result3 is not None: if result2 is not None and result3 is not None:
strnd["end"] = datetime(yea, int(result2.group(3)), int(result2.group(2)), hou, minu, sec) strnd["end"] = datetime(yea, int(result2.group(3)), int(result2.group(2)), hou, minu, sec, timezone.utc)
elif result2 is not None: elif result2 is not None:
strnd["end"] = datetime(int(result2.group(4)), int(result2.group(3)), int(result2.group(2))) strnd["end"] = datetime(int(result2.group(4)), int(result2.group(3)), int(result2.group(2)), 0, 0, 0, timezone.utc)
elif result3 is not None: elif result3 is not None:
if hou * 3600 + minu * 60 + sec > now.hour * 3600 + now.minute * 60 + now.second: if hou * 3600 + minu * 60 + sec > now.hour * 3600 + now.minute * 60 + now.second:
strnd["end"] = datetime(now.year, now.month, now.day, hou, minu, sec) strnd["end"] = datetime(now.year, now.month, now.day, hou, minu, sec, timezone.utc)
else: else:
strnd["end"] = datetime(now.year, now.month, now.day + 1, hou, minu, sec) strnd["end"] = datetime(now.year, now.month, now.day + 1, hou, minu, sec, timezone.utc)
evt._end = strnd.getDate("end") evt._end = strnd.getDate("end")
strnd["id"] = add_event(evt) strnd["id"] = add_event(evt)
except: except:

View File

@ -4,8 +4,7 @@
import re import re
import imp import imp
from datetime import datetime from datetime import datetime, timedelta, timezone
from datetime import timedelta
from hooks import hook from hooks import hook
@ -22,9 +21,9 @@ def cmd_sleep(msg):
msg.cmds[1]) is not None: msg.cmds[1]) is not None:
# First, parse the hour # First, parse the hour
p = re.match("([0-9]{1,2})[h':.,-]([0-9]{1,2})?[m':.,-]?", msg.cmds[1]) p = re.match("([0-9]{1,2})[h':.,-]([0-9]{1,2})?[m':.,-]?", msg.cmds[1])
f = [datetime(datetime.today().year, f = [datetime(datetime.now(timezone.utc).year,
datetime.today().month, datetime.now(timezone.utc).month,
datetime.today().day, datetime.now(timezone.utc).day,
hour=int(p.group(1)))] hour=int(p.group(1)))]
if p.group(2) is not None: if p.group(2) is not None:
f[0] += timedelta(minutes=int(p.group(2))) f[0] += timedelta(minutes=int(p.group(2)))
@ -37,7 +36,7 @@ def cmd_sleep(msg):
# Just get awake times # Just get awake times
else: else:
f = [datetime.now() + timedelta(minutes=15)] f = [datetime.now(timezone.utc) + timedelta(minutes=15)]
g = list() g = list()
for i in range(0,6): for i in range(0,6):
f.append(f[i] + timedelta(hours=1,minutes=30)) f.append(f[i] + timedelta(hours=1,minutes=30))

View File

@ -2,7 +2,7 @@
"""The 2014 football worldcup module""" """The 2014 football worldcup module"""
import datetime from datetime import datetime, timezone
import json import json
import re import re
from urllib.parse import quote from urllib.parse import quote
@ -32,7 +32,7 @@ def start_watch(msg):
w["channel"] = msg.channel w["channel"] = msg.channel
w["proprio"] = msg.nick w["proprio"] = msg.nick
w["sender"] = msg.sender w["sender"] = msg.sender
w["start"] = datetime.datetime.now() w["start"] = datetime.now(timezone.utc)
DATAS.addChild(w) DATAS.addChild(w)
save() save()
raise IRCException("This channel is now watching world cup events!") raise IRCException("This channel is now watching world cup events!")
@ -125,7 +125,7 @@ def txt_event(e):
return "%se minutes : %s %s (%s)" % (e["time"], detail_event(e["type_of_event"]), e["player"], e["team"]["code"]) return "%se minutes : %s %s (%s)" % (e["time"], detail_event(e["type_of_event"]), e["player"], e["team"]["code"])
def prettify(match): def prettify(match):
matchdate_local = datetime.datetime.strptime(match["datetime"].replace(':', ''), "%Y-%m-%dT%H%M%S.%f%z") matchdate_local = datetime.strptime(match["datetime"].replace(':', ''), "%Y-%m-%dT%H%M%S.%f%z")
matchdate = matchdate_local - (matchdate_local.utcoffset() - datetime.timedelta(hours=2)) matchdate = matchdate_local - (matchdate_local.utcoffset() - datetime.timedelta(hours=2))
if match["status"] == "future": if match["status"] == "future":
return ["Match à venir (%s) le %s : %s vs. %s" % (match["match_number"], matchdate.strftime("%A %d à %H:%M"), match["home_team"]["country"], match["away_team"]["country"])] return ["Match à venir (%s) le %s : %s vs. %s" % (match["match_number"], matchdate.strftime("%A %d à %H:%M"), match["home_team"]["country"], match["away_team"]["country"])]
@ -135,7 +135,7 @@ def prettify(match):
if match["status"] == "completed": if match["status"] == "completed":
msg += "Match (%s) du %s terminé : " % (match["match_number"], matchdate.strftime("%A %d à %H:%M")) msg += "Match (%s) du %s terminé : " % (match["match_number"], matchdate.strftime("%A %d à %H:%M"))
else: else:
msg += "Match en cours (%s) depuis %d minutes : " % (match["match_number"], (datetime.datetime.now(matchdate.tzinfo) - matchdate_local).seconds / 60) msg += "Match en cours (%s) depuis %d minutes : " % (match["match_number"], (datetime.now(matchdate.tzinfo) - matchdate_local).seconds / 60)
msg += "%s %d - %d %s" % (match["home_team"]["country"], match["home_team"]["goals"], match["away_team"]["goals"], match["away_team"]["country"]) msg += "%s %d - %d %s" % (match["home_team"]["country"], match["home_team"]["goals"], match["away_team"]["goals"], match["away_team"]["country"])

View File

@ -16,8 +16,10 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime import calendar
from datetime import datetime, timezone
import re import re
import time
import shlex import shlex
import bot import bot
@ -194,7 +196,7 @@ class IRCServer(SocketServer):
def read(self): def read(self):
for line in SocketServer.read(self): for line in SocketServer.read(self):
msg = IRCMessage(line, datetime.now()) msg = IRCMessage(line, datetime.now(timezone.utc))
if msg.cmd in self.hookscmd: if msg.cmd in self.hookscmd:
self.hookscmd[msg.cmd](msg) self.hookscmd[msg.cmd](msg)
@ -279,8 +281,7 @@ class IRCMessage:
"""Add an IRCv3.2 Message Tags""" """Add an IRCv3.2 Message Tags"""
# Treat special tags # Treat special tags
if key == "time": if key == "time":
# TODO: this is UTC timezone, nemubot works with local timezone value = datetime.fromtimestamp(calendar.timegm(time.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")), timezone.utc)
value = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
# Store tag # Store tag
self.tags[key] = value self.tags[key] = value

View File

@ -1,4 +1,4 @@
from datetime import datetime from datetime import datetime, timezone
import time import time
def countdown(delta, resolution=5): def countdown(delta, resolution=5):
@ -59,21 +59,22 @@ def countdown(delta, resolution=5):
return sentence[1:] return sentence[1:]
def countdown_format(date, msg_before, msg_after, timezone=None): def countdown_format(date, msg_before, msg_after, tz=None):
"""Replace in a text %s by a sentence incidated the remaining time before/after an event""" """Replace in a text %s by a sentence incidated the remaining time before/after an event"""
if timezone != None: if tz != None:
os.environ['TZ'] = timezone oldtz = os.environ['TZ']
os.environ['TZ'] = tz
time.tzset() time.tzset()
#Calculate time before the date #Calculate time before the date
if datetime.now() > date: if datetime.now(timezone.utc) > date:
sentence_c = msg_after sentence_c = msg_after
delta = datetime.now() - date delta = datetime.now(timezone.utc) - date
else: else:
sentence_c = msg_before sentence_c = msg_before
delta = date - datetime.now() delta = date - datetime.now(timezone.utc)
if timezone != None: if tz != None:
os.environ['TZ'] = "Europe/Paris" os.environ['TZ'] = oldtz
return sentence_c % countdown(delta) return sentence_c % countdown(delta)

View File

@ -1,8 +1,7 @@
# coding=utf-8 # coding=utf-8
import xml.sax import xml.sax
from datetime import datetime from datetime import datetime, timezone
from datetime import date
import logging import logging
import sys import sys
import time import time
@ -67,12 +66,12 @@ class ModuleState:
return source return source
else: else:
try: try:
return datetime.fromtimestamp(float(source)) return datetime.fromtimestamp(float(source), timezone.utc)
except ValueError: except ValueError:
while True: while True:
try: try:
return datetime.fromtimestamp(time.mktime( return datetime.fromtimestamp(time.mktime(
time.strptime(source[:19], "%Y-%m-%d %H:%M:%S"))) time.strptime(source[:19], "%Y-%m-%d %H:%M:%S")), timezone.utc)
except ImportError: except ImportError:
pass pass