Skip to content

Commit 55ccc6c

Browse files
committed
fix: zero-pad date fallback so SQLite ORDER BY date works correctly
The API fallback f"{year}-{month}-{day}" used unpadded keys, producing dates like "2026-3-2" that break lexicographic sorting (June sorts before March, 11 before 2). Now zero-pads via :02d formatting. Also simplified sync_dates_from_sub to catch all mismatches including legacy unpadded dates.
1 parent 67cead7 commit 55ccc6c

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/api/icourse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ def get_course_detail(self, course_id: str) -> dict:
149149
# Real lecture date is embedded in sub_title
150150
# ("2026-03-05第6-8节" → "2026-03-05"); fall back
151151
# to the server's year/month/day keys if missing.
152+
# Zero-pad the fallback so SQLite ORDER BY works.
152153
date = (
153154
_extract_date_from_sub(sub_title)
154-
or f"{year}-{month}-{day}"
155+
or f"{int(year):04d}-{int(month):02d}-{int(day):02d}"
155156
)
156157
lectures.append(
157158
{

src/data/database.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,18 @@ def get_unsent_lectures(self) -> list[dict]:
400400
return [dict(row) for row in rows]
401401

402402
def sync_dates_from_sub(self) -> int:
403-
"""Fix existing date rows where sub_title date differs from date column.
403+
"""Fix date rows where the stored value is wrong or badly formatted.
404404
405405
sub_title format is "2026-03-05第6-8节"; the embedded date is the
406-
real class date. Returns number of rows corrected.
406+
real class date. Also corrects zero-padding so that SQLite ORDER BY
407+
date works correctly (e.g. "2026-3-2" → "2026-03-02").
408+
Returns number of rows corrected.
407409
"""
408410
with self.conn:
409411
cur = self.conn.execute(r"""
410412
UPDATE lectures
411413
SET date = substr(sub_title, 1, 10)
412414
WHERE sub_title GLOB '????-??-??*'
413-
AND substr(sub_title, 1, 10) != date
415+
AND date != substr(sub_title, 1, 10)
414416
""")
415417
return cur.rowcount or 0

0 commit comments

Comments
 (0)