/src/wrappers/glib/library/utilities/g_date.e

http://github.com/tybor/Liberty · Specman e · 1093 lines · 494 code · 219 blank · 380 comment · 2 complexity · e789bf6a783e10a5c497a54f9f60a017 MD5 · raw file

  1. indexing
  2. description: " Date - calendrical calculations."
  3. copyright: "(C) 2006 Paolo Redaelli "
  4. license: "LGPL v2 or later"
  5. date: "$Date:$"
  6. revision: "$Revision:$"
  7. wrapping_details: "When a call can leave the GDate into an incosistent state (`invalid date') no preconditions (requires) have been written"
  8. -- Todo: Implement G_DATES that holds all the constants
  9. -- (weekdays, month names etc)
  10. class G_DATE
  11. -- The GDate data structure represents a day between January 1,
  12. -- Year 1, and sometime a few thousand years in the future (right
  13. -- now it will go to the year 65535 or so, but g_date_set_parse()
  14. -- only parses up to the year 8000 or so - just count on "a few
  15. -- thousand"). GDate is meant to represent everyday dates, not
  16. -- astronomical dates or historical dates or ISO timestamps or the
  17. -- like. It extrapolates the current Gregorian calendar forward and
  18. -- backward in time; there is no attempt to change the calendar to
  19. -- match time periods or locations. GDate does not store time
  20. -- information; it represents a day.
  21. -- The GDate implementation has several nice features; it is only a
  22. -- 64-bit struct, so storing large numbers of dates is very
  23. -- efficient. It can keep both a Julian and day-month-year
  24. -- representation of the date, since some calculations are much
  25. -- easier with one representation or the other. A Julian
  26. -- representation is simply a count of days since some fixed day in
  27. -- the past; for GDate the fixed day is January 1, 1 AD. ("Julian"
  28. -- dates in the GDate API aren't really Julian dates in the
  29. -- technical sense; technically, Julian dates count from the start
  30. -- of the Julian period, Jan 1, 4713 BC).
  31. -- GDate is simple to use. First you need a "blank" date; you can
  32. -- get a dynamically allocated date from g_date_new(), or you can
  33. -- declare an automatic variable or array and initialize it to a
  34. -- sane state by calling g_date_clear(). A cleared date is sane;
  35. -- it's safe to call g_date_set_dmy() and the other mutator
  36. -- functions to initialize the value of a cleared date. However, a
  37. -- cleared date is initially invalid, meaning that it doesn't
  38. -- represent a day that exists. It is undefined to call any of the
  39. -- date calculation routines on an invalid date. If you obtain a
  40. -- date from a user or other unpredictable source, you should check
  41. -- its validity with the g_date_valid() predicate. g_date_valid()
  42. -- is also used to check for errors with g_date_set_parse() and
  43. -- other functions that can fail. Dates can be invalidated by
  44. -- calling g_date_clear() again.
  45. -- It is very important to use the API to access the GDate
  46. -- struct. Often only the day-month-year or only the Julian
  47. -- representation is valid. Sometimes neither is valid. Use the
  48. -- API.
  49. -- GLib doesn't contain any time-manipulation functions; however,
  50. -- there is a GTime typedef and a GTimeVal struct which represents
  51. -- a more precise time (with microseconds). You can request the
  52. -- current time as a GTimeVal with g_get_current_time().
  53. inherit
  54. COMPARABLE_C_STRUCT
  55. undefine from_external_pointer
  56. redefine compare, free
  57. end
  58. EIFFEL_OWNED
  59. insert GDATE_STRUCT
  60. creation make_dmy, from_tuple, make
  61. creation {WRAPPER} from_external_pointer
  62. feature {} -- Creation
  63. make is
  64. -- Create a GDate and initializes it to a sane state. The new
  65. -- date will be cleared (`clear') but invalid (it won't
  66. -- represent an existing day).
  67. do
  68. from_external_pointer(g_date_new)
  69. end
  70. make_dmy (a_day: INTEGER_8; a_month: INTEGER; an_year: INTEGER_16) is
  71. -- Create and initialize a new GDate; it sets the value of
  72. -- the date. Assuming the day-month-year triplet you pass in
  73. -- represents an existing day, the returned date will be
  74. -- valid.
  75. do
  76. from_external_pointer(g_date_new_dmy (a_day, a_month, an_year))
  77. end
  78. from_tuple (a_tuple: TUPLE[INTEGER_8,INTEGER_8,INTEGER_16]) is
  79. -- Create and initialize a new GDate, assuming the
  80. -- day-month-year triplet you pass in represents an existing
  81. -- day, the returned date will be valid.
  82. do
  83. from_external_pointer(g_date_new_dmy (a_tuple.item_1, a_tuple.item_2, a_tuple.item_3))
  84. end
  85. make_julian (a_julian_day: INTEGER_32) is
  86. -- Create, initialize and sets the value of a new
  87. -- GDate. Assuming the Julian day number you pass in is valid
  88. -- (greater than 0, less than an unreasonably large number),
  89. -- the returned date will be valid.
  90. -- `a_julian_day': days since January 1, Year 1.
  91. do
  92. from_external_pointer(g_date_new_julian (a_julian_day))
  93. end
  94. feature
  95. clear is
  96. -- Initializes Current to a sane but invalid state. The
  97. -- cleared dates will not represent an existing date, but
  98. -- will not contain garbage.
  99. do
  100. g_date_clear (handle,1)
  101. end
  102. feature -- Setters
  103. set_day (a_day: INTEGER_8) is
  104. -- Sets the day of the month for a GDate. If the resulting
  105. -- day-month-year triplet is invalid, the date will be
  106. -- invalid.
  107. do
  108. g_date_set_day (handle, a_day)
  109. end
  110. set_month (a_month: INTEGER) is
  111. -- Sets the month of the year for a GDate. If the resulting
  112. -- day-month-year triplet is invalid, the date will be
  113. -- invalid.
  114. do
  115. g_date_set_month (handle, a_month)
  116. end
  117. set_year (an_year: INTEGER_16) is
  118. -- Sets the year for a GDate. If the resulting day-month-year
  119. -- triplet is invalid, the date will be invalid.
  120. do
  121. g_date_set_year (handle, an_year)
  122. end
  123. set_dmy (a_day, a_month, an_year: INTEGER) is
  124. -- Sets the value of a GDate from a day, month, and year. The
  125. -- day-month-year triplet must be valid; if you aren't sure
  126. -- it is, call `is_valid_dmy' to check before you set it.
  127. do
  128. g_date_set_dmy (handle, a_day.to_integer_8, a_month, an_year.to_integer_16)
  129. end
  130. set_julian (a_julian_date: INTEGER_32) is
  131. -- Sets the value of a GDate from a Julian day number.
  132. -- `a_julian_date' : Julian day number (days since January 1,
  133. -- Year 1).
  134. require positive_date: a_julian_date > 0
  135. do
  136. g_date_set_julian (handle, a_julian_date)
  137. end
  138. -- TODO: wrap void g_date_set_time (GDate *date,
  139. -- GTime time_);
  140. -- Sets the value of a date from a GTime (time_t) value. To set the value
  141. -- of a date to the current day, you could write:
  142. -- g_date_set_time (date, time (NULL));
  143. -- date : a GDate.
  144. -- time_ : GTime value to set.
  145. -- -------------------------------------------------------------------
  146. parse (a_string: STRING) is
  147. -- Parses user-inputted `a_string', and try to figure out
  148. -- what date it represents, taking the current locale into
  149. -- account. If the string is successfully parsed, the date
  150. -- will be valid after the call. Otherwise, it will be
  151. -- invalid. You should check using `is_valid' to see whether
  152. -- the parsing succeeded.
  153. -- This function is not appropriate for file formats and the
  154. -- like; it isn't very precise, and its exact behavior varies
  155. -- with the locale. It's intended to be a heuristic routine
  156. -- that guesses what the user means by a given string (and it
  157. -- does work pretty well in that capacity).
  158. require valid_string: a_string /= Void
  159. do
  160. g_date_set_parse (handle, a_string.to_external)
  161. end
  162. feature -- Getters
  163. day: INTEGER is
  164. -- the day of the month. The date must be valid.
  165. require valid: is_valid
  166. do
  167. Result := g_date_get_day (handle)
  168. end
  169. month: INTEGER is
  170. -- the month of the year. The date must be valid.
  171. require valid: is_valid
  172. do
  173. Result := g_date_get_month (handle)
  174. end
  175. year: INTEGER is
  176. -- the year of Current. The date must be valid.
  177. require valid: is_valid
  178. do
  179. Result := g_date_get_year (handle)
  180. end
  181. julian: INTEGER_32 is
  182. -- the Julian day or "serial number" of the GDate. The Julian
  183. -- day is simply the number of days since January 1, Year 1;
  184. -- i.e., January 1, Year 1 is Julian day 1; January 2, Year 1
  185. -- is Julian day 2, etc. The date must be valid.
  186. require valid: is_valid
  187. do
  188. Result := g_date_get_julian (handle)
  189. end
  190. weekday: INTEGER is
  191. -- the day of the week for a GDate. The date must be valid.
  192. require valid: is_valid
  193. do
  194. Result := g_date_get_weekday (handle)
  195. end
  196. day_of_year: INTEGER is
  197. -- the day of the year, where Jan 1 is the first day of the
  198. -- year. Shall be a NATURAL
  199. require valid: is_valid
  200. do
  201. Result := g_date_get_day_of_year (handle)
  202. end
  203. days_in_month: INTEGER_8 is
  204. -- The days in the month of Current G_DATE. See also
  205. -- G_DATES.days_in_month
  206. require valid: is_valid
  207. do
  208. Result := g_date_get_days_in_month (month, year.to_integer_16).to_integer_8
  209. end
  210. -- TODO: put this into G_DATES g_date_get_days_in_month ()
  211. -- guint8 g_date_get_days_in_month (GDateMonth month,
  212. -- GDateYear year);
  213. -- Returns the number of days in a month, taking leap years into account.
  214. -- month : month.
  215. -- year : year.
  216. -- Returns : number of days in month during the year.
  217. -- -------------------------------------------------------------------
  218. feature -- Date arithmetics
  219. infix "+", add_days (some_days: INTEGER) is
  220. -- Increments a date some number of days. To move forward by
  221. -- weeks, add weeks*7 days. The date must be valid.
  222. -- some_days : number of days to move the date forward.
  223. require
  224. positive: some_days>0 -- some_days shall be NATURAL
  225. valid_date: is_valid
  226. do
  227. g_date_add_days (handle, some_days)
  228. end
  229. subtract_days (some_days: INTEGER) is
  230. -- Moves a date some number of days into the past. To move by
  231. -- weeks, just move by weeks*7 days. The date must be valid.
  232. require
  233. positive: some_days>0 -- some_days shall be NATURAL
  234. valid_date: is_valid
  235. do
  236. g_date_subtract_days (handle, some_days)
  237. end
  238. add_months (some_months: INTEGER) is
  239. -- Increments a date by `some_months' number of months. If
  240. -- the day of the month is greater than 28, this routine may
  241. -- change the day of the month (because the destination month
  242. -- may not have the current day in it). The date must be
  243. -- valid.
  244. require
  245. valid_date: is_valid
  246. positive_months: some_months>0
  247. do
  248. g_date_add_months (handle, some_months)
  249. end
  250. subtract_months (some_months: INTEGER) is
  251. -- Moves a date some number of months into the past. If the
  252. -- current day of the month doesn't exist in the destination
  253. -- month, the day of the month may change. The date must be
  254. -- valid.
  255. require
  256. valid_date: is_valid
  257. positive_months: some_months>0
  258. do
  259. g_date_subtract_months (handle, some_months)
  260. end
  261. add_years (some_years: INTEGER) is
  262. -- Increments a date by some number of years. If the date is
  263. -- February 29, and the destination year is not a leap year,
  264. -- the date will be changed to February 28. The date must be
  265. -- valid.
  266. require
  267. valid_date: is_valid
  268. positive_years: some_years>0
  269. do
  270. g_date_add_years (handle, some_years)
  271. end
  272. subtract_years (some_years: INTEGER) is
  273. -- Moves a date some number of years into the past. If the
  274. -- current day doesn't exist in the destination year
  275. -- (i.e. it's February 29 and you move to a non-leap-year)
  276. -- then the day is changed to February 29. The date must be
  277. -- valid.
  278. do
  279. g_date_subtract_years (handle, some_years)
  280. end
  281. infix "-", days_between (another: like Current): INTEGER is
  282. -- Computes the number of days between two dates. If date2 is
  283. -- prior to date1, the returned value is negative. Both dates
  284. -- must be valid.
  285. require
  286. valid_date: is_valid
  287. valid_another_date: another /= Void and then another.is_valid
  288. do
  289. Result := g_date_days_between (handle, another.handle)
  290. end
  291. is_equal (another: like Current): BOOLEAN is
  292. do
  293. Result:=(compare(another)=0)
  294. end
  295. infix "<"(another: like Current): BOOLEAN is
  296. do
  297. Result:=(compare(another)<0)
  298. end
  299. compare (another: like Current): INTEGER is
  300. -- qsort()-style comparsion function for dates; i.e.: 0 for
  301. -- equal, less than zero if Current is less than another,
  302. -- greater than zero if Current is greater than another.
  303. -- Both dates must be valid.
  304. require else
  305. valid_date: is_valid
  306. valid_another_date: another /= Void and then another.is_valid
  307. do
  308. Result := g_date_compare (handle, another.handle)
  309. end
  310. clamp (a_min, a_max: like Current) is
  311. -- If date is prior to min_date, sets date equal to min_date. If date
  312. -- falls after max_date, sets date equal to max_date. Either min_date and
  313. -- max_date may be NULL. All non-NULL dates must be valid.
  314. -- date : a GDate to clamp.
  315. -- min_date : minimum accepted value for date.
  316. -- max_date : maximum accepted value for date.
  317. require
  318. valid_min: a_min/=Void implies a_min.is_valid
  319. valid_max: a_max/=Void implies a_max.is_valid
  320. valid_dates: (a_min=Void implies a_max/=Void) and
  321. (a_max=Void implies a_min/=Void)
  322. local min_ptr, max_ptr: POINTER
  323. do
  324. if a_min/=Void then min_ptr := a_min.handle end
  325. if a_max/=Void then max_ptr := a_max.handle end
  326. g_date_clamp (handle, min_ptr, max_ptr)
  327. end
  328. order (another: like Current) is
  329. -- Checks if Current is less than or equal to `another', and swap the values if
  330. -- this is not the case.
  331. require valid_other: another /= Void
  332. do
  333. g_date_order (handle, another.handle)
  334. end
  335. feature -- Queries
  336. is_first_of_month: BOOLEAN is
  337. -- Is the date on the first of a month?
  338. require valid: is_valid
  339. do
  340. Result:= g_date_is_first_of_month(handle).to_boolean
  341. end
  342. is_last_of_month: BOOLEAN is
  343. -- Is the date the last day of the month?
  344. require valid: is_valid
  345. do
  346. Result := g_date_is_last_of_month (handle).to_boolean
  347. end
  348. -- g_date_is_leap_year ()
  349. -- gboolean g_date_is_leap_year (GDateYear year);
  350. -- Returns TRUE if the year is a leap year.
  351. -- year : year to check.
  352. -- Returns : TRUE if the year is a leap year.
  353. -- -------------------------------------------------------------------
  354. -- g_date_get_monday_week_of_year ()
  355. -- guint g_date_get_monday_week_of_year (const GDate *date);
  356. -- Returns the week of the year, where weeks are understood to start on
  357. -- Monday. If the date is before the first Monday of the year, return 0.
  358. -- The date must be valid.
  359. -- date : a GDate.
  360. -- Returns : week of the year.
  361. -- -------------------------------------------------------------------
  362. -- g_date_get_monday_weeks_in_year ()
  363. -- guint8 g_date_get_monday_weeks_in_year (GDateYear year);
  364. -- Returns the number of weeks in the year, where weeks are taken to start
  365. -- on Monday. Will be 52 or 53. The date must be valid. (Years always have
  366. -- 52 7-day periods, plus 1 or 2 extra days depending on whether it's a
  367. -- leap year. This function is basically telling you how many Mondays are
  368. -- in the year, i.e. there are 53 Mondays if one of the extra days happens
  369. -- to be a Monday.)
  370. -- year : a year.
  371. -- Returns : number of Mondays in the year.
  372. -- -------------------------------------------------------------------
  373. -- g_date_get_sunday_week_of_year ()
  374. -- guint g_date_get_sunday_week_of_year (const GDate *date);
  375. -- Returns the week of the year during which this date falls, if weeks are
  376. -- understood to being on Sunday. The date must be valid. Can return 0 if
  377. -- the day is before the first Sunday of the year.
  378. -- date : a GDate.
  379. -- Returns : week number.
  380. -- -------------------------------------------------------------------
  381. -- g_date_get_sunday_weeks_in_year ()
  382. -- guint8 g_date_get_sunday_weeks_in_year (GDateYear year);
  383. -- Returns the number of weeks in the year, where weeks are taken to start
  384. -- on Sunday. Will be 52 or 53. The date must be valid. (Years always have
  385. -- 52 7-day periods, plus 1 or 2 extra days depending on whether it's a
  386. -- leap year. This function is basically telling you how many Sundays are
  387. -- in the year, i.e. there are 53 Sundays if one of the extra days happens
  388. -- to be a Sunday.)
  389. -- year : year to count weeks in.
  390. -- Returns : number of weeks.
  391. -- -------------------------------------------------------------------
  392. -- g_date_get_iso8601_week_of_year ()
  393. -- guint g_date_get_iso8601_week_of_year (const GDate *date);
  394. -- Returns the week of the year, where weeks are interpreted according to
  395. -- ISO 8601.
  396. -- date : a valid GDate
  397. -- Returns : ISO 8601 week number of the year.
  398. -- Since 2.6
  399. -- -------------------------------------------------------------------
  400. to_string: STRING is
  401. require is_valid
  402. local written_characters: INTEGER -- TODO: shall be NATURAL
  403. do
  404. create Result.make_filled(' ',11)
  405. written_characters:= (g_date_strftime
  406. (Result.to_external, Result.count,
  407. to_string_format.to_external, handle))
  408. check buffer_big_enought: written_characters/=0 end
  409. -- debug print("G_DATE.to_string after trimming is ")
  410. -- print(Result.count.out) print(" characters long%N") end
  411. end
  412. -- g_date_strftime ()
  413. -- gsize g_date_strftime (gchar *s,
  414. -- gsize slen,
  415. -- const gchar *format,
  416. -- const GDate *date);
  417. -- Generates a printed representation of the date, in a locale-specific
  418. -- way. Works just like the standard C strftime() function, but only
  419. -- accepts date-related formats; time-related formats give undefined
  420. -- results. Date must be valid.
  421. -- s : destination buffer.
  422. -- slen : buffer size.
  423. -- format : format string.
  424. -- date : valid GDate.
  425. -- Returns : number of characters written to the buffer, or 0 the buffer
  426. -- was too small.
  427. -- -------------------------------------------------------------------
  428. -- g_date_to_struct_tm ()
  429. -- void g_date_to_struct_tm (const GDate *date,
  430. -- struct tm *tm);
  431. -- Fills in the date-related bits of a struct tm using the date value.
  432. -- Initializes the non-date parts with something sane but meaningless.
  433. -- date : a GDate to set the struct tm from.
  434. -- tm : struct tm to fill.
  435. -- -------------------------------------------------------------------
  436. is_valid: BOOLEAN is
  437. -- Does the GDate represents an existing day?
  438. -- Note: For Eiffel user those should always be true "The date must not
  439. -- contain garbage; it should have been initialized with `clear' if
  440. -- it wasn't allocated by one of the `make' variants."
  441. -- TODO: check the above note using an invariant.
  442. do
  443. Result := g_date_valid (handle).to_boolean
  444. end
  445. -- -------------------------------------------------------------------
  446. -- g_date_valid_day ()
  447. -- gboolean g_date_valid_day (GDateDay day);
  448. -- Returns TRUE if the day of the month is valid (a day is valid if it's
  449. -- between 1 and 31 inclusive).
  450. -- day : day to check.
  451. -- Returns : TRUE if the day is valid.
  452. -- -------------------------------------------------------------------
  453. -- g_date_valid_month ()
  454. -- gboolean g_date_valid_month (GDateMonth month);
  455. -- Returns TRUE if the month value is valid. The 12 GDateMonth enumeration
  456. -- values are the only valid months.
  457. -- month : month.
  458. -- Returns : TRUE if the month is valid.
  459. -- -------------------------------------------------------------------
  460. -- g_date_valid_year ()
  461. -- gboolean g_date_valid_year (GDateYear year);
  462. -- Returns TRUE if the year is valid. Any year greater than 0 is valid,
  463. -- though there is a 16-bit limit to what GDate will understand.
  464. -- year : year.
  465. -- Returns : TRUE if the year is valid.
  466. -- -------------------------------------------------------------------
  467. -- g_date_valid_dmy ()
  468. -- gboolean g_date_valid_dmy (GDateDay day,
  469. -- GDateMonth month,
  470. -- GDateYear year);
  471. -- Returns TRUE if the day-month-year triplet forms a valid, existing day
  472. -- in the range of days GDate understands (Year 1 or later, no more than a
  473. -- few thousand years in the future).
  474. -- day : day.
  475. -- month : month.
  476. -- year : year.
  477. -- Returns : TRUE if the date is a valid one.
  478. -- -------------------------------------------------------------------
  479. -- g_date_valid_julian ()
  480. -- gboolean g_date_valid_julian (guint32 julian_date);
  481. -- Returns TRUE if the Julian day is valid. Anything greater than zero is
  482. -- basically a valid Julian, though there is a 32-bit limit.
  483. -- julian_date : Julian day to check.
  484. -- Returns : TRUE if the Julian day is valid.
  485. -- -------------------------------------------------------------------
  486. -- g_date_valid_weekday ()
  487. -- gboolean g_date_valid_weekday (GDateWeekday weekday);
  488. -- Returns TRUE if the weekday is valid. The 7 GDateWeekday enumeration
  489. -- values are the only valid weekdays.
  490. -- weekday : weekday.
  491. -- Returns : TRUE if the weekday is valid.
  492. feature {} -- External features
  493. -- #define G_DATE_BAD_DAY
  494. -- #define G_DATE_BAD_JULIAN
  495. -- #define G_DATE_BAD_YEAR
  496. g_date_new : POINTER is -- GDate
  497. external "C use <glib.h>"
  498. end
  499. g_date_new_dmy (a_day: INTEGER_8; a_month: INTEGER; an_year: INTEGER_16): POINTER is -- GDate*
  500. -- Note: `a_day' and `a_month' shall be NATURAL_8, `an_year' NATURAL_16
  501. external "C use <glib.h>"
  502. end
  503. g_date_new_julian (a_julian_day: INTEGER_32): POINTER is -- GDate
  504. -- Note: `a_julian_day' shall be a NATURAL_32
  505. external "C use <glib.h>"
  506. end
  507. g_date_clear (a_date: POINTER; n_dates: INTEGER_32) is
  508. -- Note `n_dates' shall be a NATURAL_32
  509. external "C use <glib.h>"
  510. end
  511. free (a_date: POINTER) is
  512. external "C use <glib.h>"
  513. alias "g_date_free"
  514. end
  515. g_date_set_day (a_date: POINTER; a_day: INTEGER_8) is
  516. external "C use <glib.h>"
  517. end
  518. g_date_set_month (a_date: POINTER; a_month: INTEGER) is
  519. external "C use <glib.h>"
  520. end
  521. g_date_set_year (a_date: POINTER; an_year: INTEGER_16) is
  522. external "C use <glib.h>"
  523. end
  524. g_date_set_dmy (a_date: POINTER; a_day: INTEGER_8; a_month: INTEGER; an_year: INTEGER_16) is
  525. external "C use <glib.h>"
  526. end
  527. g_date_set_julian (a_date: POINTER; a_julian_date: INTEGER_32) is
  528. -- Note: a_julian_date shall be NATURAL
  529. external "C use <glib.h>"
  530. end
  531. g_date_set_time (a_date: POINTER; a_time: INTEGER_32) is
  532. external "C use <glib.h>"
  533. end
  534. g_date_set_parse (a_date: POINTER; a_str: POINTER) is
  535. external "C use <glib.h>"
  536. end
  537. g_date_add_days (a_date: POINTER; n_days: INTEGER) is
  538. -- Note: `n_days' shall be NATURAL
  539. external "C use <glib.h>"
  540. end
  541. g_date_subtract_days (a_date: POINTER; n_days: INTEGER) is
  542. -- Note: n_days shall be NATURAL
  543. external "C use <glib.h>"
  544. end
  545. g_date_add_months (a_date: POINTER; n_months: INTEGER) is
  546. -- Note `n_months' shall be NATURAL
  547. external "C use <glib.h>"
  548. end
  549. g_date_subtract_months (a_date: POINTER; n_months: INTEGER) is
  550. -- Note `n_months' shall be NATURAL
  551. external "C use <glib.h>"
  552. end
  553. g_date_add_years (a_date: POINTER; n_years: INTEGER) is
  554. -- Note `n_years' shall be NATURAL
  555. external "C use <glib.h>"
  556. end
  557. g_date_subtract_years (a_date: POINTER; n_years: INTEGER) is
  558. -- Note `n_years' shall be NATURAL
  559. external "C use <glib.h>"
  560. end
  561. g_date_days_between (a_date, another_date: POINTER): INTEGER is -- gint
  562. external "C use <glib.h>"
  563. end
  564. g_date_compare (left, right: POINTER): INTEGER is -- gint
  565. external "C use <glib.h>"
  566. end
  567. g_date_clamp (a_date, a_min_date, a_max_date: POINTER) is
  568. external "C use <glib.h>"
  569. end
  570. g_date_order (a_date, another_date: POINTER) is
  571. external "C use <glib.h>"
  572. end
  573. g_date_get_day (a_date: POINTER): INTEGER is -- GDateDay
  574. external "C use <glib.h>"
  575. ensure is_valid_gdate_day (Result)
  576. end
  577. g_date_get_month (a_date: POINTER): INTEGER is -- GDateMonth
  578. external "C use <glib.h>"
  579. ensure is_valid_gdate_month (Result)
  580. end
  581. g_date_get_year (a_date: POINTER): INTEGER is -- GDateYear
  582. -- Result shall be NATURAL
  583. external "C use <glib.h>"
  584. end
  585. g_date_get_julian (a_date: POINTER): INTEGER_32 is -- guint32
  586. -- Result shall be NATURAL_32
  587. external "C use <glib.h>"
  588. ensure positive: Result > 0
  589. end
  590. g_date_get_weekday (a_date: POINTER): INTEGER is -- GDateWeekday
  591. external "C use <glib.h>"
  592. ensure is_valid_gdate_weekday (Result)
  593. end
  594. g_date_get_day_of_year (a_date: POINTER): INTEGER is -- guint
  595. -- Note Result shall be NATURAL
  596. external "C use <glib.h>"
  597. ensure positive: Result > 0
  598. end
  599. g_date_get_days_in_month (a_month: INTEGER; an_year: INTEGER_16): INTEGER is
  600. -- Result shall be a NATURAL_8 (guint8)
  601. external "C use <glib.h>"
  602. -- ensure fits_natural_8: Result.in_range (0,255)
  603. end
  604. g_date_is_first_of_month (a_date: POINTER): INTEGER is -- gboolean
  605. external "C use <glib.h>"
  606. end
  607. g_date_is_last_of_month (a_date: POINTER): INTEGER is -- gboolean
  608. external "C use <glib.h>"
  609. end
  610. g_date_is_leap_year (an_year: INTEGER_16): INTEGER is -- gboolean
  611. external "C use <glib.h>"
  612. end
  613. g_date_get_monday_week_of_year (a_date: POINTER): INTEGER is -- guint
  614. external "C use <glib.h>"
  615. ensure natural: Result >= 0
  616. end
  617. g_date_get_monday_weeks_in_year (an_year: INTEGER_16): INTEGER_8 is
  618. external "C use <glib.h>"
  619. -- TODO: ensure natural_8: Result.in_range (0,255)
  620. end
  621. g_date_get_sunday_week_of_year (a_date: POINTER): INTEGER is -- guint
  622. external "C use <glib.h>"
  623. ensure natural: Result >= 0
  624. end
  625. g_date_get_sunday_weeks_in_year (an_year: INTEGER_16): INTEGER_8 is
  626. -- Result shall be NATURAL_8
  627. external "C use <glib.h>"
  628. -- TODO: ensure natural_8:
  629. end
  630. g_date_get_iso8601_week_of_year (a_date: POINTER): INTEGER is -- guint
  631. -- Result shall be NATURAL
  632. external "C use <glib.h>"
  633. end
  634. g_date_strftime (string: POINTER; len: INTEGER; format, a_date: POINTER): INTEGER is -- gsize
  635. -- Note: len and Result are actually gsize
  636. external "C use <glib.h>"
  637. end
  638. g_date_to_struct_tm (a_date, a_tm_struct: POINTER) is
  639. external "C use <glib.h>"
  640. end
  641. g_date_valid (a_date: POINTER): INTEGER is -- gboolean
  642. external "C use <glib.h>"
  643. end
  644. g_date_valid_day (a_day: INTEGER_8): INTEGER is -- gboolean
  645. external "C use <glib.h>"
  646. end
  647. g_date_valid_month (a_month: INTEGER): INTEGER is -- gboolean
  648. external "C use <glib.h>"
  649. end
  650. g_date_valid_year (an_year: INTEGER_16): INTEGER is -- gboolean
  651. external "C use <glib.h>"
  652. end
  653. g_date_valid_dmy (a_day: INTEGER_8; a_month: INTEGER; an_year: INTEGER_16): INTEGER is -- gboolean
  654. external "C use <glib.h>"
  655. end
  656. g_date_valid_julian (a_julian_date: INTEGER_32): INTEGER is -- gboolean
  657. -- Note: a_julian_date shall be NATURAL_32
  658. external "C use <glib.h>"
  659. end
  660. g_date_valid_weekday (a_weekday: INTEGER): INTEGER is -- gboolean
  661. external "C use <glib.h>"
  662. end
  663. g_usec_per_sec: INTEGER is
  664. -- Number of microseconds in one second (1 million). This
  665. -- macro is provided for code readability.
  666. external "C macro use <glib.h>"
  667. alias "G_USEC_PER_SEC"
  668. end
  669. feature {} -- enum GDateDMY
  670. -- This enumeration isn't used in the API, but may be useful if you
  671. -- need to mark a number as a day, month, or year.
  672. g_date_day: INTEGER is
  673. -- a day
  674. external "C macro use <glib.h>"
  675. alias "G_DATE_DAY"
  676. end
  677. g_date_month: INTEGER is
  678. -- a month
  679. external "C macro use <glib.h>"
  680. alias "G_DATE_MONTH"
  681. end
  682. g_date_year: INTEGER is
  683. -- a year
  684. external "C macro use <glib.h>"
  685. alias "G_DATE_YEAR"
  686. end
  687. feature -- GDateDay a guint8
  688. -- Integer representing a day of the month; between 1 and 31.
  689. is_valid_gdate_day (a_day: INTEGER): BOOLEAN is
  690. do
  691. Result:=a_day.in_range(1,31)
  692. end
  693. g_date_bad_day: INTEGER is
  694. -- represents an invalid day of the month.
  695. external "C macro use <glib.h>"
  696. alias "G_DATE_BAD_DAY"
  697. end
  698. feature {} -- enum GDateMonth
  699. is_valid_gdate_month(a_month: INTEGER): BOOLEAN is
  700. do
  701. -- Note: oh, I know it is quite a stupid implementation, but
  702. -- who assure us that these values are contiguous?
  703. Result:=((a_month=g_date_january) or else
  704. (a_month=g_date_february) or else
  705. (a_month=g_date_march) or else
  706. (a_month=g_date_april) or else
  707. (a_month=g_date_may) or else
  708. (a_month=g_date_june) or else
  709. (a_month=g_date_july) or else
  710. (a_month=g_date_august) or else
  711. (a_month=g_date_september) or else
  712. (a_month=g_date_october) or else
  713. (a_month=g_date_november) or else
  714. (a_month=g_date_december))
  715. end
  716. g_date_bad_month: INTEGER is
  717. -- invalid value.
  718. external "C macro use <glib.h>"
  719. alias "G_DATE_BAD_MONTH"
  720. end
  721. g_date_january: INTEGER is
  722. -- January.
  723. external "C macro use <glib.h>"
  724. alias "G_DATE_JANUARY"
  725. end
  726. g_date_february: INTEGER is
  727. -- February.
  728. external "C macro use <glib.h>"
  729. alias "G_DATE_FEBRUARY"
  730. end
  731. g_date_march: INTEGER is
  732. -- March.
  733. external "C macro use <glib.h>"
  734. alias "G_DATE_MARCH"
  735. end
  736. g_date_april: INTEGER is
  737. -- April.
  738. external "C macro use <glib.h>"
  739. alias "G_DATE_APRIL"
  740. end
  741. g_date_may: INTEGER is
  742. -- May.
  743. external "C macro use <glib.h>"
  744. alias "G_DATE_MAY"
  745. end
  746. g_date_june: INTEGER is
  747. -- June.
  748. external "C macro use <glib.h>"
  749. alias "G_DATE_JUNE"
  750. end
  751. g_date_july: INTEGER is
  752. -- July.
  753. external "C macro use <glib.h>"
  754. alias "G_DATE_JULY"
  755. end
  756. g_date_august: INTEGER is
  757. -- August.
  758. external "C macro use <glib.h>"
  759. alias "G_DATE_AUGUST"
  760. end
  761. g_date_september: INTEGER is
  762. -- September.
  763. external "C macro use <glib.h>"
  764. alias "G_DATE_SEPTEMBER"
  765. end
  766. g_date_october: INTEGER is
  767. -- October.
  768. external "C macro use <glib.h>"
  769. alias "G_DATE_OCTOBER"
  770. end
  771. g_date_november: INTEGER is
  772. -- November.
  773. external "C macro use <glib.h>"
  774. alias "G_DATE_NOVEMBER"
  775. end
  776. g_date_december: INTEGER is
  777. -- December.
  778. external "C macro use <glib.h>"
  779. alias "G_DATE_DECEMBER"
  780. end
  781. -- typedef guint16 GDateYear: Integer representing a year;
  782. -- G_DATE_BAD_YEAR is the invalid value. The year must be 1 or
  783. -- higher; negative (BC) years are not allowed. The year is
  784. -- represented with four digits.
  785. feature {} -- enum GDateWeekday;
  786. is_valid_gdate_weekday (a_weekday: INTEGER): BOOLEAN is
  787. do
  788. -- Note: oh, I know it is quite a stupid implementation, but
  789. -- who assure us that these values are contiguous?
  790. Result:=((a_weekday=g_date_monday) or else
  791. (a_weekday=g_date_tuesday) or else
  792. (a_weekday=g_date_wednesday) or else
  793. (a_weekday=g_date_thursday) or else
  794. (a_weekday=g_date_friday) or else
  795. (a_weekday=g_date_saturday) or else
  796. (a_weekday=g_date_sunday))
  797. end
  798. -- Enumeration representing a day of the week; G_DATE_MONDAY,
  799. -- G_DATE_TUESDAY, etc. G_DATE_BAD_WEEKDAY is an invalid weekday.
  800. g_date_bad_weekday: INTEGER is
  801. -- invalid value.
  802. external "C macro use <glib.h>"
  803. alias "G_DATE_BAD_WEEKDAY"
  804. end
  805. g_date_monday: INTEGER is
  806. -- Monday.
  807. external "C macro use <glib.h>"
  808. alias "G_DATE_MONDAY"
  809. end
  810. g_date_tuesday: INTEGER is
  811. -- Tuesday.
  812. external "C macro use <glib.h>"
  813. alias "G_DATE_TUESDAY"
  814. end
  815. g_date_wednesday: INTEGER is
  816. -- Wednesday.
  817. external "C macro use <glib.h>"
  818. alias "G_DATE_WEDNESDAY"
  819. end
  820. g_date_thursday: INTEGER is
  821. -- Thursday.
  822. external "C macro use <glib.h>"
  823. alias "G_DATE_THURSDAY"
  824. end
  825. g_date_friday: INTEGER is
  826. -- Friday.
  827. external "C macro use <glib.h>"
  828. alias "G_DATE_FRIDAY"
  829. end
  830. g_date_saturday: INTEGER is
  831. -- Saturday.
  832. external "C macro use <glib.h>"
  833. alias "G_DATE_SATURDAY"
  834. end
  835. g_date_sunday: INTEGER is
  836. -- Sunday.
  837. external "C macro use <glib.h>"
  838. alias "G_DATE_SUNDAY"
  839. end
  840. feature {} --
  841. to_string_format: STRING is "%%Y-%%m-%%d"
  842. -- The format used in feature `to_string'; the double %% is
  843. -- becuase both Eiffel and the C function use % as a special character.
  844. feature {} -- GDate struct access
  845. -- typedef struct {
  846. -- guint julian_days : 32; /* julian days representation - we use a
  847. -- * bitfield hoping that 64 bit platforms
  848. -- * will pack this whole struct in one big
  849. -- * int
  850. -- */
  851. -- guint julian : 1; /* julian is valid */
  852. -- guint dmy : 1; /* dmy is valid */
  853. -- /* DMY representation */
  854. -- guint day : 6;
  855. -- guint month : 4;
  856. -- guint year : 16;
  857. -- } GDate;
  858. -- Represents a day between January 1, Year 1 and a few thousand
  859. -- years in the future. None of its members should be accessed
  860. -- directly. If the GDate is obtained from g_date_new(), it will be
  861. -- safe to mutate but invalid and thus not safe for calendrical
  862. -- computations. If it's declared on the stack, it will contain
  863. -- garbage so must be initialized with
  864. -- g_date_clear(). g_date_clear() makes the date invalid but
  865. -- sane. An invalid date doesn't represent a day, it's "empty." A
  866. -- date becomes valid after you set it to a Julian day or you set a
  867. -- day, month, and year.
  868. -- guint julian_days : 32; the Julian representation of the date
  869. -- guint julian : 1; this bit is set if julian_days is valid
  870. -- guint dmy : 1; this is set if day, month and year are valid
  871. -- guint day : 6; the day of the day-month-year representation of the date, as a number between 1 and 31
  872. -- guint month : 4; the day of the day-month-year representation of the date, as a number between 1 and 12
  873. -- guint year : 16; the day of the day-month-year representation of the date
  874. end