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