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 __init__(self, tz_name=None): """ :param str tz_name: The timezone to represent, if not specified it defaults to the local timezone. """ super(Timezone, self).__init__() if tz_name is None: tz_name = tzlocal.get_localzone().zone self.add('tzid', tz_name) tz_details = parse_tz_posix_env_var(get_tz_posix_env_var(tz_name)) timezone_standard = icalendar.TimezoneStandard() timezone_standard.add( 'dtstart', datetime.datetime(1601, 1, 1, 2, 0, tzinfo=dateutil.tz.tzutc())) timezone_standard.add( 'tzoffsetfrom', tz_details.offset + datetime.timedelta(0, SECONDS_IN_ONE_HOUR)) timezone_standard.add('tzoffsetto', tz_details.offset) if tz_details.offset_dst: timezone_standard.add('rrule', tz_details.dst_end) timezone_daylight = icalendar.TimezoneDaylight() timezone_daylight.add( 'dtstart', datetime.datetime(1601, 1, 1, 2, 0, tzinfo=dateutil.tz.tzutc())) timezone_daylight.add('tzoffsetfrom', tz_details.offset) timezone_daylight.add( 'tzoffsetto', tz_details.offset + datetime.timedelta(0, SECONDS_IN_ONE_HOUR)) timezone_daylight.add('rrule', tz_details.dst_start) self.add_component(timezone_daylight) self.add_component(timezone_standard)
def generate_ical(entries, filename): cal = ical.Calendar() timezone = ical.cal.Timezone() timezone.add('TZID', TIMEZONE) timezone.add('x-lic-location', TIMEZONE) tzs = ical.TimezoneStandard() tzs.add('tzname', 'MEZ') tzs.add('dtstart', datetime.datetime(1996, 10, 27, 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 = ical.TimezoneDaylight() tzd.add('tzname', 'MESZ') tzd.add('dtstart', datetime.datetime(1981, 3, 29, 2, 0, 0)) tzd.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'}) tzd.add('TZOFFSETFROM', datetime.timedelta(hours=1)) tzd.add('TZOFFSETTO', datetime.timedelta(hours=2)) timezone.add_component(tzs) timezone.add_component(tzd) cal.add_component(timezone) cal.add('version', '2.0') cal.add('prodid', '-//willenbot') cal.add('x-wr-calname', 'Stratum 0') for entry in entries: component = entry.getIcal() if component: cal.add_component(component) write_if_changed(filename, cal.to_ical())
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 _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_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 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()
config = package['silviatheo'] LANGS = config['langs'] TZ_ES = pytz.timezone('Europe/Madrid') WEBSITE = 'https://www.silviatheo.eu/' PREPARTY_URL = WEBSITE + '#pre-party' PREPARTY_ADDR = 'Saporem, Calle de Ventura de la Vega, 5, 28014 Madrid, Spain' PREPARTY_MAP = 'https://www.google.com/maps/place/Saporem/@40.4158492,-3.7010353,17z/data=!3m1!4b1!4m5!3m4!1s0xd42288175bcdb51:0x9bcb201a9344e5b!8m2!3d40.4158492!4d-3.6988466' TRANSPORTATION_URL = WEBSITE + '#transportation' TRANSPORTATION_ADDR = 'El Brillante, Plaza del Emperador Carlos V, 8, 28012 Madrid, Spain' TRANSPORTATION_MAP = 'https://www.google.com/maps/place/El+Brillante/@40.4084654,-3.6937362,19z/data=!3m1!4b1!4m5!3m4!1s0xd422628ae900001:0x9f47c22eb73ab37a!8m2!3d40.4084654!4d-3.693189' WEDDING_URL = WEBSITE WEDDING_ADDR = 'La Quinta de Illescas - Finca para bodas, A-42 KM.30, 45200 Illescas, Toledo, Spain' WEDDING_MAP = 'https://www.google.com/maps/place/La+Quinta+de+Illescas/@40.164897,-3.8161193,18.58z/data=!4m5!3m4!1s0xd41f4029c1e3685:0xe528694c1b18788e!8m2!3d40.1649416!4d-3.8158414' 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)
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()
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)