Python datetime.replace() Examples

The following are code examples for showing how to use datetime.replace(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: django-forcedfields   Author: monotonee   File: test_timestamp_field.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 2
Project: django-forcedfields   Author: monotonee   File: test_timestamp_field.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 3
Project: tissuelab   Author: VirtualPlants   File: compiler.py    (license) View Source Project 6 votes vote down vote up
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 
Example 4
Project: pypond   Author: esnet   File: util_test.py    (license) View Source Project 6 votes vote down vote up
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) 
Example 5
Project: webapp2   Author: GoogleCloudPlatform   File: i18n.py    (license) View Source Project 5 votes vote down vote up
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)) 
Example 6
Project: webapp2   Author: GoogleCloudPlatform   File: i18n.py    (license) View Source Project 5 votes vote down vote up
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) 
Example 7
Project: eventroller   Author: MoveOnOrg   File: facebook.py    (license) View Source Project 5 votes vote down vote up
def strip_tz(datetime):
    """Strip the timezone for USE_TZ=False"""
    return datetime.replace(tzinfo=None) 
Example 8
Project: blockhooks   Author: EthereumWebhooks   File: i18n.py    (license) View Source Project 5 votes vote down vote up
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)) 
Example 9
Project: blockhooks   Author: EthereumWebhooks   File: i18n.py    (license) View Source Project 5 votes vote down vote up
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)