Parsing and generating RRule in iCalendar format

Source: Internet
Author: User

Recently, I am working on a Calendar project, which is equivalent to Google Calendar or Calendar in Outlook. Using iCalendar in the release and sharing of Calendar is a standard for Calendar data exchange, see Wikipedia: http://zh.wikipedia.org/wiki/ICalendar

Because C # is used for development, we hope to find an open-source or free iCalendar component to help generate. ics files. Indeed someone did this, that is DDay. iCal, Open Source Address: http://sourceforge.net/projects/dday-ical/

For Calendar, common events are well set. The most troublesome thing is loop events. Loop events have multiple attributes that need to be set and the next occurrence time must be calculated. However, in the iCalendar standard, these loop settings, eventually convert to a string in RRule format (for RRule format standards, we can refer to the http://www.kanzaki.com/docs/ical/rrule.html ).

Since the ics file is generated using DDay. iCal, this component must also parse the RRule format and generate the RRule format. The following describes how to use DDay. iCal to process RRule.

I. RRule Parsing

DDay. iCal has a RecurrencePattern object, which can be used to describe loop settings. This object can be passed into the RRule string during construction. Then, we can use the GetOccurrences method of the RecurringComponent object to obtain the time when a cyclic event occurs within the specified time range.

Let's take a look at the Code:

RecurringComponent recurringComponent = new RecurringComponent (); RecurrencePattern pattern = new week ("FREQ = WEEKLY; BYDAY = MO"); // RRule is set to recurringComponent every Monday. recurrenceRules. add (pattern); recurringComponent. start = new iCalDateTime (Convert. toDateTime ("00:00:00"); // This cyclic event occurs from 6.1 var occurrences = recurringComponent. getOccurrences (Convert. toDateTime ("23:59:59"), // although we want to take an event after 7.1, it cannot be written as-1, this function is> startTime <= endTime Convert During calculation. toDateTime ("July 23:59:59"); // obtain the specific time when a cyclic event occurred in February (Occurrence occurrence in occurrences) {DateTime occurrenceTime = occurrence. period. startTime. local; Console. writeLine (occurrenceTime. toString ("yyyy-MM-dd "));}

Ii. RRule generation

RRule uses RecurrencePattern to set the attributes of the loop, and then uses the ToString method to obtain the RRule string.

The Code is as follows:

RecurrencePattern pattern = new RecurrencePattern (); pattern. byDay = new List <IWeekDay> () {new WeekDay (DayOfWeek. sunday)}; // pattern occurs on Sunday every week. frequency = FrequencyType. weekly; // the cycle is Weekly Console. writeLine (pattern. toString (); // generate the RRule Format String RecurringComponent recurringComponent = new RecurringComponent (); recurringComponent. recurrenceRules. add (pattern); recurringComponent. start = new iCalDateTime (Convert. toDateTime ("00:00:00"); // This cyclic event occurs from 6.1 var occurrences = recurringComponent. getOccurrences (Convert. toDateTime ("23:59:59"), // although we want to take an event after 7.1, it cannot be written as-1, this function is> startTime <= endTime Convert During calculation. toDateTime ("July 23:59:59"); // obtain the specific time when a cyclic event occurred in February (Occurrence occurrence in occurrences) {DateTime occurrenceTime = occurrence. period. startTime. local; Console. writeLine (occurrenceTime. toString ("yyyy-MM-dd "));}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.