def formatdate(datetime, localtime=False, usegmt=False): """ This is the reverse. It will take a datetime object and format and do the deconversions necessary to pass it to email.utils.formatdate() and thus return a string properly formatted as an RFC822 date. """ return email.utils.formatdate(calendar.timegm(datetime.utctimetuple()), localtime=localtime, usegmt=usegmt)
def datetime_to_struct(datetime): """Convert a datetime object to an UTC-based 9-tuple. The reverse of ``struct_to_datetime``. """ if not datetime: return datetime return datetime.utctimetuple()
def from_datetime(datetime): """ Takes a UTC timestamp (a datetime object) and converts it into a 9-tuple to be used by feedparser. """ if datetime==None: return None return datetime.utctimetuple()
def seconds_from_datetime(dt): """Convert datetime into seconds since epochs in UTC timezone. IE, to use for instance.launch_time: toseconds(instance.launch_time). to invert (assuming UTC timestamps): import pytz utc = pytz.UTC utc.localize(datetime.fromtimestamp(seconds)) """ return time.mktime(dt.utctimetuple())
def testDateTime(self): iso_string = "2013-06-13T21:33:44+09:00" datetime = dateutil.parser.parse(iso_string) time_tuple = datetime.utctimetuple() self.assertEqual(time_tuple.tm_year, 2013) self.assertEqual(time_tuple.tm_mon, 6) self.assertEqual(time_tuple.tm_mday, 13) self.assertEqual(time_tuple.tm_hour, 12) self.assertEqual(time_tuple.tm_min, 33) self.assertEqual(time_tuple.tm_sec, 44) native = isoToNative(iso_string) self.assertIsNone(native.tzinfo) time_tuple = native.utctimetuple() self.assertEqual(time_tuple.tm_year, 2013) self.assertEqual(time_tuple.tm_mon, 6) self.assertEqual(time_tuple.tm_mday, 13) self.assertEqual(time_tuple.tm_hour, 12) self.assertEqual(time_tuple.tm_min, 33) self.assertEqual(time_tuple.tm_sec, 44)
def from_datetime(cls, datetime): return calendar.timegm(datetime.utctimetuple())
def toseconds(dt): """Converts datetime object to seconds.""" return time.mktime(dt.utctimetuple())
def datetime_to_utc(dt): """Converts a datetime object to UTC timestamp naive datetime will be considered UTC.""" return calendar.timegm(dt.utctimetuple())
def to_timestamp(datetime): # Converts a datetime object to unix UTC time return calendar.timegm(datetime.utctimetuple())
def datetime_to_timestamp(datetime): import calendar if datetime: return calendar.timegm(datetime.utctimetuple()) else: return None
def timestamp(self, datetime=datetime.datetime.utcnow()): ''' return unix timestamp from datetime ''' return int(time.mktime(datetime.utctimetuple())*1000)
def timestamp(datetime): return int ( time.mktime( datetime.utctimetuple() ) )
def datetimeToTimestamp(datetime): """Convert a UTC datetime object to a UTC timestamp""" return mktime_utc(datetime.utctimetuple())