|
1 | 1 | import datetime as dt |
| 2 | +import warnings |
2 | 3 | from collections import OrderedDict |
3 | 4 |
|
4 | 5 | import pytest |
|
8 | 9 | from khal.icalendar import new_vevent |
9 | 10 | from khal.parse_datetime import ( |
10 | 11 | construct_daynames, |
| 12 | + datetimefstr, |
11 | 13 | eventinfofstr, |
12 | 14 | guessdatetimefstr, |
13 | 15 | guessrangefstr, |
@@ -546,6 +548,70 @@ def test_leap_year(): |
546 | 548 | assert _replace_uid(event).to_ical() == vevent |
547 | 549 |
|
548 | 550 |
|
| 551 | +def test_datetimefstr_without_year_does_not_warn(): |
| 552 | + with warnings.catch_warnings(): |
| 553 | + warnings.simplefilter("error") |
| 554 | + parsed = datetimefstr(["17.03.", "16:00"], "%d.%m. %H:%M", dt.date(2016, 9, 19)) |
| 555 | + assert parsed == dt.datetime(2017, 3, 17, 16, 0) |
| 556 | + leap = datetimefstr(["29.02."], "%d.%m.", dt.date(2016, 1, 1), in_future=False) |
| 557 | + assert leap == dt.datetime(2016, 2, 29) |
| 558 | + with pytest.raises(ValueError, match="^$"): |
| 559 | + datetimefstr(["29.02."], "%d.%m.", dt.date(2015, 1, 1)) |
| 560 | + |
| 561 | + |
| 562 | +@pytest.mark.parametrize( |
| 563 | + ("dateformat", "text"), |
| 564 | + [ |
| 565 | + ("%d.%m.%%Y", "17.03.%Y"), |
| 566 | + ("%%j %d.%m.", "%j 17.03."), |
| 567 | + ("%%%d.%m.", "%17.03."), |
| 568 | + ], |
| 569 | +) |
| 570 | +def test_datetimefstr_escaped_percent_is_not_a_year_directive(dateformat, text): |
| 571 | + with warnings.catch_warnings(): |
| 572 | + warnings.simplefilter("error") |
| 573 | + result = datetimefstr(text.split(" "), dateformat, dt.date(2016, 9, 19)) |
| 574 | + assert result == dt.datetime(2017, 3, 17) |
| 575 | + |
| 576 | + |
| 577 | +@pytest.mark.parametrize( |
| 578 | + ("dateformat", "text", "expected"), |
| 579 | + [ |
| 580 | + ("%d.%m.%Y", "17.03.2016", dt.datetime(2016, 3, 17)), |
| 581 | + ("%d.%m.%y", "17.03.16", dt.datetime(2016, 3, 17)), |
| 582 | + ("%j", "076", dt.datetime(1900, 3, 17)), |
| 583 | + ], |
| 584 | +) |
| 585 | +def test_datetimefstr_year_directives_still_parsed(dateformat, text, expected): |
| 586 | + with warnings.catch_warnings(): |
| 587 | + warnings.simplefilter("error") |
| 588 | + result = datetimefstr([text], dateformat, dt.date(2016, 9, 19), infer_year=False) |
| 589 | + assert result == expected |
| 590 | + |
| 591 | + |
| 592 | +@pytest.mark.parametrize( |
| 593 | + ("dtime_list", "dateformat"), |
| 594 | + [ |
| 595 | + (["17.03."], "%d.%m. "), |
| 596 | + (["17.03. "], "%d.%m."), |
| 597 | + ([" 17.03."], "%d.%m."), |
| 598 | + ], |
| 599 | +) |
| 600 | +@pytest.mark.filterwarnings("ignore::DeprecationWarning") |
| 601 | +def test_datetimefstr_surrounding_whitespace_still_rejected(dtime_list, dateformat): |
| 602 | + with pytest.raises(ValueError, match="does not match|unconverted|without a year"): |
| 603 | + datetimefstr(dtime_list, dateformat, dt.date(2016, 9, 19)) |
| 604 | + |
| 605 | + |
| 606 | +@pytest.mark.filterwarnings("ignore::DeprecationWarning") |
| 607 | +def test_guessdatetimefstr_dateformat_with_trailing_space_raises_parse_error(): |
| 608 | + locale = dict(LOCALE_BERLIN) |
| 609 | + locale["dateformat"] = "%d.%m. " |
| 610 | + locale["datetimeformat"] = "%d.%m. %H:%M" |
| 611 | + with pytest.raises(DateTimeParseError): |
| 612 | + guessdatetimefstr(["17.03."], locale, default_day=dt.date(2024, 1, 1)) |
| 613 | + |
| 614 | + |
549 | 615 | test_set_description = _create_testcases( |
550 | 616 | # now events where the start date has to be inferred, too |
551 | 617 | # today |
|
0 commit comments