Python django.utils.timezone.make_naive() Examples
The following are 30 code examples for showing how to use django.utils.timezone.make_naive(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
django.utils.timezone
, or try the search function
.
Example 1
Project: bioforum Author: reBiocoder File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # SQLite doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.") return str(value)
Example 2
Project: bioforum Author: reBiocoder File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): """ Transform a datetime value to an object compatible with what is expected by the backend driver for datetime columns. If naive datetime is passed assumes that is in UTC. Normally Django models.DateTimeField makes sure that if USE_TZ is True passed datetime is timezone aware. """ if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # cx_Oracle doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") return Oracle_datetime.from_datetime(value)
Example 3
Project: bioforum Author: reBiocoder File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # MySQL doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.") if not self.connection.features.supports_microsecond_precision: value = value.replace(microsecond=0) return str(value)
Example 4
Project: django-trackstats Author: pennersr File: trackers.py License: MIT License | 6 votes |
def get_start_date(self, qs): most_recent_kwargs = self.get_most_recent_kwargs() last_stat = self.statistic_model.objects.most_recent( **most_recent_kwargs) if last_stat: start_date = last_stat.date else: first_instance = qs.order_by(self.date_field).first() if first_instance is None: # No data return start_date = getattr(first_instance, self.date_field) if start_date and isinstance(start_date, datetime): if timezone.is_aware(start_date): start_date = timezone.make_naive(start_date).date() else: start_date = start_date.date() return start_date
Example 5
Project: Hands-On-Application-Development-with-PyCharm Author: PacktPublishing File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # SQLite doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.") return str(value)
Example 6
Project: Hands-On-Application-Development-with-PyCharm Author: PacktPublishing File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): """ Transform a datetime value to an object compatible with what is expected by the backend driver for datetime columns. If naive datetime is passed assumes that is in UTC. Normally Django models.DateTimeField makes sure that if USE_TZ is True passed datetime is timezone aware. """ if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # cx_Oracle doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") return Oracle_datetime.from_datetime(value)
Example 7
Project: zentral Author: zentralopensource File: mt_models.py License: Apache License 2.0 | 6 votes |
def add_field(self, k, v): if not isinstance(k, str) or not k: raise ValueError("Invalid field name {}".format(k)) if k in self.fields: raise ValueError("Field {} already added".format(k)) if self.is_empty_value(v): return elif isinstance(v, int): v = str(v) elif isinstance(v, datetime): if is_aware(v): v = make_naive(v) v = v.isoformat() elif isinstance(v, list): assert(all([isinstance(e, str) and len(e) == 40 for e in v])) elif not isinstance(v, str): raise ValueError("Invalid field value {} for field {}".format(v, k)) self.fields[k] = v
Example 8
Project: python Author: Yeah-Kun File: operations.py License: Apache License 2.0 | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # SQLite doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.") return six.text_type(value)
Example 9
Project: python Author: Yeah-Kun File: operations.py License: Apache License 2.0 | 6 votes |
def adapt_datetimefield_value(self, value): """ Transform a datetime value to an object compatible with what is expected by the backend driver for datetime columns. If naive datetime is passed assumes that is in UTC. Normally Django models.DateTimeField makes sure that if USE_TZ is True passed datetime is timezone aware. """ if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # cx_Oracle doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") return Oracle_datetime.from_datetime(value)
Example 10
Project: python Author: Yeah-Kun File: operations.py License: Apache License 2.0 | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # MySQL doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.") if not self.connection.features.supports_microsecond_precision: value = value.replace(microsecond=0) return six.text_type(value)
Example 11
Project: GetTogether Author: GetTogetherComm File: 0022_localize_datetimes.py License: BSD 2-Clause "Simplified" License | 6 votes |
def localize_event_datetimes(apps, schema_editor): Event = MyModel = apps.get_model("events", "Event") for event in Event.objects.all(): utc_tz = pytz.timezone("UTC") event_tz = get_event_timezone(event) print("Converting event %s to %s" % (event.id, event_tz)) event.start_time = utc_tz.localize( timezone.make_naive( event_tz.localize(timezone.make_naive(event.start_time, pytz.utc)) ) ) event.end_time = utc_tz.localize( timezone.make_naive( event_tz.localize(timezone.make_naive(event.end_time, pytz.utc)) ) ) event.save()
Example 12
Project: GetTogether Author: GetTogetherComm File: 0022_localize_datetimes.py License: BSD 2-Clause "Simplified" License | 6 votes |
def localize_commonevent_datetimes(apps, schema_editor): CommonEvent = MyModel = apps.get_model("events", "CommonEvent") for event in CommonEvent.objects.all(): utc_tz = pytz.timezone("UTC") event_tz = get_event_timezone(event) print("Converting common event %s to %s" % (event.id, event_tz)) event.start_time = utc_tz.localize( timezone.make_naive( event_tz.localize(timezone.make_naive(event.start_time, pytz.utc)) ) ) event.end_time = utc_tz.localize( timezone.make_naive( event_tz.localize(timezone.make_naive(event.end_time, pytz.utc)) ) ) event.save()
Example 13
Project: luscan-devel Author: blackye File: __init__.py License: GNU General Public License v2.0 | 6 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): if settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value try: parsed = parse_date(value) if parsed is not None: return parsed except ValueError: msg = self.error_messages['invalid_date'] % value raise exceptions.ValidationError(msg) msg = self.error_messages['invalid'] % value raise exceptions.ValidationError(msg)
Example 14
Project: openhgsenti Author: drexly File: operations.py License: Apache License 2.0 | 6 votes |
def adapt_datetimefield_value(self, value): """ Transform a datetime value to an object compatible with what is expected by the backend driver for datetime columns. If naive datetime is passed assumes that is in UTC. Normally Django models.DateTimeField makes sure that if USE_TZ is True passed datetime is timezone aware. """ if value is None: return None # cx_Oracle doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") return Oracle_datetime.from_datetime(value)
Example 15
Project: python2017 Author: bpgc-cte File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # SQLite doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("SQLite backend does not support timezone-aware datetimes when USE_TZ is False.") return six.text_type(value)
Example 16
Project: python2017 Author: bpgc-cte File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): """ Transform a datetime value to an object compatible with what is expected by the backend driver for datetime columns. If naive datetime is passed assumes that is in UTC. Normally Django models.DateTimeField makes sure that if USE_TZ is True passed datetime is timezone aware. """ if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # cx_Oracle doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("Oracle backend does not support timezone-aware datetimes when USE_TZ is False.") return Oracle_datetime.from_datetime(value)
Example 17
Project: python2017 Author: bpgc-cte File: operations.py License: MIT License | 6 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # MySQL doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.") if not self.connection.features.supports_microsecond_precision: value = value.replace(microsecond=0) return six.text_type(value)
Example 18
Project: django-storages Author: jschneier File: test_gcloud.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_get_modified_time(self): naive_date = datetime(2017, 1, 2, 3, 4, 5, 678) aware_date = timezone.make_aware(naive_date, timezone.utc) self.storage._bucket = mock.MagicMock() blob = mock.MagicMock() blob.updated = aware_date self.storage._bucket.get_blob.return_value = blob with self.settings(TIME_ZONE='America/Montreal', USE_TZ=False): mt = self.storage.get_modified_time(self.filename) self.assertTrue(timezone.is_naive(mt)) naive_date_montreal = timezone.make_naive(aware_date) self.assertEqual(mt, naive_date_montreal) self.storage._bucket.get_blob.assert_called_with(self.filename) with self.settings(TIME_ZONE='America/Montreal', USE_TZ=True): mt = self.storage.get_modified_time(self.filename) self.assertTrue(timezone.is_aware(mt)) self.assertEqual(mt, aware_date) self.storage._bucket.get_blob.assert_called_with(self.filename)
Example 19
Project: django-storages Author: jschneier File: test_gcloud.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_get_created_time(self): naive_date = datetime(2017, 1, 2, 3, 4, 5, 678) aware_date = timezone.make_aware(naive_date, timezone.utc) self.storage._bucket = mock.MagicMock() blob = mock.MagicMock() blob.time_created = aware_date self.storage._bucket.get_blob.return_value = blob with self.settings(TIME_ZONE='America/Montreal', USE_TZ=False): mt = self.storage.get_created_time(self.filename) self.assertTrue(timezone.is_naive(mt)) naive_date_montreal = timezone.make_naive(aware_date) self.assertEqual(mt, naive_date_montreal) self.storage._bucket.get_blob.assert_called_with(self.filename) with self.settings(TIME_ZONE='America/Montreal', USE_TZ=True): mt = self.storage.get_created_time(self.filename) self.assertTrue(timezone.is_aware(mt)) self.assertEqual(mt, aware_date) self.storage._bucket.get_blob.assert_called_with(self.filename)
Example 20
Project: GTDWeb Author: lanbing510 File: utils.py License: GNU General Public License v2.0 | 5 votes |
def to_current_timezone(value): """ When time zone support is enabled, convert aware datetimes to naive dateimes in the current time zone for display. """ if settings.USE_TZ and value is not None and timezone.is_aware(value): current_timezone = timezone.get_current_timezone() return timezone.make_naive(value, current_timezone) return value
Example 21
Project: GTDWeb Author: lanbing510 File: __init__.py License: GNU General Public License v2.0 | 5 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): if settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value try: parsed = parse_date(value) if parsed is not None: return parsed except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_date'], code='invalid_date', params={'value': value}, ) raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
Example 22
Project: bioforum Author: reBiocoder File: utils.py License: MIT License | 5 votes |
def to_current_timezone(value): """ When time zone support is enabled, convert aware datetimes to naive datetimes in the current time zone for display. """ if settings.USE_TZ and value is not None and timezone.is_aware(value): current_timezone = timezone.get_current_timezone() return timezone.make_naive(value, current_timezone) return value
Example 23
Project: bioforum Author: reBiocoder File: __init__.py License: MIT License | 5 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): if settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value try: parsed = parse_date(value) if parsed is not None: return parsed except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_date'], code='invalid_date', params={'value': value}, ) raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )
Example 24
Project: django-trackstats Author: pennersr File: test_trackers.py License: MIT License | 5 votes |
def to_date(dt): return timezone.make_naive(dt).date()
Example 25
Project: stream-django Author: GetStream File: activity.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def activity_time(self): atime = self.created_at if is_aware(self.created_at): atime = make_naive(atime, pytz.utc) return atime
Example 26
Project: django-easy-audit Author: soynatan File: utils.py License: GNU General Public License v3.0 | 5 votes |
def get_field_value(obj, field): """ Gets the value of a given model instance field. :param obj: The model instance. :type obj: Model :param field: The field you want to find the value of. :type field: Any :return: The value of the field as a string. :rtype: str """ if isinstance(field, DateTimeField): # DateTimeFields are timezone-aware, so we need to convert the field # to its naive form before we can accuratly compare them for changes. try: value = field.to_python(getattr(obj, field.name, None)) if value is not None and settings.USE_TZ and not timezone.is_naive(value): value = timezone.make_naive(value, timezone=timezone.utc) except ObjectDoesNotExist: value = field.default if field.default is not NOT_PROVIDED else None else: try: value = smart_text(getattr(obj, field.name, None)) except ObjectDoesNotExist: value = field.default if field.default is not NOT_PROVIDED else None return value
Example 27
Project: zing Author: evernote File: timezone.py License: GNU General Public License v3.0 | 5 votes |
def make_naive(value, tz=None): """Makes a `datetime` naive, i.e. not aware of timezones. :param value: `datetime` object to make timezone-aware. :param tz: `tzinfo` object with the timezone information the given value needs to be converted to. By default, site's own default timezone will be used. """ if getattr(settings, "USE_TZ", False) and timezone.is_aware(value): use_tz = tz if tz is not None else timezone.get_default_timezone() value = timezone.make_naive(value, timezone=use_tz) return value
Example 28
Project: Hands-On-Application-Development-with-PyCharm Author: PacktPublishing File: utils.py License: MIT License | 5 votes |
def to_current_timezone(value): """ When time zone support is enabled, convert aware datetimes to naive datetimes in the current time zone for display. """ if settings.USE_TZ and value is not None and timezone.is_aware(value): return timezone.make_naive(value) return value
Example 29
Project: Hands-On-Application-Development-with-PyCharm Author: PacktPublishing File: operations.py License: MIT License | 5 votes |
def adapt_datetimefield_value(self, value): if value is None: return None # Expression values are adapted by the database. if hasattr(value, 'resolve_expression'): return value # MySQL doesn't support tz-aware datetimes if timezone.is_aware(value): if settings.USE_TZ: value = timezone.make_naive(value, self.connection.timezone) else: raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.") return str(value)
Example 30
Project: Hands-On-Application-Development-with-PyCharm Author: PacktPublishing File: __init__.py License: MIT License | 5 votes |
def to_python(self, value): if value is None: return value if isinstance(value, datetime.datetime): if settings.USE_TZ and timezone.is_aware(value): # Convert aware datetimes to the default time zone # before casting them to dates (#17742). default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() if isinstance(value, datetime.date): return value try: parsed = parse_date(value) if parsed is not None: return parsed except ValueError: raise exceptions.ValidationError( self.error_messages['invalid_date'], code='invalid_date', params={'value': value}, ) raise exceptions.ValidationError( self.error_messages['invalid'], code='invalid', params={'value': value}, )