icalrecur.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*======================================================================
  2. FILE: icalrecur.h
  3. CREATOR: eric 20 March 2000
  4. (C) COPYRIGHT 2000, Eric Busboom <eric@softwarestudio.org>
  5. http://www.softwarestudio.org
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of either:
  8. The LGPL as published by the Free Software Foundation, version
  9. 2.1, available at: http://www.gnu.org/licenses/lgpl-2.1.html
  10. Or:
  11. The Mozilla Public License Version 1.0. You may obtain a copy of
  12. the License at http://www.mozilla.org/MPL/
  13. ========================================================================*/
  14. /**
  15. @file icalrecur.h
  16. @brief Routines for dealing with recurring time
  17. How to use:
  18. 1) Get a rule and a start time from a component
  19. @code
  20. icalproperty rrule;
  21. struct icalrecurrencetype recur;
  22. struct icaltimetype dtstart;
  23. rrule = icalcomponent_get_first_property(comp,ICAL_RRULE_PROPERTY);
  24. recur = icalproperty_get_rrule(rrule);
  25. start = icalproperty_get_dtstart(dtstart);
  26. @endcode
  27. Or, just make them up:
  28. @code
  29. recur = icalrecurrencetype_from_string("FREQ=YEARLY;BYDAY=SU,WE");
  30. dtstart = icaltime_from_string("19970101T123000")
  31. @endcode
  32. 2) Create an iterator
  33. @code
  34. icalrecur_iterator* ritr;
  35. ritr = icalrecur_iterator_new(recur,start);
  36. @endcode
  37. 3) Iterator over the occurrences
  38. @code
  39. struct icaltimetype next;
  40. while (next = icalrecur_iterator_next(ritr)
  41. && !icaltime_is_null_time(next){
  42. Do something with next
  43. }
  44. @endcode
  45. Note that that the time returned by icalrecur_iterator_next is in
  46. whatever timezone that dtstart is in.
  47. */
  48. #ifndef ICALRECUR_H
  49. #define ICALRECUR_H
  50. #include "libical_ical_export.h"
  51. #include "icalarray.h"
  52. #include "icaltime.h"
  53. /*
  54. * Recurrence enumerations
  55. */
  56. typedef enum icalrecurrencetype_frequency
  57. {
  58. /* These enums are used to index an array, so don't change the
  59. order or the integers */
  60. ICAL_SECONDLY_RECURRENCE = 0,
  61. ICAL_MINUTELY_RECURRENCE = 1,
  62. ICAL_HOURLY_RECURRENCE = 2,
  63. ICAL_DAILY_RECURRENCE = 3,
  64. ICAL_WEEKLY_RECURRENCE = 4,
  65. ICAL_MONTHLY_RECURRENCE = 5,
  66. ICAL_YEARLY_RECURRENCE = 6,
  67. ICAL_NO_RECURRENCE = 7
  68. } icalrecurrencetype_frequency;
  69. typedef enum icalrecurrencetype_weekday
  70. {
  71. ICAL_NO_WEEKDAY,
  72. ICAL_SUNDAY_WEEKDAY,
  73. ICAL_MONDAY_WEEKDAY,
  74. ICAL_TUESDAY_WEEKDAY,
  75. ICAL_WEDNESDAY_WEEKDAY,
  76. ICAL_THURSDAY_WEEKDAY,
  77. ICAL_FRIDAY_WEEKDAY,
  78. ICAL_SATURDAY_WEEKDAY
  79. } icalrecurrencetype_weekday;
  80. typedef enum icalrecurrencetype_skip
  81. {
  82. ICAL_SKIP_BACKWARD = 0,
  83. ICAL_SKIP_FORWARD,
  84. ICAL_SKIP_OMIT,
  85. ICAL_SKIP_UNDEFINED
  86. } icalrecurrencetype_skip;
  87. enum icalrecurrence_array_max_values
  88. {
  89. ICAL_RECURRENCE_ARRAY_MAX = 0x7f7f,
  90. ICAL_RECURRENCE_ARRAY_MAX_BYTE = 0x7f
  91. };
  92. /*
  93. * Recurrence enumerations conversion routines.
  94. */
  95. LIBICAL_ICAL_EXPORT icalrecurrencetype_frequency icalrecur_string_to_freq(const char *str);
  96. LIBICAL_ICAL_EXPORT const char *icalrecur_freq_to_string(icalrecurrencetype_frequency kind);
  97. LIBICAL_ICAL_EXPORT icalrecurrencetype_skip icalrecur_string_to_skip(const char *str);
  98. LIBICAL_ICAL_EXPORT const char *icalrecur_skip_to_string(icalrecurrencetype_skip kind);
  99. LIBICAL_ICAL_EXPORT const char *icalrecur_weekday_to_string(icalrecurrencetype_weekday kind);
  100. LIBICAL_ICAL_EXPORT icalrecurrencetype_weekday icalrecur_string_to_weekday(const char *str);
  101. /**
  102. * Recurrence type routines
  103. */
  104. /* See RFC 5545 Section 3.3.10, RECUR Value, and RFC 7529
  105. * for an explanation of the values and fields in struct icalrecurrencetype.
  106. *
  107. * The maximums below are based on lunisolar leap years (13 months)
  108. */
  109. #define ICAL_BY_SECOND_SIZE 62 /* 0 to 60 */
  110. #define ICAL_BY_MINUTE_SIZE 61 /* 0 to 59 */
  111. #define ICAL_BY_HOUR_SIZE 25 /* 0 to 23 */
  112. #define ICAL_BY_MONTH_SIZE 14 /* 1 to 13 */
  113. #define ICAL_BY_MONTHDAY_SIZE 32 /* 1 to 31 */
  114. #define ICAL_BY_WEEKNO_SIZE 56 /* 1 to 55 */
  115. #define ICAL_BY_YEARDAY_SIZE 386 /* 1 to 385 */
  116. #define ICAL_BY_SETPOS_SIZE ICAL_BY_YEARDAY_SIZE /* 1 to N */
  117. #define ICAL_BY_DAY_SIZE 7*(ICAL_BY_WEEKNO_SIZE-1)+1 /* 1 to N */
  118. /** Main struct for holding digested recurrence rules */
  119. struct icalrecurrencetype
  120. {
  121. icalrecurrencetype_frequency freq;
  122. /* until and count are mutually exclusive. */
  123. struct icaltimetype until;
  124. int count;
  125. short interval;
  126. icalrecurrencetype_weekday week_start;
  127. /* The BY* parameters can each take a list of values. Here I
  128. * assume that the list of values will not be larger than the
  129. * range of the value -- that is, the client will not name a
  130. * value more than once.
  131. * Each of the lists is terminated with the value
  132. * ICAL_RECURRENCE_ARRAY_MAX unless the list is full.
  133. */
  134. short by_second[ICAL_BY_SECOND_SIZE];
  135. short by_minute[ICAL_BY_MINUTE_SIZE];
  136. short by_hour[ICAL_BY_HOUR_SIZE];
  137. short by_day[ICAL_BY_DAY_SIZE]; /* Encoded value, see below */
  138. short by_month_day[ICAL_BY_MONTHDAY_SIZE];
  139. short by_year_day[ICAL_BY_YEARDAY_SIZE];
  140. short by_week_no[ICAL_BY_WEEKNO_SIZE];
  141. short by_month[ICAL_BY_MONTH_SIZE];
  142. short by_set_pos[ICAL_BY_SETPOS_SIZE];
  143. /* For RSCALE extension (RFC 7529) */
  144. char *rscale;
  145. icalrecurrencetype_skip skip;
  146. };
  147. LIBICAL_ICAL_EXPORT int icalrecurrencetype_rscale_is_supported(void);
  148. LIBICAL_ICAL_EXPORT icalarray *icalrecurrencetype_rscale_supported_calendars(void);
  149. LIBICAL_ICAL_EXPORT void icalrecurrencetype_clear(struct icalrecurrencetype *r);
  150. /**
  151. * Array Encoding
  152. *
  153. * The 'day' element of the by_day array is encoded to allow
  154. * representation of both the day of the week ( Monday, Tueday), but also
  155. * the Nth day of the week ( First tuesday of the month, last thursday of
  156. * the year) These routines decode the day values
  157. */
  158. /** 1 == Monday, etc. */
  159. LIBICAL_ICAL_EXPORT enum icalrecurrencetype_weekday icalrecurrencetype_day_day_of_week(short day);
  160. /** 0 == any of day of week. 1 == first, 2 = second, -2 == second to last, etc */
  161. LIBICAL_ICAL_EXPORT int icalrecurrencetype_day_position(short day);
  162. /**
  163. * The 'month' element of the by_month array is encoded to allow
  164. * representation of the "L" leap suffix (RFC 7529).
  165. * These routines decode the month values.
  166. */
  167. LIBICAL_ICAL_EXPORT int icalrecurrencetype_month_is_leap(short month);
  168. LIBICAL_ICAL_EXPORT int icalrecurrencetype_month_month(short month);
  169. /** Recurrance rule parser */
  170. /** Convert between strings and recurrencetype structures. */
  171. LIBICAL_ICAL_EXPORT struct icalrecurrencetype icalrecurrencetype_from_string(const char *str);
  172. LIBICAL_ICAL_EXPORT char *icalrecurrencetype_as_string(struct icalrecurrencetype *recur);
  173. LIBICAL_ICAL_EXPORT char *icalrecurrencetype_as_string_r(struct icalrecurrencetype *recur);
  174. /** Recurrence iteration routines */
  175. typedef struct icalrecur_iterator_impl icalrecur_iterator;
  176. /** Create a new recurrence rule iterator */
  177. LIBICAL_ICAL_EXPORT icalrecur_iterator *icalrecur_iterator_new(struct icalrecurrencetype rule,
  178. struct icaltimetype dtstart);
  179. /** Get the next occurrence from an iterator */
  180. LIBICAL_ICAL_EXPORT struct icaltimetype icalrecur_iterator_next(icalrecur_iterator *);
  181. /** Free the iterator */
  182. LIBICAL_ICAL_EXPORT void icalrecur_iterator_free(icalrecur_iterator *);
  183. /**
  184. * Fills array up with at most 'count' time_t values, each
  185. * representing an occurrence time in seconds past the POSIX epoch
  186. */
  187. LIBICAL_ICAL_EXPORT int icalrecur_expand_recurrence(char *rule, time_t start,
  188. int count, time_t * array);
  189. #endif