You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

264 regels
10 KiB

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