Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

修复(calendar):添加 VTIMEZONE 以合乎 RFC 5545 的要求 #11

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli_cqu/util/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from ..data.schedule import Schedule
from ..model import Course, ExperimentCourse
from ..util.datetime import materialize_calendar
from ..util.datetime import materialize_calendar, VTIMEZONE

__all__ = ("make_ical", )

Expand All @@ -22,6 +22,7 @@ def make_ical(courses: List[Union[Course, ExperimentCourse]], start: date,
cal = Calendar()
cal.add("prodid", "-//Zombie110year//CLI CQU//")
cal.add("version", "2.0")
cal.add_component(VTIMEZONE)
for course in courses:
for ev in build_event(course, start, schedule):
cal.add_component(ev)
Expand Down
18 changes: 15 additions & 3 deletions cli_cqu/util/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import time
from datetime import timedelta
from datetime import timezone
from icalendar import Timezone
from typing import *

from ..data.schedule import Schedule
Expand All @@ -16,6 +17,19 @@
# 14节 表示全天
FULL_DAY = 14

# 北京时间
VTIMEZONE: Timezone = Timezone.from_ical("""BEGIN:VTIMEZONE
TZID:Asia/Shanghai
X-LIC-LOCATION:Asia/Shanghai
BEGIN:STANDARD
TZNAME:CST
TZOFFSETFROM:+0800
TZOFFSETTO:+0800
DTSTART:19700101T000000
END:STANDARD
END:VTIMEZONE""")
TIMEZONE: timezone = VTIMEZONE.to_tz()


def materialize_calendar(t_week: str, t_lesson: str, start: date,
schedule: Schedule) -> Tuple[datetime, datetime]:
Expand Down Expand Up @@ -61,9 +75,7 @@ def materialize_calendar(t_week: str, t_lesson: str, start: date,
partial_times[0],
timedelta(days=partial_days) +
partial_times[1])
# timezone(timedelta(hours=8), "Asia/Shanghai"): 北京时间
dt: datetime = datetime.combine(
start, time.min, timezone(timedelta(hours=8), "Asia/Shanghai"))
dt: datetime = datetime.combine(start, time.min, TIMEZONE)
result = (dt + partial_td[0], dt + partial_td[1])
return result

Expand Down