def test_compare_old_instances(self): # Compare dates that don't have the _micros attribute yet # (e.g., from old pickles). dt = DateTime('1997/1/1') dt1 = DateTime('1997/2/2') dt._millis = dt._micros / 1000 del dt._micros dt1._millis = dt1._micros / 1000 del dt1._micros self.testCompareOperations(dt, dt1)
def test_compare_new_old_instances(self): # Compare a date with _micros attribute with one that does not # (e.g., from an old pickle). dt = DateTime('1997/1/1') dt1 = DateTime('1997/2/2') dt1._millis = dt._micros / 1000 del dt1._micros self.testCompareOperations(dt, dt1)
def test___setstate___without_micros(self): ISO = '2001-10-10T00:00:00+02:00' dt = DateTime(ISO) micros = dt._micros dt._millis = dt._micros / 1000 del dt._micros state = dt.__dict__ dt1 = DateTime() dt1.__setstate__(state) self.assertEqual(dt1._micros, micros)
def test_strftime_old_instance(self): # https://bugs.launchpad.net/zope2/+bug/290254 # Ensure that dates without _micros attribute (e.g., from old # pickles) still render correctly in strftime. ISO = '2001-10-10T00:00:00+02:00' dt = DateTime(ISO) dt._millis = dt._micros / 1000 del dt._micros self.assertEqual(dt.strftime('%Y'), '2001') # Now, create one via pickling / unpickling. from cPickle import dumps, loads self.assertEqual(loads(dumps(dt)).strftime('%Y'), '2001')
def createDateTimeFromMillis(millis): """ Returns a DateTime object, build from the number of milliseconds since epoch. Parameter should be a int or long. This one should be used by solvers, as DateTime.__cmp__ actually only compares the _millis parameter of the two DateTime objects. This is currently not perfect: DateTime only supports creating a new object from a floating point number of seconds since epoch, so a rounding issue is still possible, that's why _millis is explicitely set to the same value after the DateTime object has been created from (millis / 1000.) A better way would be to compute (yr,mo,dy,hr,mn,sc,tz,t,d,s,millisecs) from millis, and then create the DateTime object from it (see "elif ac == 11:" in DateTime._parse_args). Another solution would be a DateTime implementation that relies exclusively on integer values internally. """ millis = long(millis) date = DateTime(millis / 1000.0) date._millis = millis return date
def createDateTimeFromMillis(millis): # pylint: disable=redefined-outer-name """ Returns a DateTime object, build from the number of milliseconds since epoch. Parameter should be a int or long. This one should be used by solvers, as DateTime.__cmp__ actually only compares the _millis parameter of the two DateTime objects. This is currently not perfect: DateTime only supports creating a new object from a floating point number of seconds since epoch, so a rounding issue is still possible, that's why _millis is explicitely set to the same value after the DateTime object has been created from (millis / 1000.) A better way would be to compute (yr,mo,dy,hr,mn,sc,tz,t,d,s,millisecs) from millis, and then create the DateTime object from it (see "elif ac == 11:" in DateTime._parse_args). Another solution would be a DateTime implementation that relies exclusively on integer values internally. """ millis = long(millis) date = DateTime(millis / 1000.) date._millis = millis return date