Mercurial > personal > weather-server
changeset 39:b77c8e7d2742
Use zoneinfo rather than pytz.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Tue, 01 Apr 2025 15:54:21 -0400 |
parents | d5a18ecebf47 |
children | 5cc8ce54ad84 |
files | pyproject.toml weather_server/common.py weather_server/locations.py weather_server/logfile.py weather_server/logfile_test.py weather_server/server.py weather_server/types.py |
diffstat | 7 files changed, 12 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/pyproject.toml Tue Apr 01 15:46:16 2025 -0400 +++ b/pyproject.toml Tue Apr 01 15:54:21 2025 -0400 @@ -5,13 +5,12 @@ [project] name = "weather-server" dynamic = ["version"] -requires-python = ">=3.7" +requires-python = ">=3.9" dependencies = [ "attrs", "flask", "flup", "pymongo", - "pytz", ] [tool.hatch.version]
--- a/weather_server/common.py Tue Apr 01 15:46:16 2025 -0400 +++ b/weather_server/common.py Tue Apr 01 15:54:21 2025 -0400 @@ -3,10 +3,9 @@ import typing as t import bson -import pytz BSON_OPTIONS = bson.DEFAULT_CODEC_OPTIONS.with_options( - tz_aware=True, tzinfo=pytz.UTC) + tz_aware=True, tzinfo=datetime.UTC) def bson_encode(data: t.Dict[str, t.Any]) -> bytes: @@ -29,4 +28,4 @@ def utc_now(): - return datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) + return datetime.datetime.now(datetime.UTC)
--- a/weather_server/locations.py Tue Apr 01 15:46:16 2025 -0400 +++ b/weather_server/locations.py Tue Apr 01 15:54:21 2025 -0400 @@ -4,8 +4,7 @@ import datetime import pathlib import typing as t - -import pytz +import zoneinfo from . import logfile from . import types @@ -61,9 +60,9 @@ def timezone(self) -> datetime.tzinfo: try: - return pytz.timezone(self.tz_name) - except pytz.UnknownTimeZoneError: - return pytz.UTC + return zoneinfo.ZoneInfo(self.tz_name) + except zoneinfo.ZoneInfoNotFoundError: + return datetime.UTC def __repr__(self) -> str: return f'<Location in {self.root!r}>'
--- a/weather_server/logfile.py Tue Apr 01 15:46:16 2025 -0400 +++ b/weather_server/logfile.py Tue Apr 01 15:54:21 2025 -0400 @@ -1,7 +1,6 @@ """The part which handles writing things out and reading things in from CSV. """ -import attr import collections import concurrent.futures as futures import contextlib @@ -11,11 +10,11 @@ import threading import typing as t +import attr import bson from . import common - # The number of entries to keep in memory without reading from disk. CACHED_ENTRIES = 16384
--- a/weather_server/logfile_test.py Tue Apr 01 15:46:16 2025 -0400 +++ b/weather_server/logfile_test.py Tue Apr 01 15:54:21 2025 -0400 @@ -6,14 +6,13 @@ import unittest import bson -import pytz from . import common from . import logfile def ts(n): - return datetime.datetime.utcfromtimestamp(n).replace(tzinfo=pytz.UTC) + return datetime.datetime.fromtimestamp(n, datetime.UTC) class LoggerTest(unittest.TestCase):
--- a/weather_server/server.py Tue Apr 01 15:46:16 2025 -0400 +++ b/weather_server/server.py Tue Apr 01 15:54:21 2025 -0400 @@ -5,7 +5,6 @@ import bson import flask -import pytz from . import common from . import locations @@ -43,7 +42,7 @@ entries = tuple(reader) except (KeyError, bson.InvalidBSON): flask.abort(400) - now = datetime.datetime.now(tz=pytz.UTC) + now = datetime.datetime.now(tz=datetime.UTC) loc.record(entries, now) return flask.jsonify({'status': 'OK'}) @@ -59,7 +58,7 @@ date = tz.normalize(last_reading.sample_time.astimezone(tz)) else: date = None - now = datetime.datetime.now(tz=pytz.UTC) + now = datetime.datetime.now(tz=datetime.UTC) diff = (now - date) if date else None is_recent = diff and diff < datetime.timedelta(hours=12) return flask.render_template( @@ -67,7 +66,7 @@ location=loc, last_reading=last_reading, date=date, - date_format=f'%H:%M' if is_recent else '%Y-%m-%d %H:%M') + date_format='%H:%M' if is_recent else '%Y-%m-%d %H:%M') @app.route('/<location>/recent') def recent(location: str):