def _tz_aware_timestamp_adapter(val): '''from https://gist.github.com/acdha/6655391''' datepart, timepart = val.split(b" ") year, month, day = map(int, datepart.split(b"-")) if b"+" in timepart: timepart, tz_offset = timepart.rsplit(b"+", 1) if tz_offset == b'00:00': tzinfo = pytz.utc else: hours, minutes = map(int, tz_offset.split(b':', 1)) tzinfo = pytz.utc(datetime.timedelta(hours=hours, minutes=minutes)) else: tzinfo = None timepart_full = timepart.split(b".") hours, minutes, seconds = map(int, timepart_full[0].split(b":")) if len(timepart_full) == 2: microseconds = int('{:0<6.6}'.format(timepart_full[1].decode())) else: microseconds = 0 val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds, tzinfo) return val
def main(): dbpath = "./impc.sqlite" prepare = not os.path.exists(dbpath) try: utc = pytz.utc() except: utc = pytz.utc conn = sqlite3.connect(dbpath) conn.row_factory = sqlite3.Row cursor = conn.cursor() if prepare: for sql in DBSCHEMA: cursor.execute(sql) rollbackvalues = {} servers = cursor.execute("select * from server").fetchall() if not servers: sys.exit() for server in servers: print(dict(server)) rollbackvalues[server['id']] = 0 #daemonize() while True: now = datetime.now(utc) for server in cursor.execute( "select * from server where active=1").fetchall(): try: listeners = methods[server["type"]](dict(server)) except: listeners = None tz = pytz.timezone(server["timezone"]) localnow = now.astimezone(tz) if listeners is not None: cursor.execute( "insert into entry (time, local_time, server, listeners) values (?, ?, ?, ?)", (now, localnow, server['id'], listeners)) rollbackvalues[server['id']] = listeners else: cursor.execute( "insert into entry (time, local_time, server, listeners) values (?, ?, ?, ?)", (now, localnow, server['id'], rollbackvalues[server['id']] / 2)) rollbackvalues[server['id']] = rollbackvalues[server['id']] / 2 conn.commit() print("sleeping") sleep(600)
def test(): dbpath = "./impc.sqlite" prepare = not os.path.exists(dbpath) try: utc = pytz.utc() except: utc = pytz.utc conn = sqlite3.connect(dbpath) conn.row_factory = sqlite3.Row cursor = conn.cursor() if prepare: for sql in DBSCHEMA: cursor.execute(sql) rollbackvalues = {} for server in cursor.execute( "select * from server where active=1").fetchall(): #try: listeners = methods[server["type"]](dict(server))
try: import pytz except ImportError, e: print e pytz = None # This is a Date object for 1/1/2014 print "datetime.date(2014,1,1):", date(2014,1,1) # This is a Time object for 5:30:30.5 PM, no time zone or date set print "datetime.time(17,30,30,50):", time(17,30,30,500) # This is a Datetime object for date and time and the moment it is created print "datetime.datetime.now():", datetime.now() # This is a Datetime object for slightly later. Will not change as 'now' changes. print "datetime.datetime.now():", datetime.now() #These are some formatting examples. print "ctime formatting:", datetime.now().ctime() print "strftime('Hour %H on a %A in %Y'):", datetime.now().strftime('Hour %H on a %A in %Y') # Check if pytz exists first. if pytz: utc = pytz.utc() central = pytz.timezone('US/Central') central = pytz.timezone('US/Eastern') print "Central time zone now()", central.localize(datetime.now()) print "Eastern time zone now()", eastern.localize(datetime.now()) #Display Central time as Eastern time dt_cst = central.localize(datetime.now()) print "Current time in Central time zone:", dt_cst print "Current time in Eastern time zone:", dt_cst.astimezone(eastern)