"""RULES_inventory_amendment1.txt (I6b): infer UTC date crossings for rows that give a UTC time. Writes results/i6b.csv (sheet_row, class, utc_minus_local, local_date_offset, date_utc_is) and prints counts.""" import csv import os import re import sys from collections import Counter sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import lib_ee as L # noqa: E402 MON = {m: i for i, m in enumerate(["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"], 1)} MONS = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec" UTC_TIME = re.compile(r"(\d{1,2})[:h.]?(\d{2})\s*(?:UTC|GMT)\b", re.I) UTC_DATE_WORD = re.compile(r"(\d{1,2})[:h.]?(\d{2})\s*(?:UTC|GMT)\s*(?:on\s+|,\s*)?(?:(?:mon|tue|wed|thu|fri|sat|sun)[a-z]*\s+)?(\d{1,2})\s+(" + MONS + r")[a-z]*", re.I) UTC_DATE_NUM = re.compile(r"(\d{1,2})[:h.]?(\d{2})\s*(?:UTC|GMT)\s*(?:on\s+)?(?:(\d{4})-)?(\d{1,2})-(\d{1,2})\b", re.I) LOCAL_T = re.compile(r"(\d{1,2})[:h.](\d{2})|midnight|noon", re.I) AMPM = re.compile(r"^\s*(a\.?m\.?|p\.?m\.?)", re.I) LOC_DATE = re.compile(r"\b(\d{1,2})\s+(" + MONS + r")[a-z]*|\b(" + MONS + r")[a-z]*\.?\s+(\d{1,2})\b|\bon\s+(\d{1,2})-(\d{1,2})\b", re.I) def nearest(d, mon, day): best = None for y in (d.year - 1, d.year, d.year + 1): try: c = L.dt.date(y, mon, day) except ValueError: continue if best is None or abs((c - d).days) < abs((best - d).days): best = c return best def local_date_offset(text, d, upto): ml = list(LOC_DATE.finditer(text[:upto])) if not ml: return None g = ml[-1] try: if g.group(1): c = nearest(d, MON[g.group(2).lower()[:3]], int(g.group(1))) elif g.group(3): c = nearest(d, MON[g.group(3).lower()[:3]], int(g.group(4))) else: c = nearest(d, int(g.group(5)), int(g.group(6))) except (KeyError, ValueError): return None return None if c is None else (c - d).days def classify(text, date_utc): d = L.pdate(date_utc) for rx, kind in ((UTC_DATE_WORD, "word"), (UTC_DATE_NUM, "num")): m = rx.search(text) if m: if kind == "word": c = nearest(d, MON[m.group(4).lower()[:3]], int(m.group(3))) else: try: c = nearest(d, int(m.group(4)), int(m.group(5))) except ValueError: c = None if c is None: continue utc_off = (c - d).days # stated UTC date minus date_utc loc = local_date_offset(text, d, m.start()) return "stated", utc_off, loc m = UTC_TIME.search(text) if not m: return "none", None, None uh, um = int(m.group(1)), int(m.group(2)) if uh > 23 or um > 59: return "U3_unknown", None, None pre = text[max(0, m.start() - 60):m.start()] cands = list(LOCAL_T.finditer(pre)) if not cands: return "U3_unknown", None, None g = cands[-1] if g.group(0).lower() == "midnight": lh, lm = 0, 0 elif g.group(0).lower() == "noon": lh, lm = 12, 0 else: lh, lm = int(g.group(1)), int(g.group(2)) tail = pre[g.end():] ap = AMPM.match(tail) if ap: if ap.group(1).lower().startswith("p") and lh < 12: lh += 12 if ap.group(1).lower().startswith("a") and lh == 12: lh = 0 if lh > 23 or lm > 59: return "U3_unknown", None, None delta = (uh * 60 + um) - (lh * 60 + lm) cross = 1 if delta < -12 * 60 else (-1 if delta > 12 * 60 else 0) loc = local_date_offset(text, d, max(0, m.start() - 60) + g.start()) # utc_off relative to date_utc: if the local date is known (offset loc from date_utc), UTC date = local + cross utc_off = None if loc is None else loc + cross return "U3_cross%+d" % cross, utc_off, loc def main(): ev = L.load_events() out = [] cnt = Counter() for e in ev: text = e["description"] + " || " + e["cause_text"] c, utc_off, loc = classify(text, e["date_utc"]) if c == "stated": is_ = "UTC" if utc_off == 0 else ("local" if loc == 0 else "other/unknown") c2 = "stated_same" if utc_off == 0 else "stated_differs" elif c.startswith("U3_cross"): cross = int(c[8:]) if cross == 0: is_ = "same either way" elif loc is None: is_ = "unknown (no local date)" elif loc == 0: is_ = "local" elif loc + cross == 0: is_ = "UTC" else: is_ = "other" c2 = c else: is_ = "-" c2 = c cnt[(c2, is_)] += 1 out.append((e["sheet_row"], c2, "" if utc_off is None else utc_off, "" if loc is None else loc, is_)) with open(os.path.join(L.RES, "i6b.csv"), "w", encoding="utf-8", newline="\n") as f: w = csv.writer(f, lineterminator="\n") w.writerow(["sheet_row", "class", "utc_date_minus_date_utc", "local_date_minus_date_utc", "date_utc_is"]) for r in sorted(out, key=lambda x: int(x[0])): w.writerow(r) for k, v in sorted(cnt.items()): print(k, v) if __name__ == "__main__": main()