def _convert_datetime(time): """ Convert time, which may be one of a whole lot of things, into a standard 9 part time tuple. """ if type(time) is datetime.datetime: return time.timetuple() elif (type(time) is tuple and len(time) ==9) or type(time) is struct_time: # Already done! return time elif type(time) is int or type(time) is float: # Assume this is a seconds-since-epoch time return localtime(time) elif type(time) in types.StringTypes: # A time stamp? try: return strptime(time, "%a, %d %b %Y %H:%M:%S %Z") except ValueError: # Maybe this is a string of an epoch time? try: return localtime(float(time)) except ValueError: # Guess not. raise Exception("Unrecongised time format!") else: # No idea what this is. Give up! raise Exception("Unrecongised time format!")
def datetime_to_JSON(time,roundit): #x = datetime.fromtimestamp(mktime(time.timetuple())) unixtime = mktime(time.timetuple())+1e-6*time.microsecond unixtime = (int)(round(unixtime/roundit)*roundit) return unixtime*1000
def datetime_format_to_unixtime(time): return mktime(time.timetuple())