Skip to content

Instantly share code, notes, and snippets.

@ivey
Created October 26, 2015 05:15
Show Gist options
  • Save ivey/50223c025111475369d2 to your computer and use it in GitHub Desktop.
Save ivey/50223c025111475369d2 to your computer and use it in GitHub Desktop.
from math import floor
from datetime import datetime, timedelta
from time import mktime
import plugins
def _initialise(bot):
plugins.register_handler(_handle_cp_action)
def _handle_cp_action(bot, event, command):
if event.text.startswith('+cp'):
cps = current_cycle()
n = next_checkpoint(cps)
c = cycle_end(cps)
yield from bot.coro_send_message(event.conv, _("Next checkpoint is {} <b>{}</b>.\nThis cycle ends {} <b>{}</b>.").format(n['date'], n['time'], c['date'], c['time']))
else:
pass
EPOCH=1389150000000
CYCLE_LENGTH=630000000
CHECKPOINT_LENGTH=18000
def current_cycle():
present = datetime.now()
tt = datetime.timetuple(present)
now = mktime(tt) * 1000
cycle = floor((now - EPOCH) / CYCLE_LENGTH)
cycle_display = cycle + 1
start = datetime.fromtimestamp(CHECKPOINT_LENGTH+((EPOCH + (cycle*CYCLE_LENGTH))/1000))
year = start.year
checkpoints = []
for i in range(0, 35):
next = (start > present) and (present + timedelta(seconds=CHECKPOINT_LENGTH)) > start
cp = {
'date': start.strftime('%a %d %b'),
'time': start.strftime('%I:%M%p'),
'next': next,
'past': (start < present)
}
checkpoints.append(cp)
start = start + timedelta(seconds=CHECKPOINT_LENGTH)
return checkpoints
def next_checkpoint(cps):
future = [cp for cp in cps if cp['past'] == False]
return future[0]
def cycle_end(cps):
return cps[-1]
# def main():
# cps = current_cycle()
# print next_checkpoint(cps)
# print cycle_end(cps)
# if __name__ == "__main__":
# main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment