gdate.h 12 KB

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