我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用datetime.replace()。
def _assert_datetime_equal(self, datetime_1, datetime_2): """ Assert two datetime.datetime values are equal. This method does NOT compare any division of time smaller than a minute, i.e. seconds and milliseconds, if present, are not considered in equality. This could also be implemented with datetime.replace() to zero out seconds and microseconds. Args: datetime_1 (datetime.datetime): The first (left) value to compare. datetime_2 (datetime.datetime): The second (right) value to compare. """ self.assertEqual(datetime_1.date(), datetime_2.date()) self.assertEqual(datetime_1.hour, datetime_2.hour) self.assertEqual(datetime_1.minute, datetime_2.minute)
def _assert_datetime_not_equal(self, datetime_1, datetime_2): """ Assert two datetime.datetime values are NOT equal. This method does NOT compare any division of time smaller than a minute, i.e. seconds and milliseconds, if present, are not considered in the equality test. This could also be implemented with datetime.replace() to zero out seconds and microseconds. Args: datetime_1 (datetime.datetime): The first (left) value to compare. datetime_2 (datetime.datetime): The second (right) value to compare. """ required_datetime_attributes = ['date', 'hour', 'minute'] datetime_1_has_interface = set(dir(datetime_1)).issuperset(required_datetime_attributes) datetime_2_has_interface = set(dir(datetime_2)).issuperset(required_datetime_attributes) if datetime_1_has_interface and datetime_2_has_interface: self.assertNotEqual(datetime_1.date(), datetime_2.date()) self.assertNotEqual(datetime_1.hour, datetime_2.hour) self.assertNotEqual(datetime_1.minute, datetime_2.minute) else: self.assertNotEqual(datetime_1, datetime_2)
def results_iter(self): if self.connection.ops.oracle: from django.db.models.fields import DateTimeField fields = [DateTimeField()] else: needs_string_cast = self.connection.features.needs_datetime_string_cast offset = len(self.query.extra_select) for rows in self.execute_sql(MULTI): for row in rows: datetime = row[offset] if self.connection.ops.oracle: datetime = self.resolve_columns(row, fields)[offset] elif needs_string_cast: datetime = typecast_timestamp(str(datetime)) # Datetimes are artifically returned in UTC on databases that # don't support time zone. Restore the zone used in the query. if settings.USE_TZ: datetime = datetime.replace(tzinfo=None) datetime = timezone.make_aware(datetime, self.query.tzinfo) yield datetime
def test_round_trip(self): """Test ms -> dt -> ms and dt -> ms -> dt""" # ms -> dt -> ms to_dt = dt_from_ms(self.ms_reference) from_dt = ms_from_dt(to_dt) self.assertEqual(from_dt, self.ms_reference) # dt -> ms -> dt to test rounding in aware_utcnow() now = aware_utcnow() to_ms = ms_from_dt(now) back_to_dt = dt_from_ms(to_ms) self.assertEqual(now, back_to_dt) # dt from unixtime -> ms -> dt utc = datetime.datetime.utcfromtimestamp(1459442035).replace(tzinfo=pytz.UTC) utcms = ms_from_dt(utc) back_to_utc = dt_from_ms(utcms) self.assertEqual(utc, back_to_utc)
def to_local_timezone(self, datetime): """Returns a datetime object converted to the local timezone. :param datetime: A ``datetime`` object. :returns: A ``datetime`` object normalized to a timezone. """ if datetime.tzinfo is None: datetime = datetime.replace(tzinfo=pytz.UTC) return self.tzinfo.normalize(datetime.astimezone(self.tzinfo))
def to_utc(self, datetime): """Returns a datetime object converted to UTC and without tzinfo. :param datetime: A ``datetime`` object. :returns: A naive ``datetime`` object (no timezone), converted to UTC. """ if datetime.tzinfo is None: datetime = self.tzinfo.localize(datetime) return datetime.astimezone(pytz.UTC).replace(tzinfo=None)
def strip_tz(datetime): """Strip the timezone for USE_TZ=False""" return datetime.replace(tzinfo=None)