gdate.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // {$include <glib/gquark.h>}
  2. { GDate
  3. Date calculations (not time for now, to be resolved). These are a
  4. mutant combination of Steffen Beyer's DateCalc routines
  5. (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
  6. date routines (written for in-house software). Written by Havoc
  7. Pennington <[email protected]>
  8. }
  9. type
  10. PGTime = ^TGTime;
  11. TGTime = gint32;
  12. PGDateYear = ^TGDateYear;
  13. TGDateYear = guint16;
  14. PGDateDay = ^TGDateDay;
  15. TGDateDay = guint8; // day of the month
  16. { day of the month }
  17. { make struct tm known without having to include time.h }
  18. { from time.h }
  19. Ptm = ^Ttm;
  20. Ttm = record
  21. tm_sec : gint; // Seconds. [0-60] (1 leap second)
  22. tm_min : gint; // Minutes. [0-59]
  23. tm_hour : gint; // Hours. [0-23]
  24. tm_mday : gint; // Day. [1-31]
  25. tm_mon : gint; // Month. [0-11]
  26. tm_year : gint; // Year - 1900.
  27. tm_wday : gint; // Day of week. [0-6]
  28. tm_yday : gint; // Days in year.[0-365]
  29. tm_isdst : gint; // DST. [-1/0/1]
  30. tm_gmtoff: glong; // Seconds east of UTC.
  31. tm_zone : pgchar; // Timezone abbreviation.
  32. end;
  33. { enum used to specify order of appearance in parsed date strings }
  34. type
  35. PGDateDMY = ^TGDateDMY;
  36. TGDateDMY = integer;
  37. const G_DATE_DAY = 0;
  38. G_DATE_MONTH = 1;
  39. G_DATE_YEAR = 2;
  40. { actual week and month values }
  41. type
  42. PGDateWeekday = ^TGDateWeekday;
  43. TGDateWeekday = integer;
  44. const G_DATE_BAD_WEEKDAY = 0;
  45. G_DATE_MONDAY = 1;
  46. G_DATE_TUESDAY = 2;
  47. G_DATE_WEDNESDAY = 3;
  48. G_DATE_THURSDAY = 4;
  49. G_DATE_FRIDAY = 5;
  50. G_DATE_SATURDAY = 6;
  51. G_DATE_SUNDAY = 7;
  52. type
  53. PGDateMonth = ^TGDateMonth;
  54. TGDateMonth = integer;
  55. const
  56. G_DATE_BAD_MONTH = 0;
  57. G_DATE_JANUARY = 1;
  58. G_DATE_FEBRUARY = 2;
  59. G_DATE_MARCH = 3;
  60. G_DATE_APRIL = 4;
  61. G_DATE_MAY = 5;
  62. G_DATE_JUNE = 6;
  63. G_DATE_JULY = 7;
  64. G_DATE_AUGUST = 8;
  65. G_DATE_SEPTEMBER = 9;
  66. G_DATE_OCTOBER = 10;
  67. G_DATE_NOVEMBER = 11;
  68. G_DATE_DECEMBER = 12;
  69. const
  70. G_DATE_BAD_JULIAN = 0;
  71. G_DATE_BAD_DAY = 0;
  72. G_DATE_BAD_YEAR = 0;
  73. { Note: directly manipulating structs is generally a bad idea, but
  74. in this case it's an incredibly bad idea, because all or part
  75. of this struct can be invalid at any given time. Use the functions,
  76. or you will get hosed, I promise.
  77. }
  78. { julian days representation - we use a
  79. bitfield hoping that 64 bit platforms
  80. will pack this whole struct in one big
  81. int
  82. }
  83. { julian is valid }
  84. { dmy is valid }
  85. { DMY representation }
  86. type
  87. PGDate = ^TGDate;
  88. TGDate = record
  89. flag0 : longint;
  90. flag1 : longint;
  91. end;
  92. { g_date_new() returns an invalid date, you then have to _set() stuff
  93. to get a usable anObject. You can also allocate a GDate statically,
  94. then call g_date_clear() to initialize.
  95. }
  96. function g_date_new:PGDate; cdecl;external gliblib name 'g_date_new';
  97. function g_date_new_dmy(day:TGDateDay; month:TGDateMonth; year:TGDateYear):PGDate;cdecl;external gliblib name 'g_date_new_dmy';
  98. function g_date_new_julian(julian_day:guint32):PGDate;cdecl;external gliblib name 'g_date_new_julian';
  99. procedure g_date_free(date:PGDate);cdecl;external gliblib name 'g_date_free';
  100. { check g_date_valid() after doing an operation that might fail, like
  101. _parse. Almost all g_date operations are undefined on invalid
  102. dates (the exceptions are the mutators, since you need those to
  103. return to validity).
  104. }
  105. function g_date_valid (date:PGDate): gboolean;cdecl;external gliblib name 'g_date_valid';
  106. function g_date_valid_month (month:TGDateMonth):gboolean;cdecl;external gliblib name 'g_date_valid_month';
  107. function g_date_valid_year (year:TGDateYear): gboolean;cdecl;external gliblib name 'g_date_valid_year';
  108. function g_date_valid_weekday (weekday:TGDateWeekday): gboolean;cdecl;external gliblib name 'g_date_valid_weekday';
  109. function g_date_valid_julian (julian_date:guint32): gboolean;cdecl;external gliblib name 'g_date_valid_julian';
  110. function g_date_get_weekday(date:PGDate):TGDateWeekday;cdecl;external gliblib name 'g_date_get_weekday';
  111. function g_date_get_month(date:PGDate):TGDateMonth;cdecl;external gliblib name 'g_date_get_month';
  112. function g_date_get_year(date:PGDate):TGDateYear;cdecl;external gliblib name 'g_date_get_year';
  113. function g_date_get_day(date:PGDate):TGDateDay;cdecl;external gliblib name 'g_date_get_day';
  114. function g_date_get_julian(date:PGDate):guint32;cdecl;external gliblib name 'g_date_get_julian';
  115. function g_date_get_day_of_year(date:PGDate):guint;cdecl;external gliblib name 'g_date_get_day_of_year';
  116. { First monday/sunday is the start of week 1; if we haven't reached
  117. that day, return 0. These are not ISO weeks of the year; that
  118. routine needs to be added.
  119. these functions return the number of weeks, starting on the
  120. corrsponding day
  121. }
  122. function g_date_get_monday_week_of_year(date:PGDate):guint;cdecl;external gliblib name 'g_date_get_monday_week_of_year';
  123. function g_date_get_sunday_week_of_year(date:PGDate):guint;cdecl;external gliblib name 'g_date_get_sunday_week_of_year';
  124. { If you create a static date struct you need to clear it to get it
  125. in a sane state before use. You can clear a whole array at
  126. once with the ndates argument.
  127. }
  128. procedure g_date_clear(date:PGDate; n_dates:guint);cdecl;external gliblib name 'g_date_clear';
  129. { The parse routine is meant for dates typed in by a user, so it
  130. permits many formats but tries to catch common typos. If your data
  131. needs to be strictly validated, it is not an appropriate function.
  132. }
  133. procedure g_date_set_parse(date:PGDate; str:Pgchar);cdecl;external gliblib name 'g_date_set_parse';
  134. procedure g_date_set_time(date:PGDate; time:TGTime);cdecl;external gliblib name 'g_date_set_time';
  135. procedure g_date_set_month(date:PGDate; month:TGDateMonth);cdecl;external gliblib name 'g_date_set_month';
  136. procedure g_date_set_day(date:PGDate; day:TGDateDay);cdecl;external gliblib name 'g_date_set_day';
  137. procedure g_date_set_year(date:PGDate; year:TGDateYear);cdecl;external gliblib name 'g_date_set_year';
  138. procedure g_date_set_dmy(date:PGDate; day:TGDateDay; month:TGDateMonth; y:TGDateYear);cdecl;external gliblib name 'g_date_set_dmy';
  139. procedure g_date_set_julian(date:PGDate; julian_date:guint32);cdecl;external gliblib name 'g_date_set_julian';
  140. function g_date_is_first_of_month(date:PGDate):gboolean;cdecl;external gliblib name 'g_date_is_first_of_month';
  141. function g_date_is_last_of_month(date:PGDate):gboolean;cdecl;external gliblib name 'g_date_is_last_of_month';
  142. { To go forward by some number of weeks just go forward weeks 7 days }
  143. procedure g_date_add_days(date:PGDate; n_days:guint);cdecl;external gliblib name 'g_date_add_days';
  144. procedure g_date_subtract_days(date:PGDate; n_days:guint);cdecl;external gliblib name 'g_date_subtract_days';
  145. { If you add/sub months while day > 28, the day might change }
  146. procedure g_date_add_months(date:PGDate; n_months:guint);cdecl;external gliblib name 'g_date_add_months';
  147. procedure g_date_subtract_months(date:PGDate; n_months:guint);cdecl;external gliblib name 'g_date_subtract_months';
  148. { If it's feb 29, changing years can move you to the 28th }
  149. procedure g_date_add_years(date:PGDate; n_years:guint);cdecl;external gliblib name 'g_date_add_years';
  150. procedure g_date_subtract_years(date:PGDate; n_years:guint);cdecl;external gliblib name 'g_date_subtract_years';
  151. function g_date_is_leap_year (year:TGDateYear): gboolean; cdecl;external gliblib name 'g_date_is_leap_year';
  152. function g_date_get_days_in_month (month: TGDateMonth; year: TGDateYear):guint8;cdecl;external gliblib name 'g_date_get_days_in_month';
  153. function g_date_get_monday_weeks_in_year (year:TGDateYear): guint8; cdecl;external gliblib name 'g_date_get_monday_weeks_in_year';
  154. function g_date_get_sunday_weeks_in_year (year:TGDateYear): guint8; cdecl;external gliblib name 'g_date_get_sunday_weeks_in_year';
  155. { Returns the number of days between the two dates. If date2 comes
  156. before date1, a negative value is return. }
  157. function g_date_days_between(date1:PGDate; date2:PGDate):gint;cdecl;external gliblib name 'g_date_days_between';
  158. { qsort-friendly (with a cast...) }
  159. function g_date_compare(lhs:PGDate; rhs:PGDate):gint;cdecl;external gliblib name 'g_date_compare';
  160. procedure g_date_to_struct_tm(date:PGDate; tm:Ptm);cdecl;external gliblib name 'g_date_to_struct_tm';
  161. procedure g_date_clamp(date:PGDate; min_date:PGDate; max_date:PGDate);cdecl;external gliblib name 'g_date_clamp';
  162. { Swap date1 and date2's values if date1 > date2. }
  163. procedure g_date_order(date1:PGDate; date2:PGDate);cdecl;external gliblib name 'g_date_order';
  164. { Just like strftime() except you can only use date-related formats.
  165. Using a time format is undefined.
  166. }
  167. function g_date_strftime(s:Pgchar; slen:gsize; format:Pgchar; date:PGDate):gsize;cdecl;external gliblib name 'g_date_strftime';
  168. { DEPRECATED functions are maped to their newer versions as the arguments and the return value are the same}
  169. { DEPRECATED functions are disabled because a
  170. name conflict with G_DATE_MONTH, G_DATE_YEAR and G_DATE_DAY
  171. function g_date_weekday(date:PGDate):TGDateWeekday;cdecl;external gliblib name 'g_date_get_weekday';
  172. function g_date_month(date:PGDate):TGDateMonth;cdecl;external gliblib name 'g_date_get_month';
  173. function g_date_year(date:PGDate):TGDateYear;cdecl;external gliblib name 'g_date_get_year';
  174. function g_date_day(date:PGDate):TGDateDay;cdecl;external gliblib name 'g_date_get_day';
  175. function g_date_julian(date:PGDate):guint32;cdecl;external gliblib name 'g_date_get_julian';
  176. function g_date_day_of_year(date:PGDate):guint;cdecl;external gliblib name 'g_date_get_day_of_year';
  177. function g_date_monday_week_of_year(date:PGDate):guint;cdecl;external gliblib name 'g_date_get_monday_week_of_year';
  178. function g_date_sunday_week_of_year(date:PGDate):guint;cdecl;external gliblib name 'g_date_get_sunday_week_of_year';
  179. function g_date_days_in_month (month: TGDateMonth; year: TGDateYear):guint8;cdecl;external gliblib name 'g_date_get_days_in_month';
  180. function g_date_monday_weeks_in_year (year:TGDateYear): guint8; cdecl;external gliblib name 'g_date_get_monday_weeks_in_year';
  181. function g_date_sunday_weeks_in_year (year:TGDateYear): guint8; cdecl;external gliblib name 'g_date_get_sunday_weeks_in_year';
  182. }