icaltime.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*======================================================================
  2. FILE: icaltime.h
  3. CREATOR: eric 02 June 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. The Original Code is eric. The Initial Developer of the Original
  14. Code is Eric Busboom
  15. ======================================================================*/
  16. /** @file icaltime.h
  17. * @brief struct icaltimetype is a pseudo-object that abstracts time
  18. * handling.
  19. *
  20. * It can represent either a DATE or a DATE-TIME (floating, UTC or in a
  21. * given timezone), and it keeps track internally of its native timezone.
  22. *
  23. * The typical usage is to call the correct constructor specifying the
  24. * desired timezone. If this is not known until a later time, the
  25. * correct behavior is to specify a NULL timezone and call
  26. * icaltime_convert_to_zone() at a later time.
  27. *
  28. * There are several ways to create a new icaltimetype:
  29. *
  30. * - icaltime_null_time()
  31. * - icaltime_null_date()
  32. * - icaltime_current_time_with_zone()
  33. * - icaltime_today()
  34. * - icaltime_from_timet_with_zone(time_t tm, int is_date,
  35. * icaltimezone *zone)
  36. * - icaltime_from_string_with_zone(const char* str, icaltimezone *zone)
  37. * - icaltime_from_day_of_year(int doy, int year)
  38. * - icaltime_from_week_number(int week_number, int year)
  39. *
  40. * italtimetype objects can be converted to different formats:
  41. *
  42. * - icaltime_as_timet(struct icaltimetype tt)
  43. * - icaltime_as_timet_with_zone(struct icaltimetype tt,
  44. * icaltimezone *zone)
  45. * - icaltime_as_ical_string(struct icaltimetype tt)
  46. *
  47. * Accessor methods include:
  48. *
  49. * - icaltime_get_timezone(struct icaltimetype t)
  50. * - icaltime_get_tzid(struct icaltimetype t)
  51. * - icaltime_set_timezone(struct icaltimetype t, const icaltimezone *zone)
  52. * - icaltime_day_of_year(struct icaltimetype t)
  53. * - icaltime_day_of_week(struct icaltimetype t)
  54. * - icaltime_start_doy_of_week(struct icaltimetype t, int fdow)
  55. * - icaltime_week_number(struct icaltimetype t)
  56. *
  57. * Query methods include:
  58. *
  59. * - icaltime_is_null_time(struct icaltimetype t)
  60. * - icaltime_is_valid_time(struct icaltimetype t)
  61. * - icaltime_is_date(struct icaltimetype t)
  62. * - icaltime_is_utc(struct icaltimetype t)
  63. *
  64. * Modify, compare and utility methods include:
  65. *
  66. * - icaltime_compare_with_zone(struct icaltimetype a,struct icaltimetype b)
  67. * - icaltime_compare(struct icaltimetype a,struct icaltimetype b)
  68. * - icaltime_compare_date_only(struct icaltimetype a,
  69. * struct icaltimetype b)
  70. * - icaltime_adjust(struct icaltimetype *tt, int days, int hours,
  71. * int minutes, int seconds);
  72. * - icaltime_normalize(struct icaltimetype t);
  73. * - icaltime_convert_to_zone(const struct icaltimetype tt,
  74. * icaltimezone *zone);
  75. */
  76. #ifndef ICALTIME_H
  77. #define ICALTIME_H
  78. #include "libical_ical_export.h"
  79. #include <time.h>
  80. /* An opaque struct representing a timezone. We declare this here to avoid
  81. a circular dependancy. */
  82. #if !defined(ICALTIMEZONE_DEFINED)
  83. #define ICALTIMEZONE_DEFINED
  84. typedef struct _icaltimezone icaltimezone;
  85. #endif
  86. /** icaltime_span is returned by icalcomponent_get_span() */
  87. struct icaltime_span
  88. {
  89. time_t start; /**< in UTC */
  90. time_t end; /**< in UTC */
  91. int is_busy; /**< 1->busy time, 0-> free time */
  92. };
  93. typedef struct icaltime_span icaltime_span;
  94. /*
  95. * FIXME
  96. *
  97. * is_utc is redundant, and might be considered a minor optimization.
  98. * It might be deprecated, so you should use icaltime_is_utc() instead.
  99. */
  100. struct icaltimetype
  101. {
  102. int year; /**< Actual year, e.g. 2001. */
  103. int month; /**< 1 (Jan) to 12 (Dec). */
  104. int day;
  105. int hour;
  106. int minute;
  107. int second;
  108. int is_utc; /**< 1-> time is in UTC timezone */
  109. int is_date; /**< 1 -> interpret this as date. */
  110. int is_daylight; /**< 1 -> time is in daylight savings time. */
  111. const icaltimezone *zone; /**< timezone */
  112. };
  113. typedef struct icaltimetype icaltimetype;
  114. /** Return a null time, which indicates no time has been set.
  115. This time represent the beginning of the epoch */
  116. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_null_time(void);
  117. /** Return a null date */
  118. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_null_date(void);
  119. /** Returns the current time in the given timezone, as an icaltimetype. */
  120. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_current_time_with_zone(const icaltimezone *zone);
  121. /** Returns the current day as an icaltimetype, with is_date set. */
  122. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_today(void);
  123. /** Convert seconds past UNIX epoch to a timetype*/
  124. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_from_timet(const time_t v, const int is_date);
  125. /** Convert seconds past UNIX epoch to a timetype, using timezones. */
  126. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_from_timet_with_zone(const time_t tm,
  127. const int is_date,
  128. const icaltimezone *zone);
  129. /** create a time from an ISO format string */
  130. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_from_string(const char *str);
  131. /** create a time from an ISO format string */
  132. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_from_string_with_zone(const char *str,
  133. const icaltimezone *zone);
  134. /** Create a new time, given a day of year and a year. */
  135. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_from_day_of_year(const int doy, const int year);
  136. /** @brief Contructor (TODO).
  137. * Create a new time from a weeknumber and a year. */
  138. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_from_week_number(const int week_number,
  139. const int year);
  140. /** Return the time as seconds past the UNIX epoch */
  141. LIBICAL_ICAL_EXPORT time_t icaltime_as_timet(const struct icaltimetype);
  142. /** Return the time as seconds past the UNIX epoch, using timezones. */
  143. LIBICAL_ICAL_EXPORT time_t icaltime_as_timet_with_zone(const struct icaltimetype tt,
  144. const icaltimezone *zone);
  145. /** Return a string represention of the time, in RFC5545 format. */
  146. LIBICAL_ICAL_EXPORT const char *icaltime_as_ical_string(const struct icaltimetype tt);
  147. LIBICAL_ICAL_EXPORT char *icaltime_as_ical_string_r(const struct icaltimetype tt);
  148. /** @brief Return the timezone */
  149. LIBICAL_ICAL_EXPORT const icaltimezone *icaltime_get_timezone(const struct icaltimetype t);
  150. /** @brief Return the tzid, or NULL for a floating time */
  151. LIBICAL_ICAL_EXPORT const char *icaltime_get_tzid(const struct icaltimetype t);
  152. /** @brief Set the timezone */
  153. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_set_timezone(struct icaltimetype *t,
  154. const icaltimezone *zone);
  155. /** Return the day of the year of the given time */
  156. LIBICAL_ICAL_EXPORT int icaltime_day_of_year(const struct icaltimetype t);
  157. /** Return the day of the week of the given time. Sunday is 1 */
  158. LIBICAL_ICAL_EXPORT int icaltime_day_of_week(const struct icaltimetype t);
  159. /** Return the day of the year for the Sunday of the week that the
  160. given time is within. */
  161. LIBICAL_ICAL_EXPORT int icaltime_start_doy_of_week(const struct icaltimetype t);
  162. /** Return the day of the year for the first day of the week that the
  163. given time is within. */
  164. LIBICAL_ICAL_EXPORT int icaltime_start_doy_week(const struct icaltimetype t, int fdow);
  165. /** Return the week number for the week the given time is within */
  166. LIBICAL_ICAL_EXPORT int icaltime_week_number(const struct icaltimetype t);
  167. /** Return true of the time is null. */
  168. LIBICAL_ICAL_EXPORT int icaltime_is_null_time(const struct icaltimetype t);
  169. /** Returns false if the time is clearly invalid, but is not null. This
  170. is usually the result of creating a new time type buy not clearing
  171. it, or setting one of the flags to an illegal value. */
  172. LIBICAL_ICAL_EXPORT int icaltime_is_valid_time(const struct icaltimetype t);
  173. /** @brief Returns true if time is of DATE type, false if DATE-TIME */
  174. LIBICAL_ICAL_EXPORT int icaltime_is_date(const struct icaltimetype t);
  175. /** @brief Returns true if time is relative to UTC zone */
  176. LIBICAL_ICAL_EXPORT int icaltime_is_utc(const struct icaltimetype t);
  177. /** Return -1, 0, or 1 to indicate that a<b, a==b or a>b */
  178. LIBICAL_ICAL_EXPORT int icaltime_compare_with_zone(const struct icaltimetype a,
  179. const struct icaltimetype b);
  180. /** Return -1, 0, or 1 to indicate that a<b, a==b or a>b */
  181. LIBICAL_ICAL_EXPORT int icaltime_compare(const struct icaltimetype a, const struct icaltimetype b);
  182. /** like icaltime_compare, but only use the date parts. */
  183. LIBICAL_ICAL_EXPORT int icaltime_compare_date_only(const struct icaltimetype a,
  184. const struct icaltimetype b);
  185. /** like icaltime_compare, but only use the date parts. */
  186. LIBICAL_ICAL_EXPORT int icaltime_compare_date_only_tz(const struct icaltimetype a,
  187. const struct icaltimetype b,
  188. icaltimezone *tz);
  189. /** Adds or subtracts a number of days, hours, minutes and seconds. */
  190. LIBICAL_ICAL_EXPORT void icaltime_adjust(struct icaltimetype *tt,
  191. const int days, const int hours,
  192. const int minutes, const int seconds);
  193. /** Normalize the icaltime, so that all fields are within the normal range. */
  194. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_normalize(const struct icaltimetype t);
  195. /** convert tt, of timezone tzid, into a utc time. Does nothing if the
  196. time is already UTC. */
  197. LIBICAL_ICAL_EXPORT struct icaltimetype icaltime_convert_to_zone(const struct icaltimetype tt,
  198. icaltimezone *zone);
  199. /** Return the number of days in the given month */
  200. LIBICAL_ICAL_EXPORT int icaltime_days_in_month(const int month, const int year);
  201. /** Return whether you've specified a leapyear or not. */
  202. LIBICAL_ICAL_EXPORT int icaltime_is_leap_year(const int year);
  203. /** Return the number of days in this year */
  204. LIBICAL_ICAL_EXPORT int icaltime_days_in_year(const int year);
  205. /** @brief calculate an icaltimespan given a start and end time. */
  206. LIBICAL_ICAL_EXPORT struct icaltime_span icaltime_span_new(struct icaltimetype dtstart,
  207. struct icaltimetype dtend, int is_busy);
  208. /** @brief Returns true if the two spans overlap **/
  209. LIBICAL_ICAL_EXPORT int icaltime_span_overlaps(icaltime_span *s1, icaltime_span *s2);
  210. /** @brief Returns true if the span is totally within the containing
  211. * span
  212. */
  213. LIBICAL_ICAL_EXPORT int icaltime_span_contains(icaltime_span *s, icaltime_span *container);
  214. #endif /* !ICALTIME_H */