2014-11-02 02:02:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Nemubot is a smart and modulable IM bot.
|
|
|
|
# Copyright (C) 2012-2014 nemunaire
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2014-09-30 21:51:14 +00:00
|
|
|
from datetime import datetime, timezone
|
2014-08-29 15:13:28 +00:00
|
|
|
import time
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
|
2014-08-29 15:13:28 +00:00
|
|
|
def countdown(delta, resolution=5):
|
|
|
|
sec = delta.seconds
|
|
|
|
hours, remainder = divmod(sec, 3600)
|
|
|
|
minutes, seconds = divmod(remainder, 60)
|
|
|
|
an = int(delta.days / 365.25)
|
|
|
|
days = delta.days % 365.25
|
|
|
|
|
|
|
|
sentence = ""
|
|
|
|
force = False
|
|
|
|
|
|
|
|
if resolution > 0 and (force or an > 0):
|
|
|
|
force = True
|
2014-11-09 13:11:54 +00:00
|
|
|
sentence += " %i an" % an
|
2014-08-29 15:13:28 +00:00
|
|
|
|
|
|
|
if an > 1:
|
|
|
|
sentence += "s"
|
|
|
|
if resolution > 2:
|
|
|
|
sentence += ","
|
|
|
|
elif resolution > 1:
|
|
|
|
sentence += " et"
|
|
|
|
|
|
|
|
if resolution > 1 and (force or days > 0):
|
|
|
|
force = True
|
2014-11-09 13:11:54 +00:00
|
|
|
sentence += " %i jour" % days
|
2014-08-29 15:13:28 +00:00
|
|
|
|
|
|
|
if days > 1:
|
|
|
|
sentence += "s"
|
|
|
|
if resolution > 3:
|
|
|
|
sentence += ","
|
|
|
|
elif resolution > 2:
|
|
|
|
sentence += " et"
|
|
|
|
|
|
|
|
if resolution > 2 and (force or hours > 0):
|
|
|
|
force = True
|
2014-11-09 13:11:54 +00:00
|
|
|
sentence += " %i heure" % hours
|
2014-08-29 15:13:28 +00:00
|
|
|
if hours > 1:
|
|
|
|
sentence += "s"
|
|
|
|
if resolution > 4:
|
|
|
|
sentence += ","
|
|
|
|
elif resolution > 3:
|
|
|
|
sentence += " et"
|
|
|
|
|
|
|
|
if resolution > 3 and (force or minutes > 0):
|
|
|
|
force = True
|
2014-11-09 13:11:54 +00:00
|
|
|
sentence += " %i minute" % minutes
|
2014-08-29 15:13:28 +00:00
|
|
|
if minutes > 1:
|
|
|
|
sentence += "s"
|
|
|
|
if resolution > 4:
|
|
|
|
sentence += " et"
|
|
|
|
|
|
|
|
if resolution > 4 and (force or seconds > 0):
|
|
|
|
force = True
|
2014-11-09 13:11:54 +00:00
|
|
|
sentence += " %i seconde" % seconds
|
2014-08-29 15:13:28 +00:00
|
|
|
if seconds > 1:
|
|
|
|
sentence += "s"
|
|
|
|
return sentence[1:]
|
|
|
|
|
|
|
|
|
2014-09-30 21:51:14 +00:00
|
|
|
def countdown_format(date, msg_before, msg_after, tz=None):
|
2014-11-09 13:11:54 +00:00
|
|
|
"""Replace in a text %s by a sentence incidated the remaining time
|
|
|
|
before/after an event"""
|
|
|
|
if tz is not None:
|
2014-09-30 21:51:14 +00:00
|
|
|
oldtz = os.environ['TZ']
|
|
|
|
os.environ['TZ'] = tz
|
2014-08-29 15:13:28 +00:00
|
|
|
time.tzset()
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
# Calculate time before the date
|
2014-10-09 05:30:04 +00:00
|
|
|
try:
|
|
|
|
if datetime.now(timezone.utc) > date:
|
|
|
|
sentence_c = msg_after
|
|
|
|
delta = datetime.now(timezone.utc) - date
|
|
|
|
else:
|
|
|
|
sentence_c = msg_before
|
|
|
|
delta = date - datetime.now(timezone.utc)
|
|
|
|
except TypeError:
|
|
|
|
if datetime.now() > date:
|
|
|
|
sentence_c = msg_after
|
|
|
|
delta = datetime.now() - date
|
|
|
|
else:
|
|
|
|
sentence_c = msg_before
|
|
|
|
delta = date - datetime.now()
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
if tz is not None:
|
2014-09-30 21:51:14 +00:00
|
|
|
os.environ['TZ'] = oldtz
|
2014-08-29 15:13:28 +00:00
|
|
|
|
|
|
|
return sentence_c % countdown(delta)
|