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

Commit

Permalink
修复(calendar):添加 VTIMEZONE 以合乎 RFC 5545 的要求
Browse files Browse the repository at this point in the history
Merge pull request #11 from Hagb/upstream-timezone-comp
  • Loading branch information
zombie110year committed Sep 29, 2020
2 parents 49a402d + 82040dd commit 6a77eff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit 6a77eff

Please sign in to comment.