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