<s> import time <EOL> import datetime <EOL> def loadTimezones ( db ) : <EOL> """<STR_LIT>""" <EOL> import configuration <EOL> def add ( name , abbrev , utc_offset ) : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( name , abbrev , utc_offset ) ) <EOL> if configuration . database . DRIVER == "<STR_LIT>" : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> add ( "<STR_LIT>" , "<STR_LIT>" , datetime . timedelta ( ) ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> for full_name , abbrev , utc_offset in cursor . fetchall ( ) : <EOL> region , _ , name = full_name . partition ( "<STR_LIT:/>" ) <EOL> if region not in ( "<STR_LIT>" , "<STR_LIT>" ) and name and "<STR_LIT:/>" not in name : <EOL> add ( full_name , abbrev , utc_offset ) <EOL> db . commit ( ) <EOL> def updateTimezones ( db ) : <EOL> """<STR_LIT>""" <EOL> import configuration <EOL> if configuration . database . DRIVER == "<STR_LIT>" : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> db . commit ( ) <EOL> def __fetchTimezones ( db ) : <EOL> groups = db . storage [ "<STR_LIT>" ] . get ( None , { } ) <EOL> if not groups : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> for full_name , abbrev , utc_offset in cursor . fetchall ( ) : <EOL> group , name = full_name . split ( "<STR_LIT:/>" ) <EOL> if isinstance ( utc_offset , int ) : <EOL> utc_offset = datetime . timedelta ( utc_offset ) <EOL> groups . setdefault ( group , { } ) [ name ] = ( abbrev , utc_offset ) <EOL> db . storage [ "<STR_LIT>" ] [ None ] = groups <EOL> return groups <EOL> def sortedTimezones ( db ) : <EOL> groups = __fetchTimezones ( db ) <EOL> result = [ ] <EOL> for key in sorted ( groups . keys ( ) ) : <EOL> result . append ( ( key , sorted ( [ ( name , abbrev , utc_offset ) for name , ( abbrev , utc_offset ) in groups [ key ] . items ( ) ] ) ) ) <EOL> return result <EOL> def __fetchUTCOffset ( db , timezone ) : <EOL> utc_offset = db . storage [ "<STR_LIT>" ] . get ( timezone ) <EOL> if utc_offset is None : <EOL> groups = db . storage [ "<STR_LIT>" ] . get ( None ) <EOL> if groups : <EOL> group , name = timezone . split ( "<STR_LIT:/>" ) <EOL> utc_offset = groups [ group ] [ name ] [ <NUM_LIT:2> ] <EOL> else : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( timezone , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> utc_offset = row [ <NUM_LIT:0> ] <EOL> else : <EOL> return <NUM_LIT:0> <EOL> db . storage [ "<STR_LIT>" ] [ timezone ] = utc_offset <EOL> return utc_offset <EOL> def adjustTimestamp ( db , timestamp , timezone ) : <EOL> return timestamp + __fetchUTCOffset ( db , timezone ) <EOL> def formatTimestamp ( db , timestamp , timezone ) : <EOL> utc_offset = __fetchUTCOffset ( db , timezone ) <EOL> seconds = utc_offset . total_seconds ( ) <EOL> offset = "<STR_LIT>" % ( "<STR_LIT:->" if seconds < <NUM_LIT:0> else "<STR_LIT:+>" , seconds / <NUM_LIT> , ( seconds % <NUM_LIT> ) / <NUM_LIT> ) <EOL> return time . strftime ( "<STR_LIT>" , ( timestamp + utc_offset ) . timetuple ( ) ) + offset <EOL> def validTimezone ( db , timezone ) : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( timezone , ) ) <EOL> return bool ( cursor . fetchone ( ) ) </s>
