[books] Allow timezone changes when importing from KOReader

Turns out you need a city-based timezone for DST stuff to work properly.
The US/Eastern timezone doesn't mess with DST because it can be so wonky
in different regions. So while we fix timezone defaulting to a
DST-friendly timezone too.
This commit is contained in:
2025-07-19 01:54:27 -04:00
parent 7c6e895ae4
commit 4db8793d5c
6 changed files with 126 additions and 55 deletions

View File

@ -4,7 +4,8 @@ import sqlite3
from datetime import datetime, timedelta
from enum import Enum
import pytz
import pendulum
from zoneinfo import ZoneInfo
import requests
from books.constants import BOOKS_TITLES_TO_IGNORE
from django.apps import apps
@ -208,6 +209,35 @@ def build_page_data(page_rows: list, book_map: dict, user_tz=None) -> dict:
)
return book_map
def one_off_fix_colins_profile(profile):
home_tz = "America/New_York"
europe = "2023-10-15"
europe_tz = "Europe/Paris"
europe_end = "2023-12-15"
washington = "2023-10-15"
washington_tz = "America/Los_Angeles"
washington_end = "2023-05-04"
camp = "2024-08-04"
camp_end = "2024-08-10"
camp_tz = "America/Halifax"
summer = "2025-07-10"
summer_end = "2025-07-13"
summer_tz = "America/Los_Angeles"
profile.timezone_change_log = ""
profile.timezone_change_log += f"{europe_tz} - {pendulum.parse(europe)}\n"
profile.timezone_change_log += f"{home_tz} - {pendulum.parse(europe_end)}\n"
profile.timezone_change_log += f"{washington_tz} - {pendulum.parse(washington)}\n"
profile.timezone_change_log += f"{home_tz} - {pendulum.parse(washington_end)}\n"
profile.timezone_change_log += f"{camp_tz} - {pendulum.parse(camp)}\n"
profile.timezone_change_log += f"{home_tz} - {pendulum.parse(camp_end)}\n"
profile.timezone_change_log += f"{summer_tz} - {pendulum.parse(summer)}\n"
profile.timezone_change_log += f"{home_tz} - {pendulum.parse(summer_end)}\n"
profile.save()
def build_scrobbles_from_book_map(
book_map: dict, user: "User"
@ -278,55 +308,18 @@ def build_scrobbles_from_book_map(
)
continue
timezone = user.profile.timezone
timestamp = user.profile.get_timestamp_with_tz(datetime.fromtimestamp(int(first_page.get("start_ts"))))
stop_timestamp = user.profile.get_timestamp_with_tz(datetime.fromtimestamp(int(last_page.get("end_ts"))))
timestamp = datetime.fromtimestamp(
int(first_page.get("start_ts"))
).replace(tzinfo=pytz.timezone(timezone))
if user.id == 1 and not user.profile.timezone_change_log:
one_off_fix_colins_profile(user.profile)
# Add a shim here temporarily to fix imports while we were in France
# if date is between 10/15 and 12/15, cast it to Europe/Central
if (
datetime(2023, 10, 15).replace(
tzinfo=pytz.timezone("Europe/Paris")
)
<= timestamp
<= datetime(2023, 12, 15).replace(
tzinfo=pytz.timezone("Europe/Paris")
)
):
timezone = "Europe/Paris"
if (
datetime(2024, 4, 28).replace(
tzinfo=pytz.timezone("US/Pacific")
)
<= timestamp
<= datetime(2024, 5, 4).replace(
tzinfo=pytz.timezone("US/Pacific")
)
):
timezone = "US/Pacific"
if (
datetime(2024, 8, 4).replace(
tzinfo=pytz.timezone("Canada/Atlantic")
)
<= timestamp
<= datetime(2024, 8, 10).replace(
tzinfo=pytz.timezone("Canada/Atlantic")
)
):
timezone = "Canada/Atlantic"
stop_timestamp = datetime.fromtimestamp(
int(last_page.get("end_ts"))
).replace(tzinfo=pytz.timezone(timezone))
if (
timestamp.tzinfo._dst.seconds == 0
or stop_timestamp.tzinfo._dst.seconds == 0
):
# Adjust for Daylight Saving Time
if timestamp.dst() == timedelta(0) or stop_timestamp.dst() == timedelta(0):
timestamp = timestamp - timedelta(hours=1)
stop_timestamp = stop_timestamp - timedelta(hours=1)
else:
print("In DST! ", timestamp)
scrobble = Scrobble.objects.filter(
timestamp=timestamp,
@ -356,7 +349,7 @@ def build_scrobbles_from_book_map(
in_progress=False,
played_to_completion=True,
long_play_complete=False,
timezone=timezone,
timezone=timestamp.tzinfo.name
)
)
# Then start over
@ -398,9 +391,9 @@ def process_koreader_sqlite_file(file_path, user_id) -> list:
new_scrobbles = []
user = User.objects.filter(id=user_id).first()
tz = pytz.utc
tz = ZoneInfo("UTC")
if user:
tz = user.profile.timezone
tz = user.profile.tzinfo
is_os_file = "https://" not in file_path
if is_os_file: