File: zoneinfo.py

package info (click to toggle)
python-django-timezone-field 7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: python: 1,047; sh: 6; makefile: 3
file content (27 lines) | stat: -rw-r--r-- 887 bytes parent folder | download
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
26
27
try:
    import zoneinfo
except ImportError:
    from backports import zoneinfo

from .base import TimeZoneBackend, TimeZoneNotFoundError


class ZoneInfoBackend(TimeZoneBackend):
    utc_tzobj = zoneinfo.ZoneInfo("UTC")
    all_tzstrs = zoneinfo.available_timezones()
    base_tzstrs = zoneinfo.available_timezones()
    # Remove the "Factory" timezone as it can cause ValueError exceptions on
    # some systems, e.g. FreeBSD, if the system zoneinfo database is used.
    all_tzstrs.discard("Factory")
    base_tzstrs.discard("Factory")

    def is_tzobj(self, value):
        return isinstance(value, zoneinfo.ZoneInfo)

    def to_tzobj(self, tzstr):
        if tzstr in (None, ""):
            raise TimeZoneNotFoundError
        try:
            return zoneinfo.ZoneInfo(tzstr)
        except zoneinfo.ZoneInfoNotFoundError as err:
            raise TimeZoneNotFoundError from err