|
10 | 10 | print tz_utils.get_timezone('GMT +10:00') |
11 | 11 |
|
12 | 12 |
|
13 | | -Example usage (guess timezone by IP, required geoip2!):: |
14 | | -
|
15 | | - tz_utils.GEOIP_DATA_LOCATION = '/usr/local/geo_ip/GeoIP2-City.mmdb' |
16 | | - assert tz_utils.guess_timezone_by_ip('201.246.115.62') == 'Chile/Continental' |
17 | | -
|
18 | | -
|
19 | 13 | Example usage (format timezone by name):: |
20 | 14 |
|
21 | 15 | print tz_utils.format_tz_by_name('Europe/Copenhagen') |
|
33 | 27 | :copyright: 2012 by Amir Salihefendic ( http://amix.dk/ ) |
34 | 28 | :license: MIT |
35 | 29 | """ |
| 30 | + |
36 | 31 | from __future__ import annotations |
37 | 32 |
|
38 | 33 | from datetime import datetime, timedelta, tzinfo |
|
41 | 36 |
|
42 | 37 | from . import _defs |
43 | 38 |
|
44 | | -try: |
45 | | - import geoip2.database as geoip2_db |
46 | | - |
47 | | - HAS_GEOIP2 = True |
48 | | -except ImportError: |
49 | | - HAS_GEOIP2 = False |
50 | | - |
51 | 39 | # --- Exports ---------------------------------------------- |
52 | 40 | __all__ = [ |
53 | 41 | "get_timezone", |
54 | 42 | "is_valid_timezone", |
55 | | - "GEOIP_DATA_LOCATION", |
56 | | - "guess_timezone_by_ip", |
57 | 43 | "format_tz_by_name", |
58 | 44 | ] |
59 | 45 |
|
60 | | -# --- Specifies the location of GeoIP GeoLiteCity.dat database --- |
61 | | -GEOIP_DATA_LOCATION = None |
62 | | - |
63 | | - |
64 | | -# --- Functions ---------------------------------------------- |
65 | | -def guess_timezone_by_ip(ip: str, only_name: bool = False): |
66 | | - """Given an `ip` with guess timezone using geoip2. |
67 | | - Returns a tuple of (tz_offets, tz_name, tz_formatted). |
68 | | - `None` is returned if it can't guess a timezone. |
69 | | -
|
70 | | - For this to work you need to set tz_utils.GEOIP_DATA_LOCATION |
71 | | - You can get this database from http://www.maxmind.com/app/geolitecity |
72 | | -
|
73 | | - Example usage:: |
74 | | -
|
75 | | - from timezones import tz_utils |
76 | | -
|
77 | | - tz_utils.GEOIP_DATA_LOCATION = "/usr/local/geo_ip/GeoLiteCity.dat" |
78 | | - assert tz_utils.guess_timezone_by_ip("201.246.115.62") == "Chile/Continental" |
79 | | -
|
80 | | - """ |
81 | | - geo_lib = _get_geoip_lib() |
82 | | - if geo_lib: |
83 | | - try: |
84 | | - record = geo_lib.city(ip) |
85 | | - if record: |
86 | | - location = record.location |
87 | | - if location and location.time_zone: |
88 | | - if only_name: |
89 | | - return location.time_zone |
90 | | - else: |
91 | | - return format_tz_by_name(location.time_zone) |
92 | | - except Exception: |
93 | | - record = None |
94 | | - return None |
95 | | - |
96 | 46 |
|
97 | 47 | def get_timezone(tzname: str) -> tzinfo | None: |
98 | 48 | """ |
@@ -154,23 +104,6 @@ def get_last_datetime_without_dst(tz: tzinfo): |
154 | 104 |
|
155 | 105 |
|
156 | 106 | # --- Private ---------------------------------------------- |
157 | | -GEO_IP = None |
158 | | - |
159 | | - |
160 | | -def _get_geoip_lib(): |
161 | | - global GEO_IP |
162 | | - |
163 | | - if not HAS_GEOIP2 or not GEOIP_DATA_LOCATION: |
164 | | - return None |
165 | | - |
166 | | - try: |
167 | | - GEO_IP = geoip2_db.Reader(GEOIP_DATA_LOCATION) |
168 | | - except Exception: |
169 | | - return None |
170 | | - |
171 | | - return GEO_IP |
172 | | - |
173 | | - |
174 | 107 | _zero = timedelta(0) |
175 | 108 |
|
176 | 109 |
|
|
0 commit comments