#1046 Add parser support for 24hr time with PM flag

Open Brooklyn Rose Ludlow brookcub
Showing 2 of 4 files from the diff.

@@ -8,7 +8,7 @@
Loading
8 8
of a date/time stamp is omitted, the following rules are applied:
9 9
10 10
- If AM or PM is left unspecified, a 24-hour clock is assumed, however, an hour
11 -
  on a 12-hour clock (``0 <= hour <= 12``) *must* be specified if AM or PM is
11 +
  on a 12-hour clock (``0 <= hour <= 12``) *must* be specified if AM is
12 12
  specified.
13 13
- If a time zone is omitted, a timezone-naive datetime is returned.
14 14
@@ -786,7 +786,7 @@
Loading
786 786
                # Check am/pm
787 787
                elif info.ampm(l[i]) is not None:
788 788
                    value = info.ampm(l[i])
789 -
                    val_is_ampm = self._ampm_valid(res.hour, res.ampm, fuzzy)
789 +
                    val_is_ampm = self._ampm_valid(value, res.hour, res.ampm, fuzzy)
790 790
791 791
                    if val_is_ampm:
792 792
                        res.hour = self._adjust_ampm(res.hour, value)
@@ -1067,7 +1067,7 @@
Loading
1067 1067
                (all(x in string.ascii_uppercase for x in token)
1068 1068
                 or token in self.info.UTCZONE))
1069 1069
1070 -
    def _ampm_valid(self, hour, ampm, fuzzy):
1070 +
    def _ampm_valid(self, value, hour, ampm, fuzzy):
1071 1071
        """
1072 1072
        For fuzzy parsing, 'a' or 'am' (both valid English words)
1073 1073
        may erroneously trigger the AM/PM flag. Deal with that
@@ -1085,13 +1085,13 @@
Loading
1085 1085
                val_is_ampm = False
1086 1086
            else:
1087 1087
                raise ValueError('No hour specified with AM or PM flag.')
1088 -
        elif not 0 <= hour <= 12:
1089 -
            # If AM/PM is found, it's a 12 hour clock, so raise
1088 +
        elif value == 0 and not 0 <= hour <= 12:
1089 +
            # If AM is found, it's a 12 hour clock, so raise
1090 1090
            # an error for invalid range
1091 1091
            if fuzzy:
1092 1092
                val_is_ampm = False
1093 1093
            else:
1094 -
                raise ValueError('Invalid hour specified for 12-hour clock.')
1094 +
                raise ValueError('Invalid hour specified for AM flag.')
1095 1095
1096 1096
        return val_is_ampm
1097 1097

@@ -96,6 +96,7 @@
Loading
96 96
    ('13NOV2017', datetime(2017, 11, 13), "dBY (See GH360)"),
97 97
    ('0003-03-04', datetime(3, 3, 4), "pre 12 year same month (See GH PR #293)"),
98 98
    ('December.0031.30', datetime(31, 12, 30), "BYd corner case (GH#687)"),
99 +
    ('January 25, 1921 23:13 PM', datetime(1921, 1, 25, 23, 13), "24hr time w PM (issue #1042)"),
99 100
100 101
    # Cases with legacy h/m/s format, candidates for deprecation (GH#886)
101 102
    ("2016-12-21 04.2h", datetime(2016, 12, 21, 4, 12), "Fractional Hours"),
@@ -155,7 +156,8 @@
Loading
155 156
    ("01h02s", datetime(2003, 9, 25, 1, 0, 2), "random format"),
156 157
    ("01m02", datetime(2003, 9, 25, 0, 1, 2), "random format"),
157 158
    ("01m02h", datetime(2003, 9, 25, 2, 1), "random format"),
158 -
    ("2004 10 Apr 11h30m", datetime(2004, 4, 10, 11, 30), "random format")
159 +
    ("2004 10 Apr 11h30m", datetime(2004, 4, 10, 11, 30), "random format"),
160 +
    ("22:01 PM", datetime(2003, 9, 25, 22, 1), '24hr time w PM (issue #1042)')
159 161
]
160 162
# Check that we don't have any duplicates
161 163
assert len(set([x[0] for x in PARSER_DEFAULT_TEST_CASES])) == len(PARSER_DEFAULT_TEST_CASES)
@@ -490,7 +492,7 @@
Loading
490 492
            parse("13:44 AM")
491 493
492 494
        with pytest.raises(ParserError):
493 -
            parse("January 25, 1921 23:13 PM")
495 +
            parse("January 25, 1921 23:13 AM")
494 496
495 497
    def testPertain(self):
496 498
        self.assertEqual(parse("Sep 03", default=self.default),

Everything is accounted for!

No changes detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Coverage
dateutil 95.99%
Project Totals (32 files) 95.99%
Loading