File: timezone.py

package info (click to toggle)
djangorestframework 3.16.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,092 kB
  • sloc: javascript: 31,965; python: 29,367; makefile: 32; sh: 6
file content (25 lines) | stat: -rw-r--r-- 1,086 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from datetime import datetime, timezone, tzinfo


def datetime_exists(dt):
    """Check if a datetime exists. Taken from: https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html"""
    # There are no non-existent times in UTC, and comparisons between
    # aware time zones always compare absolute times; if a datetime is
    # not equal to the same datetime represented in UTC, it is imaginary.
    return dt.astimezone(timezone.utc) == dt


def datetime_ambiguous(dt: datetime):
    """Check whether a datetime is ambiguous. Taken from: https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html"""
    # If a datetime exists and its UTC offset changes in response to
    # changing `fold`, it is ambiguous in the zone specified.
    return datetime_exists(dt) and (
        dt.replace(fold=not dt.fold).utcoffset() != dt.utcoffset()
    )


def valid_datetime(dt):
    """Returns True if the datetime is not ambiguous or imaginary, False otherwise."""
    if isinstance(dt.tzinfo, tzinfo) and not datetime_ambiguous(dt):
        return True
    return False