def InitCalWithPrelims(): cal = Calendar() cal.add('prodid', '-//CalPy') cal.add('version', '2.0') cal.add('calscale', 'GREGORIAN') cal.add('method', 'PUBLISH') tzd = icalendar.TimezoneDaylight() tzd.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '2su'}) tzd.add('dtstart', datetime.datetime(2007, 3, 11, 2, 0, 0)) tzd.add('tzname', 'MDT') tzd.add('TZOFFSETFROM', datetime.timedelta(hours=-7)) tzd.add('TZOFFSETTO', datetime.timedelta(hours=-6)) tzs = icalendar.TimezoneStandard() tzs.add('rrule', {'freq': 'yearly', 'bymonth': 11, 'byday': '1su'}) tzs.add('dtstart', datetime.datetime(2007, 11, 4, 2, 0, 0)) tzs.add('tzname', 'MST') tzs.add('TZOFFSETFROM', datetime.timedelta(hours=-6)) tzs.add('TZOFFSETTO', datetime.timedelta(hours=-7)) tzc = icalendar.Timezone() tzc.add('tzid', 'America/Denver') tzc.add_component(tzd) tzc.add_component(tzs) cal.add_component(tzc) return cal
def icalTimezone(tz, schedules): ical_tz = icalendar.Timezone() ical_tz.add('tzid', tz.zone) transition_times = tz._utc_transition_times start = min(schedules, key=lambda sch: sch['term']['start'])['term']['start'] start = parseDate(start) end = max(schedules, key=lambda sch: sch['term']['end'])['term']['end'] end = parseDate(end) start_idx = bisect.bisect_left(transition_times, start) assert start_idx > 0 from_offset = tz.utcoffset(transition_times[start_idx - 1]) for time in transition_times[start_idx:]: offset = tz.utcoffset(time) tzname = tz.tzname(time) dst = tz.dst(time) time += from_offset if dst.total_seconds() == 0: transition = icalendar.TimezoneStandard() else: transition = icalendar.TimezoneDaylight() transition.add('tzname', tzname) transition.add('tzoffsetfrom', from_offset) transition.add('tzoffsetto', offset) transition.add('dtstart', time) ical_tz.add_component(transition) from_offset = offset if time > end: break return ical_tz
def test_create_to_ical(self): cal = icalendar.Calendar() cal.add('prodid', u"-//Plone.org//NONSGML plone.app.event//EN") cal.add('version', u"2.0") cal.add('x-wr-calname', u"test create calendar") cal.add('x-wr-caldesc', u"icalendar tests") cal.add('x-wr-relcalid', u"12345") cal.add('x-wr-timezone', u"Europe/Vienna") tzc = icalendar.Timezone() tzc.add('tzid', 'Europe/Vienna') tzc.add('x-lic-location', 'Europe/Vienna') tzs = icalendar.TimezoneStandard() tzs.add('tzname', 'CET') tzs.add('dtstart', datetime.datetime(1970, 10, 25, 3, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) tzs.add('TZOFFSETFROM', datetime.timedelta(hours=2)) tzs.add('TZOFFSETTO', datetime.timedelta(hours=1)) tzd = icalendar.TimezoneDaylight() tzd.add('tzname', 'CEST') tzd.add('dtstart', datetime.datetime(1970, 3, 29, 2, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) tzd.add('TZOFFSETFROM', datetime.timedelta(hours=1)) tzd.add('TZOFFSETTO', datetime.timedelta(hours=2)) tzc.add_component(tzs) tzc.add_component(tzd) cal.add_component(tzc) event = icalendar.Event() tz = pytz.timezone("Europe/Vienna") event.add('dtstart', datetime.datetime(2012, 02, 13, 10, 00, 00, tzinfo=tz))
def construct_icalendar(context, events): """Returns an icalendar.Calendar object. :param context: A content object, which is used for calendar details like Title and Description. Usually a container, collection or the event itself. :param events: The list of event objects, which are included in this calendar. """ cal = icalendar.Calendar() cal.add('prodid', PRODID) cal.add('version', VERSION) cal_tz = default_timezone(context) if cal_tz: cal.add('x-wr-timezone', cal_tz) tzmap = {} if not hasattr(events, '__getslice__'): # LazyMap doesn't have __iter__ events = [events] for event in events: if ICatalogBrain.providedBy(event) or\ IContentListingObject.providedBy(event): event = event.getObject() acc = IEventAccessor(event) tz = acc.timezone # TODO: the standard wants each recurrence to have a valid timezone # definition. sounds decent, but not realizable. if not acc.whole_day: # whole day events are exported as dates without # timezone information tzmap = add_to_zones_map(tzmap, tz, acc.start) tzmap = add_to_zones_map(tzmap, tz, acc.end) cal.add_component(IICalendarEventComponent(event).to_ical()) for (tzid, transitions) in tzmap.items(): cal_tz = icalendar.Timezone() cal_tz.add('tzid', tzid) cal_tz.add('x-lic-location', tzid) for (transition, tzinfo) in transitions.items(): if tzinfo['dst']: cal_tz_sub = icalendar.TimezoneDaylight() else: cal_tz_sub = icalendar.TimezoneStandard() cal_tz_sub.add('tzname', tzinfo['name']) cal_tz_sub.add('dtstart', transition) cal_tz_sub.add('tzoffsetfrom', tzinfo['tzoffsetfrom']) cal_tz_sub.add('tzoffsetto', tzinfo['tzoffsetto']) # TODO: add rrule # tzi.add('rrule', # {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) cal_tz.add_component(cal_tz_sub) cal.add_component(cal_tz) return cal
def _vtimezone_with_dst(tzswitches, timezone): daylight = [] standard = [] prev_prev_transition_info = None prev_prev_component = None prev_component = None _, prev_transition_info = next(tzswitches, None) if prev_transition_info is not None: for (transition_time, transition_info) in tzswitches: utc_offset, dst_offset, tz_name = transition_info # utc-offset of the previous component prev_utc_offset = prev_transition_info[0] if prev_prev_component is not None and transition_info == prev_prev_transition_info: # DST-change is the same as earlier; merge the components rather than creating a new one! prev_prev_component.add('rdate', transition_time + prev_utc_offset) # Swap prev_prev and prev components temp_component = prev_component prev_component = prev_prev_component prev_prev_component = temp_component else: component = None lst = None is_dst = dst_offset.total_seconds() != 0 if is_dst: component = icalendar.TimezoneDaylight() lst = daylight else: component = icalendar.TimezoneStandard() lst = standard component.add('dtstart', transition_time + prev_utc_offset) component.add('rdate', transition_time + prev_utc_offset) component.add('tzoffsetfrom', prev_utc_offset) component.add('tzoffsetto', utc_offset) component.add('tzname', tz_name) lst.append(component) prev_prev_component = prev_component prev_component = component prev_prev_transition_info = prev_transition_info prev_transition_info = transition_info # Create timezone component, and add all standard/dst components to it vtimezone = icalendar.Timezone(tzid=timezone) for d in daylight: vtimezone.add_component(d) for s in standard: vtimezone.add_component(s) return vtimezone
def _vtimezone_without_dst(std, timezone): vtimezone = icalendar.Timezone(tzid=timezone) standard = icalendar.TimezoneStandard() utc_offset, dst_offset, tz_name = std[1] standard.add('dtstart', std[0]) standard.add('tzoffsetfrom', utc_offset) standard.add('tzoffsetto', utc_offset) standard.add('tzname', tz_name) vtimezone.add_component(standard) return vtimezone
def getIcal(name: str): calt = icalendar.Calendar() calt['version'] = '2.0' calt['prodid'] = '-//Zxilly//JGSUCalender//CN' calt['X-WR-TIMEZONE'] = 'Asia/Shanghai' calt['X-WR-CALNAME'] = name tz = icalendar.Timezone() tz['tzid'] = 'Asia/Shanghai' tzStandard = icalendar.TimezoneStandard() tzStandard.add('X-LIC-LOCATION', 'Asia/Shanghai') tzStandard.add('TZOFFSETFROM', timedelta(hours=8)) tzStandard.add('TZOFFSETTO', timedelta(hours=8)) tzStandard.add('TZNAME', 'CST') tz.add_component(tzStandard) calt.add_component(tz) return calt
def _create_timezone_static(tz): """create an icalendar vtimezone from a pytz.tzinfo.StaticTzInfo :param tz: the timezone :type tz: pytz.tzinfo.StaticTzInfo :returns: timezone information :rtype: icalendar.Timezone() """ timezone = icalendar.Timezone() timezone.add('TZID', tz) subcomp = icalendar.TimezoneStandard() subcomp.add('TZNAME', tz) subcomp.add('DTSTART', dt.datetime(1601, 1, 1)) subcomp.add('RDATE', dt.datetime(1601, 1, 1)) subcomp.add('TZOFFSETTO', tz._utcoffset) subcomp.add('TZOFFSETFROM', tz._utcoffset) timezone.add_component(subcomp) return timezone
def _calendar_header(): # generate ical file calendar = icalendar.Calendar({ 'PRODID': '-//TechisBe//Berlin tech events//DE', 'METHOD': 'PUBLISH', 'VERSION': '2.0', 'X-WR-CALNAME': 'TechisB - Berlin tech events via https://techisb.de', 'X-WR-TIMEZONE': 'Europe/Berlin', 'X-WR-CALDESC': 'All the relevant Berlin tech events in one calendar' }) # Provide timezone info - # see https://github.com/collective/icalendar/blob/master/src/icalendar/tests/test_timezoned.py#L50 tzc = icalendar.Timezone() tzc.add('tzid', 'Europe/Berlin') tzc.add('x-lic-location', 'Europe/Berlin') tzs = icalendar.TimezoneStandard() tzs.add('tzname', 'CET') tzs.add('dtstart', datetime.datetime(1970, 10, 25, 3, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) tzs.add('TZOFFSETFROM', datetime.timedelta(hours=2)) tzs.add('TZOFFSETTO', datetime.timedelta(hours=1)) tzd = icalendar.TimezoneDaylight() tzd.add('tzname', 'CEST') tzd.add('dtstart', datetime.datetime(1970, 3, 29, 2, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) tzd.add('TZOFFSETFROM', datetime.timedelta(hours=1)) tzd.add('TZOFFSETTO', datetime.timedelta(hours=2)) tzc.add_component(tzs) tzc.add_component(tzd) calendar.add_component(tzc) return calendar
def create_eastern_vtimezone(): tz = icalendar.Timezone() tz.add('tzid', "US-Eastern") tzs = icalendar.TimezoneStandard() tzs.add('dtstart', datetime(1970, 10, 25, 3, 0, 0)) tzs.add('RRULE', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) tzs.add('TZOFFSETFROM', timedelta(hours=-4)) tzs.add('TZOFFSETTO', timedelta(hours=-5)) tzs.add('TZNAME', "EST") tzd = icalendar.TimezoneDaylight() tzd.add('dtstart', datetime(1970, 10, 25, 2, 0, 0)) tzd.add('RRULE', {'freq': 'yearly', 'bymonth': 4, 'byday': '1su'}) tzd.add('TZOFFSETFROM', timedelta(hours=-5)) tzd.add('TZOFFSETTO', timedelta(hours=-4)) tzd.add('TZNAME', "EDT") tz.add_component(tzs) tz.add_component(tzd) return tz
def _create_timezone(self, tz): """ create an icalendar timezone from a pytz.tzinfo :param tz: the timezone :type tz: pytz.tzinfo :returns: timezone information set :rtype: icalendar.Timezone() """ timezone = icalendar.Timezone() timezone.add('TZID', tz) # FIXME should match year of the event, not this year this_year = datetime.datetime.today().year daylight, standard = [ (num, dt) for num, dt in enumerate(tz._utc_transition_times) if dt.year == this_year ] timezone_daylight = icalendar.TimezoneDaylight() timezone_daylight.add('TZNAME', tz._transition_info[daylight[0]][2]) timezone_daylight.add('DTSTART', daylight[1]) timezone_daylight.add('TZOFFSETFROM', tz._transition_info[daylight[0]][0]) timezone_daylight.add('TZOFFSETTO', tz._transition_info[standard[0]][0]) timezone_standard = icalendar.TimezoneStandard() timezone_standard.add('TZNAME', tz._transition_info[standard[0]][2]) timezone_standard.add('DTSTART', standard[1]) timezone_standard.add('TZOFFSETFROM', tz._transition_info[standard[0]][0]) timezone_standard.add('TZOFFSETTO', tz._transition_info[daylight[0]][0]) timezone.add_component(timezone_daylight) timezone.add_component(timezone_standard) return timezone
def initial(calendar): calendar.add("METHOD", "PUBLISH") calendar.add("VERSION", "2.0") calendar.add("X-WR-CALNAME", "课程表") calendar.add("PRODID", "-//Apple Inc.//Mac OS X 10.12//EN") calendar.add("X-APPLE-CALENDAR-COLOR", "#FC4208") calendar.add("X-WR-TIMEZONE", "Asia/Shanghai") calendar.add("CALSCALE", "GREGORIAN") # 创建Timezone对象 timezone = icalendar.Timezone() timezone.add("TZID", "Asia/Shanghai") # 创建TimezoneStandard对象 stander = icalendar.TimezoneStandard() # 实现添加TZOFFSETFROM:+0900 stander.add("TZOFFSETFROM", timedelta(0.375)) # 添加RRULE:FREQ=YEARLY;UNTIL=19910914T150000Z;BYMONTH=9;BYDAY=3SU arguements = {} arguements["FREQ"] = "YEARLY" arguements["UNTIL"] = datetime(1991, 9, 14, 15, 0, 0, tzinfo=UTC) arguements["BYMONTH"] = "9" arguements["BYDAY"] = "3SU" stander.add("RRULE", arguements) stander.add("DTSTART", datetime(1989, 9, 17, 0, 0, 0)) stander.add("TZNAME", "GMT+8") # 实现添加TZOFFSETTO:+0800 stander.add("TZOFFSETTO", timedelta(1 / 3)) timezone.add_component(stander) daylight = icalendar.TimezoneDaylight() daylight.add("TZOFFSETFROM", timedelta(1 / 3)) daylight.add("DTSTART", datetime(1991, 4, 14, 0, 0, 0)) daylight.add("TZNAME", "GMT+8") daylight.add("TZOFFSETTO", timedelta(0.375)) daylight.add("RDATE", datetime(1991, 4, 14, 0, 0, 0)) timezone.add_component(daylight) calendar.add_component(timezone) return calendar pass
def generate_vtimezone(tzid): """ Generate a vtimezone from a timezone id. See https://gist.github.com/pgcd/2f2e880e64044c1d86f8d50c0b6f235b. """ if not tzid: # UTC as a fallback doesn't work, since it has no transition info return None timezone = pytz.timezone(tzid) now = datetime.datetime.now() dst1, std1, dst2, std2 = filter( lambda x: x[0].year in (now.year, now.year + 1), zip(timezone._utc_transition_times, timezone._transition_info)) vtz = icalendar.Timezone(TZID=tzid) tz_comp = icalendar.TimezoneDaylight() utcoffset, _, tzname = dst1[1] offsetfrom = std1[1][0] tz_comp.add('dtstart', dst1[0] + offsetfrom) tz_comp.add('rdate', dst1[0] + offsetfrom) tz_comp.add('rdate', dst2[0] + offsetfrom) tz_comp.add('tzoffsetfrom', offsetfrom) tz_comp.add('tzoffsetto', utcoffset) tz_comp.add('tzname', tzname) vtz.add_component(tz_comp) tz_comp = icalendar.TimezoneStandard() utcoffset, _, tzname = std1[1] offsetfrom = dst1[1][0] tz_comp.add('dtstart', std1[0] + offsetfrom) tz_comp.add('rdate', std1[0] + offsetfrom) tz_comp.add('rdate', std2[0] + offsetfrom) tz_comp.add('tzoffsetfrom', offsetfrom) tz_comp.add('tzoffsetto', utcoffset) tz_comp.add('tzname', tzname) vtz.add_component(tz_comp) return vtz
def _vtimezone_with_dst(dst1, std1, dst2, std2, timezone): vtimezone = icalendar.Timezone(tzid=timezone) daylight = icalendar.TimezoneDaylight() utc_offset, _, tz_name = dst1[1] offsetfrom = std1[1][0] daylight.add('dtstart', dst1[0] + offsetfrom) daylight.add('rdate', dst1[0] + offsetfrom) daylight.add('rdate', dst2[0] + offsetfrom) daylight.add('tzoffsetfrom', offsetfrom) daylight.add('tzoffsetto', utc_offset) daylight.add('tzname', tz_name) vtimezone.add_component(daylight) standard = icalendar.TimezoneStandard() utc_offset, _, tz_name = std1[1] offsetfrom = dst1[1][0] standard.add('dtstart', std1[0] + offsetfrom) standard.add('rdate', std1[0] + offsetfrom) standard.add('rdate', std2[0] + offsetfrom) standard.add('tzoffsetfrom', offsetfrom) standard.add('tzoffsetto', utc_offset) standard.add('tzname', tz_name) vtimezone.add_component(standard) return vtimezone
def create_ical(self, name, summary, description, location, count, st_hr, st_mn, et_hr, et_mn, day, start_date): """ Create iCal file with given values """ cal = ical.Calendar() """ Add Timezone """ timezone = ical.Timezone() timezone.add('TZID', pytz.timezone('US/Eastern')) timezone_standard = ical.TimezoneStandard() timezone_standard.add('DTSTART', dt.datetime(1601, 11, 4, 2, 0, 0)) timezone_standard.add('RRULE', { 'FREQ': 'YEARLY', 'BYDAY': '1SU', 'BYMONTH': '11' }) timezone_standard.add('TZOFFSETFROM', dt.timedelta(hours=-4)) timezone_standard.add('TZOFFSETTO', dt.timedelta(hours=-5)) timezone_daylight = ical.TimezoneDaylight() timezone_daylight.add('DTSTART', dt.datetime(1601, 3, 11, 2, 0, 0)) timezone_daylight.add('RRULE', { 'FREQ': 'YEARLY', 'BYDAY': '2SU', 'BYMONTH': '3' }) timezone_daylight.add('TZOFFSETFROM', dt.timedelta(hours=-5)) timezone_daylight.add('TZOFFSETTO', dt.timedelta(hours=-4)) timezone.add_component(timezone_standard) timezone.add_component(timezone_daylight) cal.add_component(timezone) """ Add Event""" event = ical.Event() event.add( 'DTSTART', dt.datetime(2015, 9, start_date, st_hr, st_mn, 0, tzinfo=pytz.timezone('US/Eastern'))) event.add( 'DTEND', dt.datetime(2015, 9, start_date, et_hr, et_mn, 0, tzinfo=pytz.timezone('US/Eastern'))) event.add('SUMMARY', summary) event.add('DESCRIPTION', description) event.add('LOCATION', location) event.add('RRULE', {'FREQ': 'weekly', 'COUNT': count, 'BYDAY': day}) cal.add_component(event) f = open(name, "wb") f.write(cal.to_ical()) f.close()
def create_timezone(tz, first_date=None, last_date=None): """ create an icalendar vtimezone from a pytz.tzinfo object :param tz: the timezone :type tz: pytz.tzinfo :param first_date: the very first datetime that needs to be included in the transition times, typically the DTSTART value of the (first recurring) event :type first_date: datetime.datetime :param last_date: the last datetime that needs to included, typically the end of the (very last) event (of a recursion set) :returns: timezone information :rtype: icalendar.Timezone() we currently have a problem here: pytz.timezones only carry the absolute dates of time zone transitions, not their RRULEs. This will a) make for rather bloated VTIMEZONE components, especially for long recurring events, b) we'll need to specify for which time range this VTIMEZONE should be generated and c) will not be valid for recurring events that go into eternity. Possible Solutions: As this information is not provided by pytz at all, there is no easy solution, we'd really need to ship another version of the OLSON DB. """ if isinstance(tz, pytz.tzinfo.StaticTzInfo): return _create_timezone_static(tz) # TODO last_date = None, recurring to infinity first_date = dt.datetime.today() if not first_date else to_naive_utc(first_date) last_date = dt.datetime.today() if not last_date else to_naive_utc(last_date) timezone = icalendar.Timezone() timezone.add('TZID', tz) dst = { one[2]: 'DST' in two.__repr__() for one, two in iter(tz._tzinfos.items()) } bst = { one[2]: 'BST' in two.__repr__() for one, two in iter(tz._tzinfos.items()) } # looking for the first and last transition time we need to include first_num, last_num = 0, len(tz._utc_transition_times) - 1 first_tt = tz._utc_transition_times[0] last_tt = tz._utc_transition_times[-1] for num, transtime in enumerate(tz._utc_transition_times): if transtime > first_tt and transtime < first_date: first_num = num first_tt = transtime if transtime < last_tt and transtime > last_date: last_num = num last_tt = transtime timezones = dict() for num in range(first_num, last_num + 1): name = tz._transition_info[num][2] if name in timezones: ttime = tz.fromutc(tz._utc_transition_times[num]).replace(tzinfo=None) if 'RDATE' in timezones[name]: timezones[name]['RDATE'].dts.append( icalendar.prop.vDDDTypes(ttime)) else: timezones[name].add('RDATE', ttime) continue if dst[name] or bst[name]: subcomp = icalendar.TimezoneDaylight() else: subcomp = icalendar.TimezoneStandard() subcomp.add('TZNAME', tz._transition_info[num][2]) subcomp.add( 'DTSTART', tz.fromutc(tz._utc_transition_times[num]).replace(tzinfo=None)) subcomp.add('TZOFFSETTO', tz._transition_info[num][0]) subcomp.add('TZOFFSETFROM', tz._transition_info[num - 1][0]) timezones[name] = subcomp for subcomp in timezones.values(): timezone.add_component(subcomp) return timezone
def get_calendar(schedules, cal, cal_name): # calendar metadata if cal is None: cal = Calendar() cal.add('prodid', '-//Uni Hannover//Room ICal Calendar//DE') cal.add('X-WR-CALNAME', vText(schedules[0].room_name)) cal.add('X-WR-TIMEZONE', 'Europe/Berlin') cal.add('version', '2.0') cal.add('calscale', 'GREGORIAN') cal.add('method', 'PUBLISH') tzc = icalendar.Timezone() tzc.add('tzid', 'Europe/Berlin') tzc.add('x-lic-location', 'Europe/Berlin') tzs = icalendar.TimezoneStandard() tzs.add('tzname', 'CET') tzs.add('dtstart', datetime(1970, 10, 25, 3, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) tzs.add('TZOFFSETFROM', timedelta(hours=2)) tzs.add('TZOFFSETTO', timedelta(hours=1)) tzd = icalendar.TimezoneDaylight() tzd.add('tzname', 'CEST') tzd.add('dtstart', datetime(1970, 3, 29, 2, 0, 0)) tzd.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) tzd.add('TZOFFSETFROM', timedelta(hours=1)) tzd.add('TZOFFSETTO', timedelta(hours=2)) tzc.add_component(tzs) tzc.add_component(tzd) cal.add_component(tzc) #======================================================================= #======================================================================= # cal.add('BEGIN','VTIMEZONE') # cal.add('TZID','Europe/Berlin') # cal.add('X-LIC-LOCATION','Europe/Berlin') # cal.add('BEGIN','DAYLIGHT') # cal.add('TZOFFSETFROM',timedelta(hours=1)) # cal.add('TZOFFSETTO',timedelta(hours=2)) # cal.add('TZNAME','CEST') # cal.add('DTSTART',datetime(1970, 3, 29, 2, 0, 0)) # cal.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) # cal.add('END','DAYLIGHT') # cal.add('BEGIN','STANDARD') # cal.add('TZOFFSETFROM',timedelta(hours=2)) # cal.add('TZOFFSETTO',timedelta(hours=1)) # cal.add('TZNAME','CEST') # cal.add('DTSTART',datetime(1970,10,25, 3, 0, 0)) # cal.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) # cal.add('END','STANDARD') # cal.add('END','VTIMEZONE') #======================================================================= #======================================================================= for schedule in schedules: for reservation in schedule.reservations: event = Event() event.add('summary', reservation["title"]) event.add('dtstart', reservation["start"]) event.add('dtend', reservation["end"]) event.add('url', reservation["href"]) event['location'] = vText(schedule.room_name) cal.add_component(event) return cal
def siakng_ics(): if request.method == 'GET': return render_template('index.html') try: raw = request.form.get('tabel-jadwal') raw = raw[raw.index(prefix):raw.index(suffix) + len(suffix)] raw = raw.replace('<br>', delimiter) rows = pandas.read_html(raw)[0].to_dict('records') cal = icalendar.Calendar() cal.add('prodid', 'PRODID:-//faisalhanif.id//NONSGML SIAKNG.ics//ID') cal.add('version', '2.0') tzc = icalendar.Timezone() tzc.add('tzid', 'Asia/Jakarta') tzc.add('x-lic-location', 'Asia/Jakarta') tzs = icalendar.TimezoneStandard() tzs.add('tzname', 'WIB') tzs.add('dtstart', datetime.datetime(1970, 1, 1, 0, 0, 0)) tzs.add('TZOFFSETFROM', datetime.timedelta(hours=7)) tzs.add('TZOFFSETTO', datetime.timedelta(hours=7)) tzc.add_component(tzs) cal.add_component(tzc) tz = pytz.timezone('Asia/Jakarta') for row in rows: row = list(row.values()) periode_raw = row[4] kelas_raw = row[2] jadwal_raw = row[5] ruangan_raw = row[6] periodes = periode_raw.split(delimiter) kelas = kelas_raw[:kelas_raw.index(delimiter)] jadwals = jadwal_raw.split(delimiter) ruangans = ruangan_raw.split(delimiter) assert len(periodes) == len(jadwals) == len(ruangans) for periode, jadwal, ruangan in zip(periodes, jadwals, ruangans): event = icalendar.Event() jadwal_by, jadwal = jadwal.split(', ') periode_op, periode_ed = periode.split(' - ') op, ed = [ datetime.datetime.strptime(periode_op + ' ' + jam, '%d/%m/%Y %H.%M') for jam in jadwal.split('-') ] op = onDay(op, days_i[jadwal_by]) ed = onDay(ed, days_i[jadwal_by]) true_ed = datetime.datetime.strptime( periode_ed + ' ' + '23.59.59', '%d/%m/%Y %H.%M.%S') event.add('dtstart', tz.localize(op)) event.add('dtend', tz.localize(ed)) event.add('dtstamp', tz.localize(datetime.datetime.today())) event.add('rrule', {'freq': 'weekly', 'until': true_ed}) event.add('summary', kelas) event.add('location', ruangan) status = request.form.get('status') event.add('status', status_s[status]) event_hash = hashlib.sha1( (kelas + str(op)).encode()).hexdigest() event.add('uid', uuid.uuid5(uuid.NAMESPACE_URL, event_hash)) cal.add_component(event) return Response( cal.to_ical(), mimetype='text/calendar', headers={'Content-disposition': 'attachment; filename=SIAKNG.ics'}) except: return Response( 'ERROR', mimetype='text/plain', headers={'Content-disposition': 'attachment; filename=ERROR'})
tzd = icalendar.TimezoneDaylight() tzd.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '2su'}) tzd.add('dtstart', datetime.datetime(2007, 3, 11, 2, 0, 0)) tzd.add('tzname', 'MDT') tzd.add('TZOFFSETFROM', datetime.timedelta(hours=-7)) tzd.add('TZOFFSETTO', datetime.timedelta(hours=-6)) tzs = icalendar.TimezoneStandard() tzs.add('rrule', {'freq': 'yearly', 'bymonth': 11, 'byday': '1su'}) tzs.add('dtstart', datetime.datetime(2007, 11, 4, 2, 0, 0)) tzs.add('tzname', 'MST') tzs.add('TZOFFSETFROM', datetime.timedelta(hours=-6)) tzs.add('TZOFFSETTO', datetime.timedelta(hours=-7)) tzc = icalendar.Timezone() tzc.add('tzid', 'America/Denver') tzc.add_component(tzd) tzc.add_component(tzs) cal.add_component(tzc) tz = pytz.timezone('America/Denver') event = Event() event.add('dtstart', tz.localize(datetime.datetime(2018, 1, 13, 10, 00, 00))) event.add('dtend', tz.localize(datetime.datetime(2018, 1, 13, 11, 15, 00))) event.add('dtstamp', tz.localize(datetime.datetime(2018, 1, 9, 6, 27, 00))) event.add('created', tz.localize(datetime.datetime(2018, 1, 9, 6, 27, 00))) event.add('description', 'Description is really cool!') event.add('comment', 'Commentary about this event is that it\'s the bomb!')
def test_create_to_ical(self): cal = icalendar.Calendar() cal.add('prodid', u"-//Plone.org//NONSGML plone.app.event//EN") cal.add('version', u"2.0") cal.add('x-wr-calname', u"test create calendar") cal.add('x-wr-caldesc', u"icalendar tests") cal.add('x-wr-relcalid', u"12345") cal.add('x-wr-timezone', u"Europe/Vienna") tzc = icalendar.Timezone() tzc.add('tzid', 'Europe/Vienna') tzc.add('x-lic-location', 'Europe/Vienna') tzs = icalendar.TimezoneStandard() tzs.add('tzname', 'CET') tzs.add('dtstart', datetime.datetime(1970, 10, 25, 3, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) tzs.add('TZOFFSETFROM', datetime.timedelta(hours=2)) tzs.add('TZOFFSETTO', datetime.timedelta(hours=1)) tzd = icalendar.TimezoneDaylight() tzd.add('tzname', 'CEST') tzd.add('dtstart', datetime.datetime(1970, 3, 29, 2, 0, 0)) tzs.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) tzd.add('TZOFFSETFROM', datetime.timedelta(hours=1)) tzd.add('TZOFFSETTO', datetime.timedelta(hours=2)) tzc.add_component(tzs) tzc.add_component(tzd) cal.add_component(tzc) event = icalendar.Event() tz = pytz.timezone("Europe/Vienna") event.add( 'dtstart', tz.localize(datetime.datetime(2012, 2, 13, 10, 00, 00))) event.add( 'dtend', tz.localize(datetime.datetime(2012, 2, 17, 18, 00, 00))) event.add( 'dtstamp', tz.localize(datetime.datetime(2010, 10, 10, 10, 10, 10))) event.add( 'created', tz.localize(datetime.datetime(2010, 10, 10, 10, 10, 10))) event.add('uid', u'123456') event.add( 'last-modified', tz.localize(datetime.datetime(2010, 10, 10, 10, 10, 10))) event.add('summary', u'artsprint 2012') # event.add('rrule', u'FREQ=YEARLY;INTERVAL=1;COUNT=10') event.add('description', u'sprinting at the artsprint') event.add('location', u'aka bild, wien') event.add('categories', u'first subject') event.add('categories', u'second subject') event.add('attendee', u'häns') event.add('attendee', u'franz') event.add('attendee', u'sepp') event.add('contact', u'Max Mustermann, 1010 Wien') event.add('url', u'http://plone.org') cal.add_component(event) test_out = b'|'.join(cal.to_ical().splitlines()) test_out = test_out.decode('utf-8') vtimezone_lines = "BEGIN:VTIMEZONE|TZID:Europe/Vienna|X-LIC-LOCATION:" "Europe/Vienna|BEGIN:STANDARD|DTSTART;VALUE=DATE-TIME:19701025T03" "0000|RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10|RRULE:FREQ=YEARLY;B" "YDAY=-1SU;BYMONTH=3|TZNAME:CET|TZOFFSETFROM:+0200|TZOFFSETTO:+01" "00|END:STANDARD|BEGIN:DAYLIGHT|DTSTART;VALUE=DATE-TIME:19700329T" "020000|TZNAME:CEST|TZOFFSETFROM:+0100|TZOFFSETTO:+0200|END:DAYLI" "GHT|END:VTIMEZONE" self.assertTrue(vtimezone_lines in test_out) test_str = "DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20120213T100000" self.assertTrue(test_str in test_out) self.assertTrue("ATTENDEE:sepp" in test_out) # ical standard expects DTSTAMP and CREATED in UTC self.assertTrue("DTSTAMP;VALUE=DATE-TIME:20101010T081010Z" in test_out) self.assertTrue("CREATED;VALUE=DATE-TIME:20101010T081010Z" in test_out)
tz_d = icalendar.TimezoneDaylight() tz_d.add('tzname', 'CEST') tz_d.add('tzoffsetfrom', timedelta(hours=1)) tz_d.add('tzoffsetto', timedelta(hours=2)) tz_d.add('dtstart', datetime(1970, 3, 29, 2, 0, 0)) tz_d.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) tz_s = icalendar.TimezoneStandard() tz_s.add('tzname', 'CET') tz_s.add('tzoffsetfrom', timedelta(hours=2)) tz_s.add('tzoffsetto', timedelta(hours=1)) tz_s.add('dtstart', datetime(1970, 10, 25, 3, 0, 0)) tz_s.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'}) tz = icalendar.Timezone() tz.add('tzid', TZ_ES.zone) tz.add('tzurl', f'http://tzurl.org/zoneinfo-outlook/{TZ_ES.zone}') tz.add('x-lic-location', TZ_ES.zone) tz.add_component(tz_d) tz.add_component(tz_s) for lang in LANGS: preparty = icalendar.Event() preparty.add('summary', strings['calendar_preparty'][lang]) preparty.add('dtstart', datetime(2021, 7, 9, 20, 0, 0, tzinfo=TZ_ES)) preparty.add('dtend', datetime(2021, 7, 10, 2, 0, 0, tzinfo=TZ_ES)) preparty.add('url', urllib.parse.quote(PREPARTY_URL, safe='')) preparty.add('location', PREPARTY_ADDR) preparty.add('description', f'{PREPARTY_URL}\n\n{PREPARTY_ADDR}\n\n{PREPARTY_MAP}')
def get_calendar_entry(self): """ Build the iCalendar string for the event iCal validator, useful for debugging: http://icalvalid.cloudapp.net/ """ event = self.event event_tz = event.timezone creation_time = django_timezone.now() # Generate some description strings title, desc_plain, _desc_html = self.get_invite_texts() # Generate the Calendar event cal = icalendar.Calendar() cal.add("prodid", "-//OneEvent event entry//onevent//EN") cal.add("version", "2.0") cal.add("calscale", "GREGORIAN") cal.add("method", "REQUEST") # Generate timezone infos relevant to the event tzmap = {} tzmap = add_to_zones_map(tzmap, event_tz.zone, event.start) tzmap = add_to_zones_map(tzmap, event_tz.zone, event.get_real_end()) tzmap = add_to_zones_map(tzmap, django_timezone.get_default_timezone_name(), creation_time) for tzid, transitions in tzmap.items(): cal_tz = icalendar.Timezone() cal_tz.add("tzid", tzid) cal_tz.add("x-lic-location", tzid) for transition, tzinfo in transitions.items(): if tzinfo["dst"]: cal_tz_sub = icalendar.TimezoneDaylight() else: cal_tz_sub = icalendar.TimezoneStandard() cal_tz_sub.add("tzname", tzinfo["name"]) cal_tz_sub.add("dtstart", transition) cal_tz_sub.add("tzoffsetfrom", tzinfo["tzoffsetfrom"]) cal_tz_sub.add("tzoffsetto", tzinfo["tzoffsetto"]) cal_tz.add_component(cal_tz_sub) cal.add_component(cal_tz) cal_evt = icalendar.Event() cal_evt.add("uid", "event{0}-booking{1}@oneevent".format(event.id, self.id)) cal_evt.add("dtstamp", creation_time) cal_evt.add("dtstart", event.start.astimezone(event_tz)) cal_evt.add("dtend", event.get_real_end().astimezone(event_tz)) cal_evt.add("created", creation_time) cal_evt.add("sequence", "1") cal_evt.add("summary", title) cal_evt.add("description", desc_plain) cal_evt.add("location", vText(event.location_name)) cal_evt.add("category", "Event") cal_evt.add("status", "CONFIRMED") cal_evt.add("transp", "OPAQUE") cal_evt.add("priority", "5") cal_evt.add("class", "PUBLIC") organiser = vCalAddress("mailto:{0}".format(event.owner.email)) organiser.params["cn"] = vText(event.owner.get_full_name()) organiser.params["role"] = vText("CHAIR") cal_evt.add("organizer", organiser, encode=0) attendee = vCalAddress("mailto:{0}".format(self.person.email)) attendee.params["cutype"] = vText("INDIVIDUAL") attendee.params["role"] = vText("REQ-PARTICIPANT") attendee.params["partstat"] = vText("NEEDS-ACTION") attendee.params["rsvp"] = vText("FALSE") attendee.params["cn"] = vText(self.person.get_full_name()) cal_evt.add("attendee", attendee, encode=0) cal.add_component(cal_evt) return cal.to_ical()