Skip to content

Commit 1bbc4ce

Browse files
updates
1 parent 10832f7 commit 1bbc4ce

2 files changed

Lines changed: 29 additions & 27 deletions

File tree

lumix/timezonebot/convert.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import datetime
2-
import sys
32

3+
_zoneinfo_available = True
44
try:
55
from zoneinfo import ZoneInfo
6-
7-
USE_ZONEINFO = True
86
except ImportError:
97
try:
108
import pytz
119

12-
USE_ZONEINFO = False
10+
_zoneinfo_available = False
1311
except ImportError:
14-
print("Error: pytz not installed. Run: pip install pytz")
15-
sys.exit(1)
12+
_zoneinfo_available = False
13+
ZoneInfo = None
14+
pytz = None
1615

1716
CITY_TIMEZONE = {
1817
"tokyo": "Asia/Tokyo",
@@ -29,11 +28,13 @@ def get_time_in_city(city: str) -> str:
2928
raise ValueError(f"Città '{city}' non trovata nel database")
3029
tz_name = CITY_TIMEZONE[city_lower]
3130
try:
32-
if USE_ZONEINFO:
33-
tz = ZoneInfo(tz_name)
31+
if not _zoneinfo_available:
32+
if pytz is None:
33+
raise RuntimeError("pytz not installed")
34+
tz = pytz.timezone(tz_name)
3435
now = datetime.datetime.now(tz)
3536
else:
36-
tz = pytz.timezone(tz_name)
37+
tz = ZoneInfo(tz_name)
3738
now = datetime.datetime.now(tz)
3839
return now.strftime("%Y-%m-%d %H:%M:%S %Z")
3940
except Exception as e:

lumix/timezones/convert.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
import sys
21
from datetime import datetime
32

3+
_zoneinfo_available = True
44
try:
55
from zoneinfo import ZoneInfo, available_timezones
6-
7-
USE_ZONEINFO = True
86
except ImportError:
97
try:
108
import pytz
119

12-
USE_ZONEINFO = False
13-
# Definisci available_timezones fittizio per compatibilità
14-
if not hasattr(sys.modules[__name__], "available_timezones"):
10+
_zoneinfo_available = False
1511

16-
def available_timezones():
17-
return []
12+
def available_timezones():
13+
return []
1814

1915
except ImportError:
20-
print("Error: pytz not installed. Run: pip install pytz")
21-
sys.exit(1)
16+
_zoneinfo_available = False
17+
ZoneInfo = None
18+
pytz = None
19+
20+
def available_timezones():
21+
return []
2222

2323

2424
def convert_timezone(
2525
dt_str: str, from_tz: str, to_tz: str, date_format: str = "%Y-%m-%d %H:%M"
2626
) -> str:
2727
try:
28-
if USE_ZONEINFO:
29-
# Verifica che i fusi siano validi
28+
if not _zoneinfo_available:
29+
if pytz is None:
30+
raise ValueError("pytz not installed")
31+
from_pytz = pytz.timezone(from_tz)
32+
to_pytz = pytz.timezone(to_tz)
33+
dt_naive = datetime.strptime(dt_str, date_format)
34+
dt_from = from_pytz.localize(dt_naive)
35+
dt_to = dt_from.astimezone(to_pytz)
36+
else:
3037
if from_tz not in available_timezones():
3138
raise ValueError(f"Fuso orario non valido: {from_tz}")
3239
if to_tz not in available_timezones():
3340
raise ValueError(f"Fuso orario non valido: {to_tz}")
3441
dt_naive = datetime.strptime(dt_str, date_format)
3542
dt_from = dt_naive.replace(tzinfo=ZoneInfo(from_tz))
3643
dt_to = dt_from.astimezone(ZoneInfo(to_tz))
37-
else:
38-
from_pytz = pytz.timezone(from_tz)
39-
to_pytz = pytz.timezone(to_tz)
40-
dt_naive = datetime.strptime(dt_str, date_format)
41-
dt_from = from_pytz.localize(dt_naive)
42-
dt_to = dt_from.astimezone(to_pytz)
4344
return dt_to.strftime(date_format)
4445
except Exception as e:
4546
raise ValueError(f"Errore nella conversione: {e}")

0 commit comments

Comments
 (0)