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