gdate.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_DATE_H__
  24. #define __G_DATE_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <time.h>
  29. #include <glib/gtypes.h>
  30. #include <glib/gquark.h>
  31. G_BEGIN_DECLS
  32. /* GDate
  33. *
  34. * Date calculations (not time for now, to be resolved). These are a
  35. * mutant combination of Steffen Beyer's DateCalc routines
  36. * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
  37. * date routines (written for in-house software). Written by Havoc
  38. * Pennington <hp@pobox.com>
  39. */
  40. typedef gint32 GTime;
  41. typedef guint16 GDateYear;
  42. typedef guint8 GDateDay; /* day of the month */
  43. typedef struct _GDate GDate;
  44. /* enum used to specify order of appearance in parsed date strings */
  45. typedef enum
  46. {
  47. G_DATE_DAY = 0,
  48. G_DATE_MONTH = 1,
  49. G_DATE_YEAR = 2
  50. } GDateDMY;
  51. /* actual week and month values */
  52. typedef enum
  53. {
  54. G_DATE_BAD_WEEKDAY = 0,
  55. G_DATE_MONDAY = 1,
  56. G_DATE_TUESDAY = 2,
  57. G_DATE_WEDNESDAY = 3,
  58. G_DATE_THURSDAY = 4,
  59. G_DATE_FRIDAY = 5,
  60. G_DATE_SATURDAY = 6,
  61. G_DATE_SUNDAY = 7
  62. } GDateWeekday;
  63. typedef enum
  64. {
  65. G_DATE_BAD_MONTH = 0,
  66. G_DATE_JANUARY = 1,
  67. G_DATE_FEBRUARY = 2,
  68. G_DATE_MARCH = 3,
  69. G_DATE_APRIL = 4,
  70. G_DATE_MAY = 5,
  71. G_DATE_JUNE = 6,
  72. G_DATE_JULY = 7,
  73. G_DATE_AUGUST = 8,
  74. G_DATE_SEPTEMBER = 9,
  75. G_DATE_OCTOBER = 10,
  76. G_DATE_NOVEMBER = 11,
  77. G_DATE_DECEMBER = 12
  78. } GDateMonth;
  79. #define G_DATE_BAD_JULIAN 0U
  80. #define G_DATE_BAD_DAY 0U
  81. #define G_DATE_BAD_YEAR 0U
  82. /* Note: directly manipulating structs is generally a bad idea, but
  83. * in this case it's an *incredibly* bad idea, because all or part
  84. * of this struct can be invalid at any given time. Use the functions,
  85. * or you will get hosed, I promise.
  86. */
  87. struct _GDate
  88. {
  89. guint julian_days : 32; /* julian days representation - we use a
  90. * bitfield hoping that 64 bit platforms
  91. * will pack this whole struct in one big
  92. * int
  93. */
  94. guint julian : 1; /* julian is valid */
  95. guint dmy : 1; /* dmy is valid */
  96. /* DMY representation */
  97. guint day : 6;
  98. guint month : 4;
  99. guint year : 16;
  100. };
  101. /* g_date_new() returns an invalid date, you then have to _set() stuff
  102. * to get a usable object. You can also allocate a GDate statically,
  103. * then call g_date_clear() to initialize.
  104. */
  105. GLIB_AVAILABLE_IN_ALL
  106. GDate* g_date_new (void);
  107. GLIB_AVAILABLE_IN_ALL
  108. GDate* g_date_new_dmy (GDateDay day,
  109. GDateMonth month,
  110. GDateYear year);
  111. GLIB_AVAILABLE_IN_ALL
  112. GDate* g_date_new_julian (guint32 julian_day);
  113. GLIB_AVAILABLE_IN_ALL
  114. void g_date_free (GDate *date);
  115. /* check g_date_valid() after doing an operation that might fail, like
  116. * _parse. Almost all g_date operations are undefined on invalid
  117. * dates (the exceptions are the mutators, since you need those to
  118. * return to validity).
  119. */
  120. GLIB_AVAILABLE_IN_ALL
  121. gboolean g_date_valid (const GDate *date);
  122. GLIB_AVAILABLE_IN_ALL
  123. gboolean g_date_valid_day (GDateDay day) G_GNUC_CONST;
  124. GLIB_AVAILABLE_IN_ALL
  125. gboolean g_date_valid_month (GDateMonth month) G_GNUC_CONST;
  126. GLIB_AVAILABLE_IN_ALL
  127. gboolean g_date_valid_year (GDateYear year) G_GNUC_CONST;
  128. GLIB_AVAILABLE_IN_ALL
  129. gboolean g_date_valid_weekday (GDateWeekday weekday) G_GNUC_CONST;
  130. GLIB_AVAILABLE_IN_ALL
  131. gboolean g_date_valid_julian (guint32 julian_date) G_GNUC_CONST;
  132. GLIB_AVAILABLE_IN_ALL
  133. gboolean g_date_valid_dmy (GDateDay day,
  134. GDateMonth month,
  135. GDateYear year) G_GNUC_CONST;
  136. GLIB_AVAILABLE_IN_ALL
  137. GDateWeekday g_date_get_weekday (const GDate *date);
  138. GLIB_AVAILABLE_IN_ALL
  139. GDateMonth g_date_get_month (const GDate *date);
  140. GLIB_AVAILABLE_IN_ALL
  141. GDateYear g_date_get_year (const GDate *date);
  142. GLIB_AVAILABLE_IN_ALL
  143. GDateDay g_date_get_day (const GDate *date);
  144. GLIB_AVAILABLE_IN_ALL
  145. guint32 g_date_get_julian (const GDate *date);
  146. GLIB_AVAILABLE_IN_ALL
  147. guint g_date_get_day_of_year (const GDate *date);
  148. /* First monday/sunday is the start of week 1; if we haven't reached
  149. * that day, return 0. These are not ISO weeks of the year; that
  150. * routine needs to be added.
  151. * these functions return the number of weeks, starting on the
  152. * corrsponding day
  153. */
  154. GLIB_AVAILABLE_IN_ALL
  155. guint g_date_get_monday_week_of_year (const GDate *date);
  156. GLIB_AVAILABLE_IN_ALL
  157. guint g_date_get_sunday_week_of_year (const GDate *date);
  158. GLIB_AVAILABLE_IN_ALL
  159. guint g_date_get_iso8601_week_of_year (const GDate *date);
  160. /* If you create a static date struct you need to clear it to get it
  161. * in a sane state before use. You can clear a whole array at
  162. * once with the ndates argument.
  163. */
  164. GLIB_AVAILABLE_IN_ALL
  165. void g_date_clear (GDate *date,
  166. guint n_dates);
  167. /* The parse routine is meant for dates typed in by a user, so it
  168. * permits many formats but tries to catch common typos. If your data
  169. * needs to be strictly validated, it is not an appropriate function.
  170. */
  171. GLIB_AVAILABLE_IN_ALL
  172. void g_date_set_parse (GDate *date,
  173. const gchar *str);
  174. GLIB_AVAILABLE_IN_ALL
  175. void g_date_set_time_t (GDate *date,
  176. time_t timet);
  177. GLIB_AVAILABLE_IN_ALL
  178. void g_date_set_time_val (GDate *date,
  179. GTimeVal *timeval);
  180. #ifndef G_DISABLE_DEPRECATED
  181. GLIB_DEPRECATED_FOR(g_date_set_time_t)
  182. void g_date_set_time (GDate *date,
  183. GTime time_);
  184. #endif
  185. GLIB_AVAILABLE_IN_ALL
  186. void g_date_set_month (GDate *date,
  187. GDateMonth month);
  188. GLIB_AVAILABLE_IN_ALL
  189. void g_date_set_day (GDate *date,
  190. GDateDay day);
  191. GLIB_AVAILABLE_IN_ALL
  192. void g_date_set_year (GDate *date,
  193. GDateYear year);
  194. GLIB_AVAILABLE_IN_ALL
  195. void g_date_set_dmy (GDate *date,
  196. GDateDay day,
  197. GDateMonth month,
  198. GDateYear y);
  199. GLIB_AVAILABLE_IN_ALL
  200. void g_date_set_julian (GDate *date,
  201. guint32 julian_date);
  202. GLIB_AVAILABLE_IN_ALL
  203. gboolean g_date_is_first_of_month (const GDate *date);
  204. GLIB_AVAILABLE_IN_ALL
  205. gboolean g_date_is_last_of_month (const GDate *date);
  206. /* To go forward by some number of weeks just go forward weeks*7 days */
  207. GLIB_AVAILABLE_IN_ALL
  208. void g_date_add_days (GDate *date,
  209. guint n_days);
  210. GLIB_AVAILABLE_IN_ALL
  211. void g_date_subtract_days (GDate *date,
  212. guint n_days);
  213. /* If you add/sub months while day > 28, the day might change */
  214. GLIB_AVAILABLE_IN_ALL
  215. void g_date_add_months (GDate *date,
  216. guint n_months);
  217. GLIB_AVAILABLE_IN_ALL
  218. void g_date_subtract_months (GDate *date,
  219. guint n_months);
  220. /* If it's feb 29, changing years can move you to the 28th */
  221. GLIB_AVAILABLE_IN_ALL
  222. void g_date_add_years (GDate *date,
  223. guint n_years);
  224. GLIB_AVAILABLE_IN_ALL
  225. void g_date_subtract_years (GDate *date,
  226. guint n_years);
  227. GLIB_AVAILABLE_IN_ALL
  228. gboolean g_date_is_leap_year (GDateYear year) G_GNUC_CONST;
  229. GLIB_AVAILABLE_IN_ALL
  230. guint8 g_date_get_days_in_month (GDateMonth month,
  231. GDateYear year) G_GNUC_CONST;
  232. GLIB_AVAILABLE_IN_ALL
  233. guint8 g_date_get_monday_weeks_in_year (GDateYear year) G_GNUC_CONST;
  234. GLIB_AVAILABLE_IN_ALL
  235. guint8 g_date_get_sunday_weeks_in_year (GDateYear year) G_GNUC_CONST;
  236. /* Returns the number of days between the two dates. If date2 comes
  237. before date1, a negative value is return. */
  238. GLIB_AVAILABLE_IN_ALL
  239. gint g_date_days_between (const GDate *date1,
  240. const GDate *date2);
  241. /* qsort-friendly (with a cast...) */
  242. GLIB_AVAILABLE_IN_ALL
  243. gint g_date_compare (const GDate *lhs,
  244. const GDate *rhs);
  245. GLIB_AVAILABLE_IN_ALL
  246. void g_date_to_struct_tm (const GDate *date,
  247. struct tm *tm);
  248. GLIB_AVAILABLE_IN_ALL
  249. void g_date_clamp (GDate *date,
  250. const GDate *min_date,
  251. const GDate *max_date);
  252. /* Swap date1 and date2's values if date1 > date2. */
  253. GLIB_AVAILABLE_IN_ALL
  254. void g_date_order (GDate *date1, GDate *date2);
  255. /* Just like strftime() except you can only use date-related formats.
  256. * Using a time format is undefined.
  257. */
  258. GLIB_AVAILABLE_IN_ALL
  259. gsize g_date_strftime (gchar *s,
  260. gsize slen,
  261. const gchar *format,
  262. const GDate *date);
  263. #ifndef G_DISABLE_DEPRECATED
  264. #define g_date_weekday g_date_get_weekday
  265. #define g_date_month g_date_get_month
  266. #define g_date_year g_date_get_year
  267. #define g_date_day g_date_get_day
  268. #define g_date_julian g_date_get_julian
  269. #define g_date_day_of_year g_date_get_day_of_year
  270. #define g_date_monday_week_of_year g_date_get_monday_week_of_year
  271. #define g_date_sunday_week_of_year g_date_get_sunday_week_of_year
  272. #define g_date_days_in_month g_date_get_days_in_month
  273. #define g_date_monday_weeks_in_year g_date_get_monday_weeks_in_year
  274. #define g_date_sunday_weeks_in_year g_date_get_sunday_weeks_in_year
  275. #endif /* G_DISABLE_DEPRECATED */
  276. G_END_DECLS
  277. #endif /* __G_DATE_H__ */