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 * Copies a GDate to a newly-allocated GDate. If the input was invalid 332 * (as determined by g_date_valid()), the invalid state will be copied 333 * as is into the new object. 334 * 335 * Returns: a newly-allocated #GDate initialized from @date 336 * 337 * Since: 2.56 338 */ 339 public Date copy() 340 { 341 auto p = g_date_copy(gDate); 342 343 if(p is null) 344 { 345 return null; 346 } 347 348 return new Date(cast(GDate*) p, true); 349 } 350 351 /** 352 * Computes the number of days between two dates. 353 * If @date2 is prior to @date1, the returned value is negative. 354 * Both dates must be valid. 355 * 356 * Params: 357 * date2 = the second date 358 * 359 * Returns: the number of days between @date1 and @date2 360 */ 361 public int daysBetween(Date date2) 362 { 363 return g_date_days_between(gDate, (date2 is null) ? null : date2.getDateStruct()); 364 } 365 366 /** 367 * Frees a #GDate returned from g_date_new(). 368 */ 369 public void free() 370 { 371 g_date_free(gDate); 372 ownedRef = false; 373 } 374 375 /** 376 * Returns the day of the month. The date must be valid. 377 * 378 * Returns: day of the month 379 */ 380 public GDateDay getDay() 381 { 382 return g_date_get_day(gDate); 383 } 384 385 /** 386 * Returns the day of the year, where Jan 1 is the first day of the 387 * year. The date must be valid. 388 * 389 * Returns: day of the year 390 */ 391 public uint getDayOfYear() 392 { 393 return g_date_get_day_of_year(gDate); 394 } 395 396 /** 397 * Returns the week of the year, where weeks are interpreted according 398 * to ISO 8601. 399 * 400 * Returns: ISO 8601 week number of the year. 401 * 402 * Since: 2.6 403 */ 404 public uint getIso8601WeekOfYear() 405 { 406 return g_date_get_iso8601_week_of_year(gDate); 407 } 408 409 /** 410 * Returns the Julian day or "serial number" of the #GDate. The 411 * Julian day is simply the number of days since January 1, Year 1; i.e., 412 * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2, 413 * etc. The date must be valid. 414 * 415 * Returns: Julian day 416 */ 417 public uint getJulian() 418 { 419 return g_date_get_julian(gDate); 420 } 421 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 * Returns: week of the year 428 */ 429 public uint getMondayWeekOfYear() 430 { 431 return g_date_get_monday_week_of_year(gDate); 432 } 433 434 /** 435 * Returns the month of the year. The date must be valid. 436 * 437 * Returns: month of the year as a #GDateMonth 438 */ 439 public GDateMonth getMonth() 440 { 441 return g_date_get_month(gDate); 442 } 443 444 /** 445 * Returns the week of the year during which this date falls, if 446 * weeks are understood to begin on Sunday. The date must be valid. 447 * Can return 0 if the day is before the first Sunday of the year. 448 * 449 * Returns: week number 450 */ 451 public uint getSundayWeekOfYear() 452 { 453 return g_date_get_sunday_week_of_year(gDate); 454 } 455 456 /** 457 * Returns the day of the week for a #GDate. The date must be valid. 458 * 459 * Returns: day of the week as a #GDateWeekday. 460 */ 461 public GDateWeekday getWeekday() 462 { 463 return g_date_get_weekday(gDate); 464 } 465 466 /** 467 * Returns the year of a #GDate. The date must be valid. 468 * 469 * Returns: year in which the date falls 470 */ 471 public GDateYear getYear() 472 { 473 return g_date_get_year(gDate); 474 } 475 476 /** 477 * Returns %TRUE if the date is on the first of a month. 478 * The date must be valid. 479 * 480 * Returns: %TRUE if the date is the first of the month 481 */ 482 public bool isFirstOfMonth() 483 { 484 return g_date_is_first_of_month(gDate) != 0; 485 } 486 487 /** 488 * Returns %TRUE if the date is the last day of the month. 489 * The date must be valid. 490 * 491 * Returns: %TRUE if the date is the last day of the month 492 */ 493 public bool isLastOfMonth() 494 { 495 return g_date_is_last_of_month(gDate) != 0; 496 } 497 498 /** 499 * Checks if @date1 is less than or equal to @date2, 500 * and swap the values if this is not the case. 501 * 502 * Params: 503 * date2 = the second date 504 */ 505 public void order(Date date2) 506 { 507 g_date_order(gDate, (date2 is null) ? null : date2.getDateStruct()); 508 } 509 510 /** 511 * Sets the day of the month for a #GDate. If the resulting 512 * day-month-year triplet is invalid, the date will be invalid. 513 * 514 * Params: 515 * day = day to set 516 */ 517 public void setDay(GDateDay day) 518 { 519 g_date_set_day(gDate, day); 520 } 521 522 /** 523 * Sets the value of a #GDate from a day, month, and year. 524 * The day-month-year triplet must be valid; if you aren't 525 * sure it is, call g_date_valid_dmy() to check before you 526 * set it. 527 * 528 * Params: 529 * day = day 530 * month = month 531 * y = year 532 */ 533 public void setDmy(GDateDay day, GDateMonth month, GDateYear y) 534 { 535 g_date_set_dmy(gDate, day, month, y); 536 } 537 538 /** 539 * Sets the value of a #GDate from a Julian day number. 540 * 541 * Params: 542 * julianDate = Julian day number (days since January 1, Year 1) 543 */ 544 public void setJulian(uint julianDate) 545 { 546 g_date_set_julian(gDate, julianDate); 547 } 548 549 /** 550 * Sets the month of the year for a #GDate. If the resulting 551 * day-month-year triplet is invalid, the date will be invalid. 552 * 553 * Params: 554 * month = month to set 555 */ 556 public void setMonth(GDateMonth month) 557 { 558 g_date_set_month(gDate, month); 559 } 560 561 /** 562 * Parses a user-inputted string @str, and try to figure out what date it 563 * represents, taking the [current locale][setlocale] into account. If the 564 * string is successfully parsed, the date will be valid after the call. 565 * Otherwise, it will be invalid. You should check using g_date_valid() 566 * to see whether the parsing succeeded. 567 * 568 * This function is not appropriate for file formats and the like; it 569 * isn't very precise, and its exact behavior varies with the locale. 570 * It's intended to be a heuristic routine that guesses what the user 571 * means by a given string (and it does work pretty well in that 572 * capacity). 573 * 574 * Params: 575 * str = string to parse 576 */ 577 public void setParse(string str) 578 { 579 g_date_set_parse(gDate, Str.toStringz(str)); 580 } 581 582 /** 583 * Sets the value of a date from a #GTime value. 584 * The time to date conversion is done using the user's current timezone. 585 * 586 * Deprecated: Use g_date_set_time_t() instead. 587 * 588 * Params: 589 * time = #GTime value to set. 590 */ 591 public void setTime(GTime time) 592 { 593 g_date_set_time(gDate, time); 594 } 595 596 /** 597 * Sets the value of a date to the date corresponding to a time 598 * specified as a time_t. The time to date conversion is done using 599 * the user's current timezone. 600 * 601 * To set the value of a date to the current day, you could write: 602 * |[<!-- language="C" --> 603 * g_date_set_time_t (date, time (NULL)); 604 * ]| 605 * 606 * Params: 607 * timet = time_t value to set 608 * 609 * Since: 2.10 610 */ 611 public void setTimeT(uint timet) 612 { 613 g_date_set_time_t(gDate, timet); 614 } 615 616 /** 617 * Sets the value of a date from a #GTimeVal value. Note that the 618 * @tv_usec member is ignored, because #GDate can't make use of the 619 * additional precision. 620 * 621 * The time to date conversion is done using the user's current timezone. 622 * 623 * Params: 624 * timeval = #GTimeVal value to set 625 * 626 * Since: 2.10 627 */ 628 public void setTimeVal(TimeVal timeval) 629 { 630 g_date_set_time_val(gDate, (timeval is null) ? null : timeval.getTimeValStruct()); 631 } 632 633 /** 634 * Sets the year for a #GDate. If the resulting day-month-year 635 * triplet is invalid, the date will be invalid. 636 * 637 * Params: 638 * year = year to set 639 */ 640 public void setYear(GDateYear year) 641 { 642 g_date_set_year(gDate, year); 643 } 644 645 /** 646 * Moves a date some number of days into the past. 647 * To move by weeks, just move by weeks*7 days. 648 * The date must be valid. 649 * 650 * Params: 651 * nDays = number of days to move 652 */ 653 public void subtractDays(uint nDays) 654 { 655 g_date_subtract_days(gDate, nDays); 656 } 657 658 /** 659 * Moves a date some number of months into the past. 660 * If the current day of the month doesn't exist in 661 * the destination month, the day of the month 662 * may change. The date must be valid. 663 * 664 * Params: 665 * nMonths = number of months to move 666 */ 667 public void subtractMonths(uint nMonths) 668 { 669 g_date_subtract_months(gDate, nMonths); 670 } 671 672 /** 673 * Moves a date some number of years into the past. 674 * If the current day doesn't exist in the destination 675 * year (i.e. it's February 29 and you move to a non-leap-year) 676 * then the day is changed to February 29. The date 677 * must be valid. 678 * 679 * Params: 680 * nYears = number of years to move 681 */ 682 public void subtractYears(uint nYears) 683 { 684 g_date_subtract_years(gDate, nYears); 685 } 686 687 /** 688 * Fills in the date-related bits of a struct tm using the @date value. 689 * Initializes the non-date parts with something sane but meaningless. 690 * 691 * Params: 692 * tm = struct tm to fill 693 */ 694 public void toStructTm(void* tm) 695 { 696 g_date_to_struct_tm(gDate, tm); 697 } 698 699 /** 700 * Returns %TRUE if the #GDate represents an existing day. The date must not 701 * contain garbage; it should have been initialized with g_date_clear() 702 * if it wasn't allocated by one of the g_date_new() variants. 703 * 704 * Returns: Whether the date is valid 705 */ 706 public bool valid() 707 { 708 return g_date_valid(gDate) != 0; 709 } 710 711 /** 712 * Returns the number of days in a month, taking leap 713 * years into account. 714 * 715 * Params: 716 * month = month 717 * year = year 718 * 719 * Returns: number of days in @month during the @year 720 */ 721 public static ubyte getDaysInMonth(GDateMonth month, GDateYear year) 722 { 723 return g_date_get_days_in_month(month, year); 724 } 725 726 /** 727 * Returns the number of weeks in the year, where weeks 728 * are taken to start on Monday. Will be 52 or 53. The 729 * date must be valid. (Years always have 52 7-day periods, 730 * plus 1 or 2 extra days depending on whether it's a leap 731 * year. This function is basically telling you how many 732 * Mondays are in the year, i.e. there are 53 Mondays if 733 * one of the extra days happens to be a Monday.) 734 * 735 * Params: 736 * year = a year 737 * 738 * Returns: number of Mondays in the year 739 */ 740 public static ubyte getMondayWeeksInYear(GDateYear year) 741 { 742 return g_date_get_monday_weeks_in_year(year); 743 } 744 745 /** 746 * Returns the number of weeks in the year, where weeks 747 * are taken to start on Sunday. Will be 52 or 53. The 748 * date must be valid. (Years always have 52 7-day periods, 749 * plus 1 or 2 extra days depending on whether it's a leap 750 * year. This function is basically telling you how many 751 * Sundays are in the year, i.e. there are 53 Sundays if 752 * one of the extra days happens to be a Sunday.) 753 * 754 * Params: 755 * year = year to count weeks in 756 * 757 * Returns: the number of weeks in @year 758 */ 759 public static ubyte getSundayWeeksInYear(GDateYear year) 760 { 761 return g_date_get_sunday_weeks_in_year(year); 762 } 763 764 /** 765 * Returns %TRUE if the year is a leap year. 766 * 767 * For the purposes of this function, leap year is every year 768 * divisible by 4 unless that year is divisible by 100. If it 769 * is divisible by 100 it would be a leap year only if that year 770 * is also divisible by 400. 771 * 772 * Params: 773 * year = year to check 774 * 775 * Returns: %TRUE if the year is a leap year 776 */ 777 public static bool isLeapYear(GDateYear year) 778 { 779 return g_date_is_leap_year(year) != 0; 780 } 781 782 /** 783 * Generates a printed representation of the date, in a 784 * [locale][setlocale]-specific way. 785 * Works just like the platform's C library strftime() function, 786 * but only accepts date-related formats; time-related formats 787 * give undefined results. Date must be valid. Unlike strftime() 788 * (which uses the locale encoding), works on a UTF-8 format 789 * string and stores a UTF-8 result. 790 * 791 * This function does not provide any conversion specifiers in 792 * addition to those implemented by the platform's C library. 793 * For example, don't expect that using g_date_strftime() would 794 * make the \%F provided by the C99 strftime() work on Windows 795 * where the C library only complies to C89. 796 * 797 * Params: 798 * s = destination buffer 799 * slen = buffer size 800 * format = format string 801 * date = valid #GDate 802 * 803 * Returns: number of characters written to the buffer, or 0 the buffer was too small 804 */ 805 public static size_t strftime(string s, size_t slen, string format, Date date) 806 { 807 return g_date_strftime(Str.toStringz(s), slen, Str.toStringz(format), (date is null) ? null : date.getDateStruct()); 808 } 809 810 /** 811 * Returns %TRUE if the day of the month is valid (a day is valid if it's 812 * between 1 and 31 inclusive). 813 * 814 * Params: 815 * day = day to check 816 * 817 * Returns: %TRUE if the day is valid 818 */ 819 public static bool validDay(GDateDay day) 820 { 821 return g_date_valid_day(day) != 0; 822 } 823 824 /** 825 * Returns %TRUE if the day-month-year triplet forms a valid, existing day 826 * in the range of days #GDate understands (Year 1 or later, no more than 827 * a few thousand years in the future). 828 * 829 * Params: 830 * day = day 831 * month = month 832 * year = year 833 * 834 * Returns: %TRUE if the date is a valid one 835 */ 836 public static bool validDmy(GDateDay day, GDateMonth month, GDateYear year) 837 { 838 return g_date_valid_dmy(day, month, year) != 0; 839 } 840 841 /** 842 * Returns %TRUE if the Julian day is valid. Anything greater than zero 843 * is basically a valid Julian, though there is a 32-bit limit. 844 * 845 * Params: 846 * julianDate = Julian day to check 847 * 848 * Returns: %TRUE if the Julian day is valid 849 */ 850 public static bool validJulian(uint julianDate) 851 { 852 return g_date_valid_julian(julianDate) != 0; 853 } 854 855 /** 856 * Returns %TRUE if the month value is valid. The 12 #GDateMonth 857 * enumeration values are the only valid months. 858 * 859 * Params: 860 * month = month 861 * 862 * Returns: %TRUE if the month is valid 863 */ 864 public static bool validMonth(GDateMonth month) 865 { 866 return g_date_valid_month(month) != 0; 867 } 868 869 /** 870 * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration 871 * values are the only valid weekdays. 872 * 873 * Params: 874 * weekday = weekday 875 * 876 * Returns: %TRUE if the weekday is valid 877 */ 878 public static bool validWeekday(GDateWeekday weekday) 879 { 880 return g_date_valid_weekday(weekday) != 0; 881 } 882 883 /** 884 * Returns %TRUE if the year is valid. Any year greater than 0 is valid, 885 * though there is a 16-bit limit to what #GDate will understand. 886 * 887 * Params: 888 * year = year 889 * 890 * Returns: %TRUE if the year is valid 891 */ 892 public static bool validYear(GDateYear year) 893 { 894 return g_date_valid_year(year) != 0; 895 } 896 }