|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -import {CalendarDate, CalendarDateTime, parseAbsolute, parseDate, parseDateTime, parseTime, parseZonedDateTime, Time, ZonedDateTime} from '../'; |
| 13 | +import {CalendarDate, CalendarDateTime, parseAbsolute, parseDate, parseDateTime, parseDuration, parseTime, parseZonedDateTime, Time, ZonedDateTime} from '../'; |
14 | 14 |
|
15 | 15 | describe('string conversion', function () {
|
16 | 16 | describe('parseTime', function () {
|
@@ -352,4 +352,237 @@ describe('string conversion', function () {
|
352 | 352 | expect(date.toAbsoluteString()).toBe('2020-02-03T22:32:45.000Z');
|
353 | 353 | });
|
354 | 354 | });
|
| 355 | + |
| 356 | + describe('parseDuration', function () { |
| 357 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, hours, minutes, and seconds and returns a DateTimeDuration object', function () { |
| 358 | + const duration = parseDuration('P3Y6M6W4DT12H30M5S'); |
| 359 | + expect(duration).toStrictEqual({ |
| 360 | + years: 3, |
| 361 | + months: 6, |
| 362 | + weeks: 6, |
| 363 | + days: 4, |
| 364 | + hours: 12, |
| 365 | + minutes: 30, |
| 366 | + seconds: 5 |
| 367 | + }); |
| 368 | + }); |
| 369 | + |
| 370 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, hours, minutes, and fractional values for seconds expressed with a period and returns a DateTimeDuration object', function () { |
| 371 | + const duration = parseDuration('P3Y6M6W4DT12H30M5.5S'); |
| 372 | + expect(duration).toStrictEqual({ |
| 373 | + years: 3, |
| 374 | + months: 6, |
| 375 | + weeks: 6, |
| 376 | + days: 4, |
| 377 | + hours: 12, |
| 378 | + minutes: 30, |
| 379 | + seconds: 5.5 |
| 380 | + }); |
| 381 | + }); |
| 382 | + |
| 383 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, hours, minutes, and fractional values for seconds expressed with a comma and returns a DateTimeDuration object', function () { |
| 384 | + const duration = parseDuration('P3Y6M6W4DT12H30M5,5S'); |
| 385 | + expect(duration).toStrictEqual({ |
| 386 | + years: 3, |
| 387 | + months: 6, |
| 388 | + weeks: 6, |
| 389 | + days: 4, |
| 390 | + hours: 12, |
| 391 | + minutes: 30, |
| 392 | + seconds: 5.5 |
| 393 | + }); |
| 394 | + }); |
| 395 | + |
| 396 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, hours, and fractional values for minutes expressed with a period and returns a DateTimeDuration object', function () { |
| 397 | + const duration = parseDuration('P3Y6M6W4DT12H30.5M'); |
| 398 | + expect(duration).toStrictEqual({ |
| 399 | + years: 3, |
| 400 | + months: 6, |
| 401 | + weeks: 6, |
| 402 | + days: 4, |
| 403 | + hours: 12, |
| 404 | + minutes: 30.5, |
| 405 | + seconds: 0 |
| 406 | + }); |
| 407 | + }); |
| 408 | + |
| 409 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, hours, and fractional values for minutes expressed with a comma and returns a DateTimeDuration object', function () { |
| 410 | + const duration = parseDuration('P3Y6M6W4DT12H30,5M'); |
| 411 | + expect(duration).toStrictEqual({ |
| 412 | + years: 3, |
| 413 | + months: 6, |
| 414 | + weeks: 6, |
| 415 | + days: 4, |
| 416 | + hours: 12, |
| 417 | + minutes: 30.5, |
| 418 | + seconds: 0 |
| 419 | + }); |
| 420 | + }); |
| 421 | + |
| 422 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, and fractional values for hours expressed with a period and returns a DateTimeDuration object', function () { |
| 423 | + const duration = parseDuration('P3Y6M6W4DT12.5H'); |
| 424 | + expect(duration).toStrictEqual({ |
| 425 | + years: 3, |
| 426 | + months: 6, |
| 427 | + weeks: 6, |
| 428 | + days: 4, |
| 429 | + hours: 12.5, |
| 430 | + minutes: 0, |
| 431 | + seconds: 0 |
| 432 | + }); |
| 433 | + }); |
| 434 | + |
| 435 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, and fractional values for hours expressed with a comma and returns a DateTimeDuration object', function () { |
| 436 | + const duration = parseDuration('P3Y6M6W4DT12.5H'); |
| 437 | + expect(duration).toStrictEqual({ |
| 438 | + years: 3, |
| 439 | + months: 6, |
| 440 | + weeks: 6, |
| 441 | + days: 4, |
| 442 | + hours: 12.5, |
| 443 | + minutes: 0, |
| 444 | + seconds: 0 |
| 445 | + }); |
| 446 | + }); |
| 447 | + |
| 448 | + it('parses a negative ISO 8601 duration string that contains years, months, weeks, days, hours, minutes, and seconds and returns a DateTimeDuration object', function () { |
| 449 | + const duration = parseDuration('-P3Y6M6W4DT12H30M5S'); |
| 450 | + expect(duration).toStrictEqual({ |
| 451 | + years: -3, |
| 452 | + months: -6, |
| 453 | + weeks: -6, |
| 454 | + days: -4, |
| 455 | + hours: -12, |
| 456 | + minutes: -30, |
| 457 | + seconds: -5 |
| 458 | + }); |
| 459 | + }); |
| 460 | + |
| 461 | + it('parses an ISO 8601 duration string that contains years, months, weeks, days, hours, minutes, and seconds with a preceding + sign and returns a DateTimeDuration object', function () { |
| 462 | + const duration = parseDuration('+P3Y6M6W4DT12H30M5S'); |
| 463 | + expect(duration).toStrictEqual({ |
| 464 | + years: 3, |
| 465 | + months: 6, |
| 466 | + weeks: 6, |
| 467 | + days: 4, |
| 468 | + hours: 12, |
| 469 | + minutes: 30, |
| 470 | + seconds: 5 |
| 471 | + }); |
| 472 | + }); |
| 473 | + |
| 474 | + it('parses an ISO 8601 duration string that contains hours, minutes, and seconds and returns a DateTimeDuration object', function () { |
| 475 | + const duration = parseDuration('PT20H35M15S'); |
| 476 | + expect(duration).toStrictEqual({ |
| 477 | + years: 0, |
| 478 | + months: 0, |
| 479 | + weeks: 0, |
| 480 | + days: 0, |
| 481 | + hours: 20, |
| 482 | + minutes: 35, |
| 483 | + seconds: 15 |
| 484 | + }); |
| 485 | + }); |
| 486 | + |
| 487 | + it('parses an ISO 8601 duration string that contains years, months, weeks, and days and returns a DateTimeDuration object', function () { |
| 488 | + const duration = parseDuration('P7Y8M14W6D'); |
| 489 | + expect(duration).toStrictEqual({ |
| 490 | + years: 7, |
| 491 | + months: 8, |
| 492 | + weeks: 14, |
| 493 | + days: 6, |
| 494 | + hours: 0, |
| 495 | + minutes: 0, |
| 496 | + seconds: 0 |
| 497 | + }); |
| 498 | + }); |
| 499 | + |
| 500 | + it('parses an ISO 8601 duration string that contains years, months, hours, and seconds and returns a DateTimeDuration object', function () { |
| 501 | + const duration = parseDuration('P18Y7MT20H15S'); |
| 502 | + expect(duration).toStrictEqual({ |
| 503 | + years: 18, |
| 504 | + months: 7, |
| 505 | + weeks: 0, |
| 506 | + days: 0, |
| 507 | + hours: 20, |
| 508 | + minutes: 0, |
| 509 | + seconds: 15 |
| 510 | + }); |
| 511 | + }); |
| 512 | + |
| 513 | + it('throws an error when passed an improperly formatted ISO 8601 duration string', function () { |
| 514 | + expect(() => { |
| 515 | + parseDuration('+-P18Y7MT20H15S'); |
| 516 | + }).toThrow('Invalid ISO 8601 Duration string: +-P18Y7MT20H15S'); |
| 517 | + expect(() => { |
| 518 | + parseDuration('-+P18Y7MT20H15S'); |
| 519 | + }).toThrow('Invalid ISO 8601 Duration string: -+P18Y7MT20H15S'); |
| 520 | + expect(() => { |
| 521 | + parseDuration('--P18Y7MT20H15S'); |
| 522 | + }).toThrow('Invalid ISO 8601 Duration string: --P18Y7MT20H15S'); |
| 523 | + expect(() => { |
| 524 | + parseDuration('++P18Y7MT20H15S'); |
| 525 | + }).toThrow('Invalid ISO 8601 Duration string: ++P18Y7MT20H15S'); |
| 526 | + expect(() => { |
| 527 | + parseDuration('P18Y7MT'); |
| 528 | + }).toThrow('Invalid ISO 8601 Duration string: P18Y7MT'); |
| 529 | + expect(() => { |
| 530 | + parseDuration('P18Y7MT30H15S'); |
| 531 | + }).toThrow('Invalid ISO 8601 Duration string: P18Y7MT30H15S'); |
| 532 | + expect(() => { |
| 533 | + parseDuration('7Y6D85'); |
| 534 | + }).toThrow('Invalid ISO 8601 Duration string: 7Y6D85'); |
| 535 | + expect(() => { |
| 536 | + parseDuration('P1Y1M1W1DT1H1M1.123456789123S'); |
| 537 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M1W1DT1H1M1.123456789123S'); |
| 538 | + expect(() => { |
| 539 | + parseDuration('P0.5Y'); |
| 540 | + }).toThrow('Invalid ISO 8601 Duration string: P0.5Y'); |
| 541 | + expect(() => { |
| 542 | + parseDuration('P1Y0,5M'); |
| 543 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y0,5M'); |
| 544 | + expect(() => { |
| 545 | + parseDuration('P1Y1M0.5W'); |
| 546 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M0.5W'); |
| 547 | + expect(() => { |
| 548 | + parseDuration('P1Y1M1W0,5D'); |
| 549 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M1W0,5D'); |
| 550 | + expect(() => { |
| 551 | + parseDuration('P1Y1M1W1DT0.5H5S'); |
| 552 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M1W1DT0.5H5S - only the smallest unit can be fractional'); |
| 553 | + expect(() => { |
| 554 | + parseDuration('P1Y1M1W1DT1.5H0,5M'); |
| 555 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M1W1DT1.5H0,5M - only the smallest unit can be fractional'); |
| 556 | + expect(() => { |
| 557 | + parseDuration('P1Y1M1W1DT1H0.5M0.5S'); |
| 558 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M1W1DT1H0.5M0.5S - only the smallest unit can be fractional'); |
| 559 | + expect(() => { |
| 560 | + parseDuration('P'); |
| 561 | + }).toThrow('Invalid ISO 8601 Duration string: P'); |
| 562 | + expect(() => { |
| 563 | + parseDuration('PT'); |
| 564 | + }).toThrow('Invalid ISO 8601 Duration string: PT'); |
| 565 | + expect(() => { |
| 566 | + parseDuration('-P'); |
| 567 | + }).toThrow('Invalid ISO 8601 Duration string: -P'); |
| 568 | + expect(() => { |
| 569 | + parseDuration('-PT'); |
| 570 | + }).toThrow('Invalid ISO 8601 Duration string: -PT'); |
| 571 | + expect(() => { |
| 572 | + parseDuration('+P'); |
| 573 | + }).toThrow('Invalid ISO 8601 Duration string: +P'); |
| 574 | + expect(() => { |
| 575 | + parseDuration('+PT'); |
| 576 | + }).toThrow('Invalid ISO 8601 Duration string: +PT'); |
| 577 | + expect(() => { |
| 578 | + parseDuration('P1Y1M1W1DT1H1M1.01Sjunk'); |
| 579 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y1M1W1DT1H1M1.01Sjunk'); |
| 580 | + expect(() => { |
| 581 | + parseDuration('P-1Y1M'); |
| 582 | + }).toThrow('Invalid ISO 8601 Duration string: P-1Y1M'); |
| 583 | + expect(() => { |
| 584 | + parseDuration('P1Y-1M'); |
| 585 | + }).toThrow('Invalid ISO 8601 Duration string: P1Y-1M'); |
| 586 | + }); |
| 587 | + }); |
355 | 588 | });
|
0 commit comments