|
| 1 | +""" |
| 2 | +Utilities for handling time spans and date ranges. |
| 3 | +""" |
| 4 | + |
| 5 | +import re |
| 6 | +from datetime import datetime, timedelta |
| 7 | +from dateutil.relativedelta import relativedelta |
| 8 | + |
| 9 | + |
| 10 | +def get_week_start(date, start_of_week="monday"): |
| 11 | + """Get the start of the week for a given date.""" |
| 12 | + if start_of_week == "monday": |
| 13 | + days_back = date.weekday() |
| 14 | + else: # sunday |
| 15 | + days_back = (date.weekday() + 1) % 7 |
| 16 | + |
| 17 | + return date - timedelta(days=days_back) |
| 18 | + |
| 19 | + |
| 20 | +def get_week_end(date, start_of_week="monday"): |
| 21 | + """Get the end of the week for a given date.""" |
| 22 | + week_start = get_week_start(date, start_of_week) |
| 23 | + return week_start + timedelta(days=6) |
| 24 | + |
| 25 | + |
| 26 | +def detect_time_span(text): |
| 27 | + """Detect time span expressions in text and return span information.""" |
| 28 | + span_patterns = [ |
| 29 | + { |
| 30 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:past|last|previous)\s+month\b", |
| 31 | + "type": "month", |
| 32 | + "direction": "past", |
| 33 | + }, |
| 34 | + { |
| 35 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:past|last|previous)\s+week\b", |
| 36 | + "type": "week", |
| 37 | + "direction": "past", |
| 38 | + }, |
| 39 | + { |
| 40 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:past|last|previous)\s+(\d+)\s+days?\b", |
| 41 | + "type": "days", |
| 42 | + "direction": "past", |
| 43 | + }, |
| 44 | + { |
| 45 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:past|last|previous)\s+(\d+)\s+weeks?\b", |
| 46 | + "type": "weeks", |
| 47 | + "direction": "past", |
| 48 | + }, |
| 49 | + { |
| 50 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:past|last|previous)\s+(\d+)\s+months?\b", |
| 51 | + "type": "months", |
| 52 | + "direction": "past", |
| 53 | + }, |
| 54 | + { |
| 55 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:next|coming|following)\s+month\b", |
| 56 | + "type": "month", |
| 57 | + "direction": "future", |
| 58 | + }, |
| 59 | + { |
| 60 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:next|coming|following)\s+week\b", |
| 61 | + "type": "week", |
| 62 | + "direction": "future", |
| 63 | + }, |
| 64 | + { |
| 65 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:next|coming|following)\s+(\d+)\s+days?\b", |
| 66 | + "type": "days", |
| 67 | + "direction": "future", |
| 68 | + }, |
| 69 | + { |
| 70 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:next|coming|following)\s+(\d+)\s+weeks?\b", |
| 71 | + "type": "weeks", |
| 72 | + "direction": "future", |
| 73 | + }, |
| 74 | + { |
| 75 | + "pattern": r"\b(?:for\s+the\s+|during\s+the\s+|in\s+the\s+)?(?:next|coming|following)\s+(\d+)\s+months?\b", |
| 76 | + "type": "months", |
| 77 | + "direction": "future", |
| 78 | + }, |
| 79 | + ] |
| 80 | + |
| 81 | + for pattern_info in span_patterns: |
| 82 | + match = re.search(pattern_info["pattern"], text, re.IGNORECASE) |
| 83 | + if match: |
| 84 | + result = { |
| 85 | + "type": pattern_info["type"], |
| 86 | + "direction": pattern_info["direction"], |
| 87 | + "matched_text": match.group(0), |
| 88 | + "start_pos": match.start(), |
| 89 | + "end_pos": match.end(), |
| 90 | + } |
| 91 | + |
| 92 | + if match.groups(): |
| 93 | + result["number"] = int(match.group(1)) |
| 94 | + |
| 95 | + return result |
| 96 | + |
| 97 | + return None |
| 98 | + |
| 99 | + |
| 100 | +def generate_time_span(span_info, base_date=None, settings=None): |
| 101 | + """Generate start and end dates for a time span.""" |
| 102 | + if base_date is None: |
| 103 | + base_date = datetime.now() |
| 104 | + |
| 105 | + if settings is None: |
| 106 | + start_of_week = "monday" |
| 107 | + days_in_month = 30 |
| 108 | + else: |
| 109 | + start_of_week = getattr(settings, "DEFAULT_START_OF_WEEK", "monday") |
| 110 | + days_in_month = getattr(settings, "DEFAULT_DAYS_IN_MONTH", 30) |
| 111 | + |
| 112 | + span_type = span_info["type"] |
| 113 | + direction = span_info["direction"] |
| 114 | + number = span_info.get("number", 1) |
| 115 | + |
| 116 | + if direction == "past": |
| 117 | + end_date = base_date |
| 118 | + |
| 119 | + if span_type == "month": |
| 120 | + start_date = end_date - relativedelta(days=days_in_month) |
| 121 | + elif span_type == "week": |
| 122 | + week_start = get_week_start(end_date, start_of_week) |
| 123 | + start_date = week_start - timedelta(days=7) |
| 124 | + end_date = week_start - timedelta(days=1) |
| 125 | + elif span_type == "days": |
| 126 | + start_date = end_date - timedelta(days=number) |
| 127 | + elif span_type == "weeks": |
| 128 | + start_date = end_date - timedelta(weeks=number) |
| 129 | + elif span_type == "months": |
| 130 | + start_date = end_date - relativedelta(months=number) |
| 131 | + else: |
| 132 | + start_date = end_date - timedelta(days=1) |
| 133 | + |
| 134 | + else: |
| 135 | + start_date = base_date |
| 136 | + |
| 137 | + if span_type == "month": |
| 138 | + end_date = start_date + relativedelta(days=days_in_month) |
| 139 | + elif span_type == "week": |
| 140 | + week_start = get_week_start(start_date, start_of_week) |
| 141 | + start_date = week_start + timedelta(days=7) |
| 142 | + end_date = start_date + timedelta(days=6) |
| 143 | + elif span_type == "days": |
| 144 | + end_date = start_date + timedelta(days=number) |
| 145 | + elif span_type == "weeks": |
| 146 | + end_date = start_date + timedelta(weeks=number) |
| 147 | + elif span_type == "months": |
| 148 | + end_date = start_date + relativedelta(months=number) |
| 149 | + else: |
| 150 | + end_date = start_date + timedelta(days=1) |
| 151 | + |
| 152 | + return (start_date, end_date) |
0 commit comments