def main(): d_now = datetime.datetime.now() d_utcnow = datetime.datetime.utcnow() d_now_tz = d_now.replace(tzinfo=pytz.utc) d_utcnow_tz = d_utcnow.replace(tzinfo=pytz.utc) dates = ( ("d_now", d_now), ("d_now_tz", d_now_tz), ("d_utcnow", d_utcnow), ("d_utcnow_tz", d_utcnow_tz), ) print "{0:>30}: {1}".format("TZ", os.environ.get('TZ', '')) print "{0:>30}: {1}".format("time.time()", time.time()) print "" for desc, date_var in dates: timestamp = "{0}.{1:06}".format(calendar.timegm(date_var.timetuple()), date_var.microsecond) from_timestamp = time.gmtime(float(timestamp)) print "{0:>30}".format(desc) print "{0:>30}: {1}".format("type()", type(date_var)) print "{0:>30}: {1}".format("str()", str(date_var)) print "{0:>30}: {1}".format("utctimetuple()", date_var.utctimetuple()) print "{0:>30}: {1}".format("calendar.timegm()", calendar.timegm(date_var.timetuple())) print "{0:>30}: {1}".format("microsecond", date_var.microsecond) print "{0:>30}: {1}".format("timestamp", timestamp) print "{0:>30}: {1}".format("from_timestamp", from_timestamp) print "{0:>30}: {1}".format("type(from_timestamp)", type(from_timestamp)) print "{0:>30}: {1}".format( "datetime.utcfromtimestamp()", datetime.datetime.utcfromtimestamp(float(timestamp))) print "" current_utc_str_timestamp = utc_str_timestamp() print "utc_str_timestamp():", current_utc_str_timestamp print " + de-serialized:", utc_timestamp2datetime( current_utc_str_timestamp)
def main(): d_now = datetime.datetime.now() d_utcnow = datetime.datetime.utcnow() d_now_tz = d_now.replace(tzinfo=pytz.utc) d_utcnow_tz = d_utcnow.replace(tzinfo=pytz.utc) dates = ( ("d_now", d_now), ("d_now_tz", d_now_tz), ("d_utcnow", d_utcnow), ("d_utcnow_tz", d_utcnow_tz), ) print "{0:>30}: {1}".format("TZ", os.environ.get('TZ', '')) print "{0:>30}: {1}".format("time.time()", time.time()) print "" for desc, date_var in dates: timestamp = "{0}.{1:06}".format(calendar.timegm(date_var.timetuple()), date_var.microsecond) from_timestamp = time.gmtime(float(timestamp)) print "{0:>30}".format(desc) print "{0:>30}: {1}".format("type()", type(date_var)) print "{0:>30}: {1}".format("str()", str(date_var)) print "{0:>30}: {1}".format("utctimetuple()", date_var.utctimetuple()) print "{0:>30}: {1}".format("calendar.timegm()", calendar.timegm(date_var.timetuple())) print "{0:>30}: {1}".format("microsecond", date_var.microsecond) print "{0:>30}: {1}".format("timestamp", timestamp) print "{0:>30}: {1}".format("from_timestamp", from_timestamp) print "{0:>30}: {1}".format("type(from_timestamp)", type(from_timestamp)) print "{0:>30}: {1}".format("datetime.utcfromtimestamp()", datetime.datetime.utcfromtimestamp(float(timestamp))) print "" current_utc_str_timestamp = utc_str_timestamp() print "utc_str_timestamp():", current_utc_str_timestamp print " + de-serialized:", utc_timestamp2datetime(current_utc_str_timestamp)
def current_timestamp_generator(): while True: yield utc_str_timestamp()