Welcome, guest | Sign In | My Account | Store | Cart

This recipe demonstrates a fictional time system from the world of Aens. It is mainly for showing how, in a game, a new system of time could alter a player's perspective of location in the game.

Python, 119 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119``` ```'''Module for Aens time conversions. This module provides several functions that allow conversion from earth seconds to Aens time units.''' __version__ = '1.0' import sys as _sys import thread as _thread import time as _time ################################################################################ EPOCH_CORRECTION = 946684800 MILLE_PER_DAY = 1000000 KHILIOI_PER_DAY = 1000 DAYS_PER_WEEK = 7 WEEKS_PER_MONTH = 4 MONTHS_PER_SEASON = 3 SEASONS_PER_YEAR = 4 SECONDS_PER_DAY = 86400 SECONDS_PER_WEEK = SECONDS_PER_DAY * DAYS_PER_WEEK SECONDS_PER_MONTH = SECONDS_PER_WEEK * WEEKS_PER_MONTH SECONDS_PER_SEASON = SECONDS_PER_MONTH * MONTHS_PER_SEASON SECONDS_PER_YEAR = SECONDS_PER_SEASON * SEASONS_PER_YEAR ################################################################################ def seconds(): 'Return seconds since the epoch.' return _time.time() - EPOCH_CORRECTION def mille(seconds): 'Convert from seconds to mille.' return int(seconds % SECONDS_PER_DAY * MILLE_PER_DAY / SECONDS_PER_DAY % KHILIOI_PER_DAY) def khilioi(seconds): 'Convert from seconds to khilioi.' return int(seconds % SECONDS_PER_DAY * KHILIOI_PER_DAY / SECONDS_PER_DAY) def day(seconds): 'Convert from seconds to day.' return int(seconds) / SECONDS_PER_DAY % DAYS_PER_WEEK + 1 def week(seconds): 'Convert from seconds to week.' return int(seconds) / SECONDS_PER_WEEK % WEEKS_PER_MONTH + 1 def month(seconds): 'Convert from seconds to month.' return int(seconds) / SECONDS_PER_MONTH % MONTHS_PER_SEASON + 1 def season(seconds): 'Convert from seconds to season.' return int(seconds) / SECONDS_PER_SEASON % SEASONS_PER_YEAR + 1 def year(seconds): 'Convert from seconds to year.' return int(seconds) / SECONDS_PER_YEAR + 1 ################################################################################ UNITS = year, season, month, week, day, khilioi, mille def format(seconds, format='%u.%u.%u.%u.%u.%03u.%03u', units=UNITS): 'Convert from seconds to string.' return format % tuple(function(seconds) for function in units) ################################################################################ class Mille_Timer: 'Mille_Timer(function, *args, **kwargs) -> Mille Timer' def __init__(self, function, *args, **kwargs): 'Initialize the Mille_Timer object.' self.__function = function self.__args = args self.__kwargs = kwargs self.__status = False self.__thread = False self.__lock = _thread.allocate_lock() def start(self): 'Start the Mille_Timer object.' self.__lock.acquire() self.__status = True if not self.__thread: self.__thread = True _thread.start_new_thread(self.__run, ()) self.__lock.release() def stop(self): 'Stop the Mille_Timer object.' self.__lock.acquire() self.__status = False self.__lock.release() def __run(self): 'Private class method.' start, next = _time.time(), 0 while True: next += 1 sleep = start + next * 0.0864 - _time.time() assert sleep >= 0, 'Function Was Too Slow' _time.sleep(sleep) self.__lock.acquire() if not self.__status: self.__thread = False break self.__lock.release() self.__function(*self.__args, **self.__kwargs) self.__lock.release() ################################################################################ if __name__ == '__main__': _sys.stdout.write('Content-Type: text/plain\n\n') _sys.stdout.write(file(_sys.argv[0]).read()) ```

Please see "Aens Time Clock" for example usage of this module.

 Created by Stephen Chappell on Tue, 20 Feb 2007 (PSF)