1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.Date; 26 27 private import glib.ConstructionException; 28 private import glib.Str; 29 private import glib.TimeVal; 30 private import glib.c.functions; 31 public import glib.c.types; 32 public import gtkc.glibtypes; 33 private import gtkd.Loader; 34 35 36 /** 37 * Represents a day between January 1, Year 1 and a few thousand years in 38 * the future. None of its members should be accessed directly. 39 * 40 * If the #GDate-struct is obtained from g_date_new(), it will be safe 41 * to mutate but invalid and thus not safe for calendrical computations. 42 * 43 * If it's declared on the stack, it will contain garbage so must be 44 * initialized with g_date_clear(). g_date_clear() makes the date invalid 45 * but sane. An invalid date doesn't represent a day, it's "empty." A date 46 * becomes valid after you set it to a Julian day or you set a day, month, 47 * and year. 48 */ 49 public final class Date 50 { 51 /** the main Gtk struct */ 52 protected GDate* gDate; 53 protected bool ownedRef; 54 55 /** Get the main Gtk struct */ 56 public GDate* getDateStruct(bool transferOwnership = false) 57 { 58 if (transferOwnership) 59 ownedRef = false; 60 return gDate; 61 } 62 63 /** the main Gtk struct as a void* */ 64 protected void* getStruct() 65 { 66 return cast(void*)gDate; 67 } 68 69 /** 70 * Sets our main struct and passes it to the parent class. 71 */ 72 public this (GDate* gDate, bool ownedRef = false) 73 { 74 this.gDate = gDate; 75 this.ownedRef = ownedRef; 76 } 77 78 ~this () 79 { 80 if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef ) 81 g_date_free(gDate); 82 } 83 84 85 /** 86 * the Julian representation of the date 87 */ 88 public @property uint julianDays() 89 { 90 return gDate.julianDays; 91 } 92 93 /** Ditto */ 94 public @property void julianDays(uint value) 95 { 96 gDate.julianDays = value; 97 } 98 99 /** 100 * this bit is set if @julian_days is valid 101 */ 102 public @property uint julian() 103 { 104 return gDate.julian; 105 } 106 107 /** Ditto */ 108 public @property void julian(uint value) 109 { 110 gDate.julian = value; 111 } 112 113 /** 114 * this is set if @day, @month and @year are valid 115 */ 116 public @property uint dmy() 117 { 118 return gDate.dmy; 119 } 120 121 /** Ditto */ 122 public @property void dmy(uint value) 123 { 124 gDate.dmy = value; 125 } 126 127 /** 128 * the day of the day-month-year representation of the date, 129 * as a number between 1 and 31 130 */ 131 public @property uint day() 132 { 133 return gDate.day; 134 } 135 136 /** Ditto */ 137 public @property void day(uint value) 138 { 139 gDate.day = value; 140 } 141 142 /** 143 * the day of the day-month-year representation of the date, 144 * as a number between 1 and 12 145 */ 146 public @property uint month() 147 { 148 return gDate.month; 149 } 150 151 /** Ditto */ 152 public @property void month(uint value) 153 { 154 gDate.month = value; 155 } 156 157 /** 158 * the day of the day-month-year representation of the date 159 */ 160 public @property uint year() 161 { 162 return gDate.year; 163 } 164 165 /** Ditto */ 166 public @property void year(uint value) 167 { 168 gDate.year = value; 169 } 170 171 /** 172 * Allocates a #GDate and initializes 173 * it to a sane state. The new date will 174 * be cleared (as if you'd called g_date_clear()) but invalid (it won't 175 * represent an existing day). Free the return value with g_date_free(). 176 * 177 * Returns: a newly-allocated #GDate 178 * 179 * Throws: ConstructionException GTK+ fails to create the object. 180 */ 181 public this() 182 { 183 auto p = g_date_new(); 184 185 if(p is null) 186 { 187 throw new ConstructionException("null returned by new"); 188 } 189 190 this(cast(GDate*) p); 191 } 192 193 /** 194 * Like g_date_new(), but also sets the value of the date. Assuming the 195 * day-month-year triplet you pass in represents an existing day, the 196 * returned date will be valid. 197 * 198 * Params: 199 * day = day of the month 200 * month = month of the year 201 * year = year 202 * 203 * Returns: a newly-allocated #GDate initialized with @day, @month, and @year 204 * 205 * Throws: ConstructionException GTK+ fails to create the object. 206 */ 207 public this(GDateDay day, GDateMonth month, GDateYear year) 208 { 209 auto p = g_date_new_dmy(day, month, year); 210 211 if(p is null) 212 { 213 throw new ConstructionException("null returned by new_dmy"); 214 } 215 216 this(cast(GDate*) p); 217 } 218 219 /** 220 * Like g_date_new(), but also sets the value of the date. Assuming the 221 * Julian day number you pass in is valid (greater than 0, less than an 222 * unreasonably large number), the returned date will be valid. 223 * 224 * Params: 225 * julianDay = days since January 1, Year 1 226 * 227 * Returns: a newly-allocated #GDate initialized with @julian_day 228 * 229 * Throws: ConstructionException GTK+ fails to create the object. 230 */ 231 public this(uint julianDay) 232 { 233 auto p = g_date_new_julian(julianDay); 234 235 if(p is null) 236 { 237 throw new ConstructionException("null returned by new_julian"); 238 } 239 240 this(cast(GDate*) p); 241 } 242 243 /** 244 * Increments a date some number of days. 245 * To move forward by weeks, add weeks*7 days. 246 * The date must be valid. 247 * 248 * Params: 249 * nDays = number of days to move the date forward 250 */ 251 public void addDays(uint nDays) 252 { 253 g_date_add_days(gDate, nDays); 254 } 255 256 /** 257 * Increments a date by some number of months. 258 * If the day of the month is greater than 28, 259 * this routine may change the day of the month 260 * (because the destination month may not have 261 * the current day in it). The date must be valid. 262 * 263 * Params: 264 * nMonths = number of months to move forward 265 */ 266 public void addMonths(uint nMonths) 267 { 268 g_date_add_months(gDate, nMonths); 269 } 270 271 /** 272 * Increments a date by some number of years. 273 * If the date is February 29, and the destination 274 * year is not a leap year, the date will be changed 275 * to February 28. The date must be valid. 276 * 277 * Params: 278 * nYears = number of years to move forward 279 */ 280 public void addYears(uint nYears) 281 { 282 g_date_add_years(gDate, nYears); 283 } 284 285 /** 286 * If @date is prior to @min_date, sets @date equal to @min_date. 287 * If @date falls after @max_date, sets @date equal to @max_date. 288 * Otherwise, @date is unchanged. 289 * Either of @min_date and @max_date may be %NULL. 290 * All non-%NULL dates must be valid. 291 * 292 * Params: 293 * minDate = minimum accepted value for @date 294 * maxDate = maximum accepted value for @date 295 */ 296 public void clamp(Date minDate, Date maxDate) 297 { 298 g_date_clamp(gDate, (minDate is null) ? null : minDate.getDateStruct(), (maxDate is null) ? null : maxDate.getDateStruct()); 299 } 300 301 /** 302 * Initializes one or more #GDate structs to a sane but invalid 303 * state. The cleared dates will not represent an existing date, but will 304 * not contain garbage. Useful to init a date declared on the stack. 305 * Validity can be tested with g_date_valid(). 306 * 307 * Params: 308 * nDates = number of dates to clear 309 */ 310 public void clear(uint nDates) 311 { 312 g_date_clear(gDate, nDates); 313 } 314 315 /** 316 * qsort()-style comparison function for dates. 317 * Both dates must be valid. 318 * 319 * Params: 320 * rhs = second date to compare 321 * 322 * Returns: 0 for equal, less than zero if @lhs is less than @rhs, 323 * greater than zero if @lhs is greater than @rhs 324 */ 325 public int compare(Date rhs) 326 { 327 return g_date_compare(gDate, (rhs is null) ? null : rhs.getDateStruct()); 328 } 329 330 /** 331 * Computes the number of days between two dates. 332 * If @date2 is prior to @date1, the returned value is negative. 333 * Both dates must be valid. 334 * 335 * Params: 336 * date2 = the second date 337 * 338 * Returns: the number of days between @date1 and @date2 339 */ 340 public int daysBetween(Date date2) 341 { 342 return g_date_days_between(gDate, (date2 is null) ? null : date2.getDateStruct()); 343 } 344 345 /** 346 * Frees a #GDate returned from g_date_new(). 347 */ 348 public void free() 349 { 350 g_date_free(gDate); 351 ownedRef = false; 352 } 353 354 /** 355 * Returns the day of the month. The date must be valid. 356 * 357 * Returns: day of the month 358 */ 359 public GDateDay getDay() 360 { 361 return g_date_get_day(gDate); 362 } 363 364 /** 365 * Returns the day of the year, where Jan 1 is the first day of the 366 * year. The date must be valid. 367 * 368 * Returns: day of the year 369 */ 370 public uint getDayOfYear() 371 { 372 return g_date_get_day_of_year(gDate); 373 } 374 375 /** 376 * Returns the week of the year, where weeks are interpreted according 377 * to ISO 8601. 378 * 379 * Returns: ISO 8601 week number of the year. 380 * 381 * Since: 2.6 382 */ 383 public uint getIso8601WeekOfYear() 384 { 385 return g_date_get_iso8601_week_of_year(gDate); 386 } 387 388 /** 389 * Returns the Julian day or "serial number" of the #GDate. The 390 * Julian day is simply the number of days since January 1, Year 1; i.e., 391 * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2, 392 * etc. The date must be valid. 393 * 394 * Returns: Julian day 395 */ 396 public uint getJulian() 397 { 398 return g_date_get_julian(gDate); 399 } 400 401 /** 402 * Returns the week of the year, where weeks are understood to start on 403 * Monday. If the date is before the first Monday of the year, return 0. 404 * The date must be valid. 405 * 406 * Returns: week of the year 407 */ 408 public uint getMondayWeekOfYear() 409 { 410 return g_date_get_monday_week_of_year(gDate); 411 } 412 413 /** 414 * Returns the month of the year. The date must be valid. 415 * 416 * Returns: month of the year as a #GDateMonth 417 */ 418 public GDateMonth getMonth() 419 { 420 return g_date_get_month(gDate); 421 } 422 423 /** 424 * Returns the week of the year during which this date falls, if 425 * weeks are understood to begin on Sunday. The date must be valid. 426 * Can return 0 if the day is before the first Sunday of the year. 427 * 428 * Returns: week number 429 */ 430 public uint getSundayWeekOfYear() 431 { 432 return g_date_get_sunday_week_of_year(gDate); 433 } 434 435 /** 436 * Returns the day of the week for a #GDate. The date must be valid. 437 * 438 * Returns: day of the week as a #GDateWeekday. 439 */ 440 public GDateWeekday getWeekday() 441 { 442 return g_date_get_weekday(gDate); 443 } 444 445 /** 446 * Returns the year of a #GDate. The date must be valid. 447 * 448 * Returns: year in which the date falls 449 */ 450 public GDateYear getYear() 451 { 452 return g_date_get_year(gDate); 453 } 454 455 /** 456 * Returns %TRUE if the date is on the first of a month. 457 * The date must be valid. 458 * 459 * Returns: %TRUE if the date is the first of the month 460 */ 461 public bool isFirstOfMonth() 462 { 463 return g_date_is_first_of_month(gDate) != 0; 464 } 465 466 /** 467 * Returns %TRUE if the date is the last day of the month. 468 * The date must be valid. 469 * 470 * Returns: %TRUE if the date is the last day of the month 471 */ 472 public bool isLastOfMonth() 473 { 474 return g_date_is_last_of_month(gDate) != 0; 475 } 476 477 /** 478 * Checks if @date1 is less than or equal to @date2, 479 * and swap the values if this is not the case. 480 * 481 * Params: 482 * date2 = the second date 483 */ 484 public void order(Date date2) 485 { 486 g_date_order(gDate, (date2 is null) ? null : date2.getDateStruct()); 487 } 488 489 /** 490 * Sets the day of the month for a #GDate. If the resulting 491 * day-month-year triplet is invalid, the date will be invalid. 492 * 493 * Params: 494 * day = day to set 495 */ 496 public void setDay(GDateDay day) 497 { 498 g_date_set_day(gDate, day); 499 } 500 501 /** 502 * Sets the value of a #GDate from a day, month, and year. 503 * The day-month-year triplet must be valid; if you aren't 504 * sure it is, call g_date_valid_dmy() to check before you 505 * set it. 506 * 507 * Params: 508 * day = day 509 * month = month 510 * y = year 511 */ 512 public void setDmy(GDateDay day, GDateMonth month, GDateYear y) 513 { 514 g_date_set_dmy(gDate, day, month, y); 515 } 516 517 /** 518 * Sets the value of a #GDate from a Julian day number. 519 * 520 * Params: 521 * julianDate = Julian day number (days since January 1, Year 1) 522 */ 523 public void setJulian(uint julianDate) 524 { 525 g_date_set_julian(gDate, julianDate); 526 } 527 528 /** 529 * Sets the month of the year for a #GDate. If the resulting 530 * day-month-year triplet is invalid, the date will be invalid. 531 * 532 * Params: 533 * month = month to set 534 */ 535 public void setMonth(GDateMonth month) 536 { 537 g_date_set_month(gDate, month); 538 } 539 540 /** 541 * Parses a user-inputted string @str, and try to figure out what date it 542 * represents, taking the [current locale][setlocale] into account. If the 543 * string is successfully parsed, the date will be valid after the call. 544 * Otherwise, it will be invalid. You should check using g_date_valid() 545 * to see whether the parsing succeeded. 546 * 547 * This function is not appropriate for file formats and the like; it 548 * isn't very precise, and its exact behavior varies with the locale. 549 * It's intended to be a heuristic routine that guesses what the user 550 * means by a given string (and it does work pretty well in that 551 * capacity). 552 * 553 * Params: 554 * str = string to parse 555 */ 556 public void setParse(string str) 557 { 558 g_date_set_parse(gDate, Str.toStringz(str)); 559 } 560 561 /** 562 * Sets the value of a date from a #GTime value. 563 * The time to date conversion is done using the user's current timezone. 564 * 565 * Deprecated: Use g_date_set_time_t() instead. 566 * 567 * Params: 568 * time = #GTime value to set. 569 */ 570 public void setTime(GTime time) 571 { 572 g_date_set_time(gDate, time); 573 } 574 575 /** 576 * Sets the value of a date to the date corresponding to a time 577 * specified as a time_t. The time to date conversion is done using 578 * the user's current timezone. 579 * 580 * To set the value of a date to the current day, you could write: 581 * |[<!-- language="C" --> 582 * g_date_set_time_t (date, time (NULL)); 583 * ]| 584 * 585 * Params: 586 * timet = time_t value to set 587 * 588 * Since: 2.10 589 */ 590 public void setTimeT(uint timet) 591 { 592 g_date_set_time_t(gDate, timet); 593 } 594 595 /** 596 * Sets the value of a date from a #GTimeVal value. Note that the 597 * @tv_usec member is ignored, because #GDate can't make use of the 598 * additional precision. 599 * 600 * The time to date conversion is done using the user's current timezone. 601 * 602 * Params: 603 * timeval = #GTimeVal value to set 604 * 605 * Since: 2.10 606 */ 607 public void setTimeVal(TimeVal timeval) 608 { 609 g_date_set_time_val(gDate, (timeval is null) ? null : timeval.getTimeValStruct()); 610 } 611 612 /** 613 * Sets the year for a #GDate. If the resulting day-month-year 614 * triplet is invalid, the date will be invalid. 615 * 616 * Params: 617 * year = year to set 618 */ 619 public void setYear(GDateYear year) 620 { 621 g_date_set_year(gDate, year); 622 } 623 624 /** 625 * Moves a date some number of days into the past. 626 * To move by weeks, just move by weeks*7 days. 627 * The date must be valid. 628 * 629 * Params: 630 * nDays = number of days to move 631 */ 632 public void subtractDays(uint nDays) 633 { 634 g_date_subtract_days(gDate, nDays); 635 } 636 637 /** 638 * Moves a date some number of months into the past. 639 * If the current day of the month doesn't exist in 640 * the destination month, the day of the month 641 * may change. The date must be valid. 642 * 643 * Params: 644 * nMonths = number of months to move 645 */ 646 public void subtractMonths(uint nMonths) 647 { 648 g_date_subtract_months(gDate, nMonths); 649 } 650 651 /** 652 * Moves a date some number of years into the past. 653 * If the current day doesn't exist in the destination 654 * year (i.e. it's February 29 and you move to a non-leap-year) 655 * then the day is changed to February 29. The date 656 * must be valid. 657 * 658 * Params: 659 * nYears = number of years to move 660 */ 661 public void subtractYears(uint nYears) 662 { 663 g_date_subtract_years(gDate, nYears); 664 } 665 666 /** 667 * Fills in the date-related bits of a struct tm using the @date value. 668 * Initializes the non-date parts with something sane but meaningless. 669 * 670 * Params: 671 * tm = struct tm to fill 672 */ 673 public void toStructTm(void* tm) 674 { 675 g_date_to_struct_tm(gDate, tm); 676 } 677 678 /** 679 * Returns %TRUE if the #GDate represents an existing day. The date must not 680 * contain garbage; it should have been initialized with g_date_clear() 681 * if it wasn't allocated by one of the g_date_new() variants. 682 * 683 * Returns: Whether the date is valid 684 */ 685 public bool valid() 686 { 687 return g_date_valid(gDate) != 0; 688 } 689 690 /** 691 * Returns the number of days in a month, taking leap 692 * years into account. 693 * 694 * Params: 695 * month = month 696 * year = year 697 * 698 * Returns: number of days in @month during the @year 699 */ 700 public static ubyte getDaysInMonth(GDateMonth month, GDateYear year) 701 { 702 return g_date_get_days_in_month(month, year); 703 } 704 705 /** 706 * Returns the number of weeks in the year, where weeks 707 * are taken to start on Monday. Will be 52 or 53. The 708 * date must be valid. (Years always have 52 7-day periods, 709 * plus 1 or 2 extra days depending on whether it's a leap 710 * year. This function is basically telling you how many 711 * Mondays are in the year, i.e. there are 53 Mondays if 712 * one of the extra days happens to be a Monday.) 713 * 714 * Params: 715 * year = a year 716 * 717 * Returns: number of Mondays in the year 718 */ 719 public static ubyte getMondayWeeksInYear(GDateYear year) 720 { 721 return g_date_get_monday_weeks_in_year(year); 722 } 723 724 /** 725 * Returns the number of weeks in the year, where weeks 726 * are taken to start on Sunday. Will be 52 or 53. The 727 * date must be valid. (Years always have 52 7-day periods, 728 * plus 1 or 2 extra days depending on whether it's a leap 729 * year. This function is basically telling you how many 730 * Sundays are in the year, i.e. there are 53 Sundays if 731 * one of the extra days happens to be a Sunday.) 732 * 733 * Params: 734 * year = year to count weeks in 735 * 736 * Returns: the number of weeks in @year 737 */ 738 public static ubyte getSundayWeeksInYear(GDateYear year) 739 { 740 return g_date_get_sunday_weeks_in_year(year); 741 } 742 743 /** 744 * Returns %TRUE if the year is a leap year. 745 * 746 * For the purposes of this function, leap year is every year 747 * divisible by 4 unless that year is divisible by 100. If it 748 * is divisible by 100 it would be a leap year only if that year 749 * is also divisible by 400. 750 * 751 * Params: 752 * year = year to check 753 * 754 * Returns: %TRUE if the year is a leap year 755 */ 756 public static bool isLeapYear(GDateYear year) 757 { 758 return g_date_is_leap_year(year) != 0; 759 } 760 761 /** 762 * Generates a printed representation of the date, in a 763 * [locale][setlocale]-specific way. 764 * Works just like the platform's C library strftime() function, 765 * but only accepts date-related formats; time-related formats 766 * give undefined results. Date must be valid. Unlike strftime() 767 * (which uses the locale encoding), works on a UTF-8 format 768 * string and stores a UTF-8 result. 769 * 770 * This function does not provide any conversion specifiers in 771 * addition to those implemented by the platform's C library. 772 * For example, don't expect that using g_date_strftime() would 773 * make the \%F provided by the C99 strftime() work on Windows 774 * where the C library only complies to C89. 775 * 776 * Params: 777 * s = destination buffer 778 * slen = buffer size 779 * format = format string 780 * date = valid #GDate 781 * 782 * Returns: number of characters written to the buffer, or 0 the buffer was too small 783 */ 784 public static size_t strftime(string s, size_t slen, string format, Date date) 785 { 786 return g_date_strftime(Str.toStringz(s), slen, Str.toStringz(format), (date is null) ? null : date.getDateStruct()); 787 } 788 789 /** 790 * Returns %TRUE if the day of the month is valid (a day is valid if it's 791 * between 1 and 31 inclusive). 792 * 793 * Params: 794 * day = day to check 795 * 796 * Returns: %TRUE if the day is valid 797 */ 798 public static bool validDay(GDateDay day) 799 { 800 return g_date_valid_day(day) != 0; 801 } 802 803 /** 804 * Returns %TRUE if the day-month-year triplet forms a valid, existing day 805 * in the range of days #GDate understands (Year 1 or later, no more than 806 * a few thousand years in the future). 807 * 808 * Params: 809 * day = day 810 * month = month 811 * year = year 812 * 813 * Returns: %TRUE if the date is a valid one 814 */ 815 public static bool validDmy(GDateDay day, GDateMonth month, GDateYear year) 816 { 817 return g_date_valid_dmy(day, month, year) != 0; 818 } 819 820 /** 821 * Returns %TRUE if the Julian day is valid. Anything greater than zero 822 * is basically a valid Julian, though there is a 32-bit limit. 823 * 824 * Params: 825 * julianDate = Julian day to check 826 * 827 * Returns: %TRUE if the Julian day is valid 828 */ 829 public static bool validJulian(uint julianDate) 830 { 831 return g_date_valid_julian(julianDate) != 0; 832 } 833 834 /** 835 * Returns %TRUE if the month value is valid. The 12 #GDateMonth 836 * enumeration values are the only valid months. 837 * 838 * Params: 839 * month = month 840 * 841 * Returns: %TRUE if the month is valid 842 */ 843 public static bool validMonth(GDateMonth month) 844 { 845 return g_date_valid_month(month) != 0; 846 } 847 848 /** 849 * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration 850 * values are the only valid weekdays. 851 * 852 * Params: 853 * weekday = weekday 854 * 855 * Returns: %TRUE if the weekday is valid 856 */ 857 public static bool validWeekday(GDateWeekday weekday) 858 { 859 return g_date_valid_weekday(weekday) != 0; 860 } 861 862 /** 863 * Returns %TRUE if the year is valid. Any year greater than 0 is valid, 864 * though there is a 16-bit limit to what #GDate will understand. 865 * 866 * Params: 867 * year = year 868 * 869 * Returns: %TRUE if the year is valid 870 */ 871 public static bool validYear(GDateYear year) 872 { 873 return g_date_valid_year(year) != 0; 874 } 875 }