1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
use webcore::value::Reference; use webcore::try_from::TryInto; /// [(JavaScript docs)](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date) /// https://www.ecma-international.org/ecma-262/6.0/#sec-date-constructor #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)] #[reference(instance_of = "Date")] pub struct Date( Reference ); impl Date { /// Creates a JavaScript Date instance that represents a single moment in time. /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) // https://www.ecma-international.org/ecma-262/6.0/#sec-date-constructor-date pub fn new() -> Self { js!( return new Date(); ).try_into().unwrap() } /// Creates a JavaScript Date instance that represents a single moment in time. /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. /// /// year is an integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999. /// month is an integer value representing the month, beginning with 0 for January to 11 for December /// day is an integer value representing the day of the month (normally from 1 to 31) /// hours an integer value representing the minute segment of a time /// seconds an integer value representing the second segment of a time /// milliseconds an integer value representing the millisecond segment of a time /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) // https://www.ecma-international.org/ecma-262/6.0/#sec-date-year-month-date-hours-minutes-seconds-ms pub fn from_datetime(year: i32, month: i32, day: i32, hours: i32, minutes: i32, seconds: i32, milliseconds: i32) -> Self { js!( return new Date(@{year}, @{month}, @{day}, @{hours}, @{minutes}, @{seconds}, @{milliseconds}); ).try_into().unwrap() } /// Creates a JavaScript Date instance that represents a single moment in time. /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. /// /// String value representing a date. The string should be in a format recognized by /// the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) // https://www.ecma-international.org/ecma-262/6.0/#sec-date-value pub fn from_iso8601(date_string: &str) -> Self { js!( return new Date(@{date_string}); ).try_into().unwrap() } /// Creates a JavaScript Date instance that represents a single moment in time. /// Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC. /// /// Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, /// with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds). /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) // https://www.ecma-international.org/ecma-262/6.0/#sec-date-value pub fn from_time(now: f64) -> Self { js!( return new Date(@{now}); ).try_into().unwrap() } /// The Date.UTC() method accepts the same parameters as the longest form of the constructor, and /// returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.utc pub fn utc(year: i32, month: i32, day: i32, hours: i32, minutes: i32, seconds: i32, milliseconds: i32) -> f64 { js!( return Date.UTC(@{year}, @{month}, @{day}, @{hours}, @{minutes}, @{seconds}, @{milliseconds}); ).try_into().unwrap() } /// The Date.parse() method parses a string representation of a date, and returns the number of /// milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in /// some cases, contains illegal date values (e.g. 2015-02-31). /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.parse pub fn parse(date_string: &str) -> f64 { js!( return Date.parse(@{date_string}); ).try_into().unwrap() } /// The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.now pub fn now() -> f64 { js!( return Date.now(); ).try_into().unwrap() } /// The getDate() method returns the day of the month for the specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getdate pub fn get_date(&self) -> i32 { js!( return @{self}.getDate(); ).try_into().unwrap() } /// The getDay() method returns the day of the week for the specified date according to local time, /// where 0 represents Sunday. For the day of the month see getDate(). /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getday pub fn get_day(&self) -> i32 { js!( return @{self}.getDay(); ).try_into().unwrap() } /// The getFullYear() method returns the year of the specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getfullyear pub fn get_full_year(&self) -> i32 { js!( return @{self}.getFullYear(); ).try_into().unwrap() } /// The getHours() method returns the hour for the specified date, according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.gethours pub fn get_hours(&self) -> i32 { js!( return @{self}.getHours(); ).try_into().unwrap() } /// The getMilliseconds() method returns the milliseconds in the specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getmilliseconds pub fn get_milliseconds(&self) -> i32 { js!( return @{self}.getMilliseconds(); ).try_into().unwrap() } /// The getMinutes() method returns the minutes in the specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getminutes pub fn get_minutes(&self) -> i32 { js!( return @{self}.getMinutes(); ).try_into().unwrap() } /// The getMonth() method returns the month in the specified date according to local time, as a /// zero-based value (where zero indicates the first month of the year). /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getmonth pub fn get_month(&self) -> i32 { js!( return @{self}.getMonth(); ).try_into().unwrap() } /// The getSeconds() method returns the seconds in the specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getSeconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getseconds pub fn get_seconds(&self) -> i32 { js!( return @{self}.getSeconds(); ).try_into().unwrap() } /// The getTime() method returns the numeric value corresponding to the time for the specified /// date according to universal time. /// /// getTime() always uses UTC for time representation. For example, a client browser in one timezone, /// getTime() will be the same as a client browser in any other timezone. /// /// You can use this method to help assign a date and time to another Date object. This method is /// functionally equivalent to the valueOf() method. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.gettime pub fn get_time(&self) -> f64 { js!( return @{self}.getTime(); ).try_into().unwrap() } /// The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale (host system settings) to UTC. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.gettimezoneoffset pub fn get_timezone_offset(&self) -> i32 { js!( return @{self}.getTimezoneOffset(); ).try_into().unwrap() } /// The getUTCDate() method returns the day (date) of the month in the specified date according to /// universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDate) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcdate pub fn get_utc_date(&self) -> i32 { js!( return @{self}.getUTCDate(); ).try_into().unwrap() } /// The getUTCDay() method returns the day of the week in the specified date according to universal /// time, where 0 represents Sunday. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDay) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcday pub fn get_utc_day(&self) -> i32 { js!( return @{self}.getUTCDay(); ).try_into().unwrap() } /// The getUTCFullYear() method returns the year in the specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcfullyear pub fn get_utc_full_year(&self) -> i32 { js!( return @{self}.getUTCFullYear(); ).try_into().unwrap() } /// The getUTCHours() method returns the hours in the specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutchours pub fn get_utc_hours(&self) -> i32 { js!( return @{self}.getUTCHours(); ).try_into().unwrap() } /// The getUTCMilliseconds() method returns the milliseconds in the specified date according to /// universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMilliseconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcmilliseconds pub fn get_utc_milliseconds(&self) -> i32 { js!( return @{self}.getUTCMilliseconds(); ).try_into().unwrap() } /// The getUTCMinutes() method returns the minutes in the specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMinutes) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcminutes pub fn get_utc_minutes(&self) -> i32 { js!( return @{self}.getUTCMinutes(); ).try_into().unwrap() } /// The getUTCMonth() returns the month of the specified date according to universal time, as a /// zero-based value (where zero indicates the first month of the year). /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcmonth pub fn get_utc_month(&self) -> i32 { js!( return @{self}.getUTCMonth(); ).try_into().unwrap() } /// The getUTCSeconds() method returns the seconds in the specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCSeconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.getutcseconds pub fn get_utc_seconds(&self) -> i32 { js!( return @{self}.getUTCSeconds(); ).try_into().unwrap() } /// The setDate() method sets the day of the Date object relative to the beginning of the currently set month. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setdate pub fn set_date(&self, date: i32) { js!{ @(no_return) @{self}.setDate(@{date}); } } /// The setFullYear() method sets the full year for a specified date according to local time. Returns new timestamp. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setfullyear pub fn set_full_year(&self, full_year: i32) { js!{ @(no_return) @{self}.setFullYear(@{full_year}); } } /// The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds /// since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.sethours pub fn set_hours(&self, hours: i32) { js!{ @(no_return) @{self}.setHours(@{hours}); } } /// The setMilliseconds() method sets the milliseconds for a specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMilliseconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setmilliseconds pub fn set_milliseconds(&self, milliseconds: i32) { js!{ @(no_return) @{self}.setMilliseconds(@{milliseconds}); } } /// The setMinutes() method sets the minutes for a specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMinutes) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setminutes pub fn set_minutes(&self, minutes: i32) { js!{ @(no_return) @{self}.setMinutes(@{minutes}); } } /// The setMonth() method sets the month for a specified date according to the currently set year. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setmonth pub fn set_month(&self, month: i32) { js!{ @(no_return) @{self}.setMonth(@{month}); } } /// The setSeconds() method sets the seconds for a specified date according to local time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setSeconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setseconds pub fn set_seconds(&self, seconds: i32) { js!{ @(no_return) @{self}.setSeconds(@{seconds}); } } /// The setTime() method sets the Date object to the time represented by a number of milliseconds since /// January 1, 1970, 00:00:00 UTC. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setTime) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.settime pub fn set_time(&self, time: f64) { js!{ @(no_return) @{self}.setTime(@{time}); } } /// The setUTCDate() method sets the day of the month for a specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCDate) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcdate pub fn set_utc_date(&self, date: i32) { js!{ @(no_return) @{self}.setUTCDate(@{date}); } } /// The setUTCFullYear() method sets the full year for a specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcfullyear pub fn set_utc_full_year(&self, full_year: i32) { js!{ @(no_return) @{self}.setUTCFullYear(@{full_year}); } } /// The setUTCHours() method sets the hour for a specified date according to universal time, and returns the number /// of milliseconds since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCHours) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutchours pub fn set_utc_hours(&self, hours: i32) { js!{ @(no_return) @{self}.setUTCHours(@{hours}); } } /// The setUTCMilliseconds() method sets the milliseconds for a specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMilliseconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcmilliseconds pub fn set_utc_milliseconds(&self, milliseconds: i32) { js!{ @(no_return) @{self}.setUTCMilliseconds(@{milliseconds}); } } /// The setUTCMinutes() method sets the minutes for a specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMinutes) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcminutes pub fn set_utc_minutes(&self, minutes: i32) { js!{ @(no_return) @{self}.setUTCMinutes(@{minutes}); } } /// The setUTCMonth() method sets the month for a specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcmonth pub fn set_utc_month(&self, month: i32) { js!{ @(no_return) @{self}.setUTCMonth(@{month}); } } /// The setUTCSeconds() method sets the seconds for a specified date according to universal time. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCSeconds) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.setutcseconds pub fn set_utc_seconds(&self, seconds: i32) { js!{ @(no_return) @{self}.setUTCSeconds(@{seconds}); } } /// The toDateString() method returns the date portion of a Date object in human readable form in American English. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.todatestring #[inline] pub fn to_date_string(&self) -> String { js!( return @{self}.toDateString(); ).try_into().unwrap() } /// The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 /// characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero /// UTC offset, as denoted by the suffix "Z". /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.toisostring #[inline] pub fn to_iso_string(&self) -> String { js!( return @{self}.toISOString(); ).try_into().unwrap() } /// The toJSON() method returns a string representation of the Date object. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.tojson #[inline] pub fn to_json(&self) -> String { js!( return @{self}.toJSON(); ).try_into().unwrap() } /// The toString() method returns a string representing the specified Date object. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.tostring #[inline] pub fn to_string(&self) -> String { js!( return @{self}.toString(); ).try_into().unwrap() } /// The toTimeString() method returns the time portion of a Date object in human readable form in American English. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toTimeString) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.totimestring #[inline] pub fn to_time_string(&self) -> String { js!( return @{self}.toTimeString(); ).try_into().unwrap() } /// The toUTCString() method converts a date to a string, using the UTC time zone. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.toutcstring #[inline] pub fn to_utc_string(&self) -> String { js!( return @{self}.toUTCString(); ).try_into().unwrap() } /// The valueOf() method returns the primitive value of a Date object. /// /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf) // https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype.valueof pub fn value_of(&self) -> f64 { js!( return @{self}.valueOf(); ).try_into().unwrap() } } #[cfg(test)] mod tests { use super::*; #[test] fn test_date_now() { let now = Date::now(); assert!( now > 0.0 ); } #[test] fn test_date_utc() { let now = Date::utc(96, 1, 2, 3, 4, 5, 0); assert_eq!(now, 823230245000.0); let now = Date::utc(0, 0, 0, 0, 0, 0, 0); assert_eq!(now, -2209075200000.0); } #[test] fn test_date_parse() { let now = Date::parse("01 Jan 1970 00:00:00 GMT"); assert_eq!(now, 0.0); let now = Date::parse("04 Dec 1995 00:12:00 GMT"); assert_eq!(now, 818035920000.0); } #[test] fn test_date_get_date() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!( now.get_date(), 19); } #[test] fn test_date_get_day() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!(now.get_day(), 2); } #[test] fn test_date_get_full_year() { let now = Date::from_iso8601("August 19, 75 23:15:30"); assert_eq!(now.get_full_year(), 1975); } #[test] fn test_date_get_hours() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!(now.get_hours(), 23); } #[test] fn test_date_get_milliseconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_milliseconds(123); assert_eq!(now.get_milliseconds(), 123); } #[test] fn test_date_get_minutes() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!(now.get_minutes(), 15); } #[test] fn test_date_get_month() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!(now.get_month(), 7); } #[test] fn test_date_get_seconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!(now.get_seconds(), 30); } #[test] fn test_date_get_time() { let now = Date::from_iso8601("July 20, 69 00:20:18 GMT+00:00"); assert_eq!(now.get_time(), -14254782000.0); } #[test] fn test_date_get_timezone_offset() { // this is impossible to test like this, since this function depends on local time only. // and there is no easy way to mock the system time, so the only real thing to check // is that two dates return the same timezone offset. let t1 = Date::from_iso8601("August 19, 1975 23:15:30 GMT+07:00"); let t2 = Date::from_iso8601("August 19, 1975 23:15:30 GMT-02:00"); assert_eq!(t1.get_timezone_offset(), t2.get_timezone_offset()); } #[test] fn test_date_get_utc_date() { let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT+11:00"); assert_eq!(now.get_utc_date(), 19); let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT-11:00"); assert_eq!(now.get_utc_date(), 20); } #[test] fn test_date_get_utc_day() { let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT+11:00"); assert_eq!( now.get_utc_day(), 2 ); let now = Date::from_iso8601("August 19, 1975 23:15:30 GMT-11:00"); assert_eq!( now.get_utc_day(), 3 ); } #[test] fn test_date_get_utc_full_year() { let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT+11:00"); assert_eq!(now.get_utc_full_year(), 1975 ); let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT-11:00"); assert_eq!(now.get_utc_full_year(), 1976 ); } #[test] fn test_date_get_utc_milliseconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_milliseconds(123); assert_eq!(now.get_utc_milliseconds(), 123); } #[test] fn test_date_get_utc_minutes() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); assert_eq!(now.get_utc_minutes(), 15); } #[test] fn test_date_get_utc_month() { let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT+11:00"); assert_eq!(now.get_utc_month(), 11); let now = Date::from_iso8601("December 31, 1975 23:15:30 GMT-11:00"); assert_eq!(now.get_utc_month(), 0); } #[test] fn test_date_set_date() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_date(3); assert_eq!(now.get_date(), 3); } #[test] fn test_date_set_full_year() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_full_year(1969); assert_eq!(now.get_full_year(), 1969); } #[test] fn test_date_set_hours() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_hours(15); assert_eq!(now.get_hours(), 15); } #[test] fn test_date_set_milliseconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_milliseconds(123); assert_eq!(now.get_milliseconds(), 123); } #[test] fn test_date_set_minutes() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_minutes(42); assert_eq!(now.get_minutes(), 42); } #[test] fn test_date_set_month() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_month(9); assert_eq!(now.get_month(), 9); } #[test] fn test_date_set_seconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_seconds(59); assert_eq!(now.get_seconds(), 59); } #[test] fn test_date_set_time() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_time(818035920000.0); assert_eq!(now.to_utc_string(), "Mon, 04 Dec 1995 00:12:00 GMT"); } #[test] fn test_date_set_utc_date() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_date(3); assert_eq!(now.get_utc_date(), 3); } #[test] fn test_date_set_utc_full_year() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_full_year(1969); assert_eq!(now.get_utc_full_year(), 1969); } #[test] fn test_date_set_utc_hours() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_hours(15); assert_eq!(now.get_utc_hours(), 15); } #[test] fn test_date_set_utc_milliseconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_milliseconds(123); assert_eq!(now.get_utc_milliseconds(), 123); } #[test] fn test_date_set_utc_minutes() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_minutes(42); assert_eq!(now.get_utc_minutes(), 42); } #[test] fn test_date_set_utc_month() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_month(9); assert_eq!(now.get_utc_month(), 9); } #[test] fn test_date_set_utc_seconds() { let now = Date::from_iso8601("August 19, 1975 23:15:30"); now.set_utc_seconds(59); assert_eq!(now.get_utc_seconds(), 59); } #[test] fn test_date_to_date_string() { let now = Date::from_datetime(1993, 6, 28, 14, 39, 7, 0); assert_eq!(now.to_date_string(), "Wed Jul 28 1993"); } #[test] fn test_date_to_iso_string() { let now = Date::from_iso8601("05 October 2011 14:48 UTC"); assert_eq!(now.to_iso_string(), "2011-10-05T14:48:00.000Z"); } #[test] fn test_date_to_json() { let now = Date::from_iso8601("August 19, 1975 23:15:30 UTC"); assert_eq!(now.to_iso_string(), "1975-08-19T23:15:30.000Z"); } #[test] fn test_date_to_time_string() { // not easy to test this due to time-zones } #[test] fn test_date_to_utc_string() { let now = Date::from_time(Date::utc(96, 1, 2, 3, 4, 5, 0)); assert_eq!(now.to_utc_string(), "Fri, 02 Feb 1996 03:04:05 GMT"); } #[test] fn test_date_value_of() { let now = Date::from_time(Date::utc(96, 1, 2, 3, 4, 5, 0)); assert_eq!(now.value_of(), 823230245000.0); } }