Fix an edge condition around the Nth weekday of the month when
authorScott Gasch <[email protected]>
Fri, 13 May 2022 02:14:34 +0000 (19:14 -0700)
committerScott Gasch <[email protected]>
Fri, 13 May 2022 02:14:34 +0000 (19:14 -0700)
the first day of the month is that target weekday.

dateparse/dateparse_utils.py
tests/dateparse_utils_test.py

index bd0d491f17e55fc4eef1894cfbe8a544b88787f2..ee2bbd9f6a9ef47519815d1747bde6124eb591af 100755 (executable)
@@ -672,10 +672,10 @@ class DateParser(dateparse_utilsListener):
             del self.context['dow']
             self.context['delta_unit'] = dow
 
-            # For the nth Fooday in Month, start at the 1st of the
-            # month and count ahead N Foodays.  For the last Fooday in
-            # Month, start at the last of the month and count back one
-            # Fooday.
+            # For the nth Fooday in Month, start at the last day of
+            # the previous month count ahead N Foodays.  For the last
+            # Fooday in Month, start at the last of the month and
+            # count back one Fooday.
             if n == -1:
                 month += 1
                 if month == 13:
@@ -684,17 +684,16 @@ class DateParser(dateparse_utilsListener):
                 tmp_date = datetime.date(year=year, month=month, day=1)
                 tmp_date = tmp_date - datetime.timedelta(days=1)
 
-                self.context['year'] = tmp_date.year
-                self.context['month'] = tmp_date.month
-                self.context['day'] = tmp_date.day
-
                 # The delta adjustment code can handle the case where
                 # the last day of the month is the day we're looking
                 # for already.
             else:
-                self.context['year'] = year
-                self.context['month'] = month
-                self.context['day'] = 1
+                tmp_date = datetime.date(year=year, month=month, day=1)
+                tmp_date = tmp_date - datetime.timedelta(days=1)
+
+            self.context['year'] = tmp_date.year
+            self.context['month'] = tmp_date.month
+            self.context['day'] = tmp_date.day
             self.main_type = DateParser.PARSE_TYPE_BASE_AND_OFFSET_EXPR
         except Exception as e:
             raise ParseException(f'Invalid nthWeekday expression: {ctx.getText()}') from e
index 6e539ff8ccce9592e09dd48a94b0d6b13bf0703f..8dd8131662a36b68ae49430b4829c75a12f9e751 100755 (executable)
@@ -73,6 +73,7 @@ parsable_expressions = [
     ('2 sun in jun', datetime.datetime(2021, 6, 13)),
     ('easter -40 days', datetime.datetime(2021, 2, 23)),
     ('easter +39 days', datetime.datetime(2021, 5, 13)),
+    ('2nd Sunday in May, 2022', datetime.datetime(2022, 5, 8)),
     ('1st tuesday in nov, 2024', datetime.datetime(2024, 11, 5)),
     (
         '2 days before last xmas at 3:14:15.92a',