]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - scripts/check-datetimes.py
examples: explore project work through native Topic Maps (slice 22U)
[topic-maps.git] / scripts / check-datetimes.py
1 #!/usr/bin/env python3
2 """Independent integer/calendar/string DateTime oracle; no float or local timezone."""
3 import calendar
4 import datetime
5 import json
6 from pathlib import Path
7 import re
8 import sys
9 import xml.etree.ElementTree as ET
10 if hasattr(sys, 'set_int_max_str_digits'):
11 sys.set_int_max_str_digits(0)
12 root = Path(__file__).resolve().parent.parent
13 r = json.loads(Path(sys.argv[1]).read_text())
14 assert r['kind'] == 'datetime-semantic-and-cxtm-evidence' and r['failed'] == 0
15 assert len(r['checks']) == len(set(r['checks'])) == r['passed']
16 values = ['1976-09-19T12:01:01', '1976-09-19T12:02:00', '2011-01-01T00:00:00.001', '2011-01-01T00:00:00Z', '2010-12-31T22:00:00Z']
17 for c, value in zip(r['cases'], values):
18 assert c['ctmSource'].encode() == (root / 'vendor/cxtm' / c['id']).read_bytes()
19 items = [o for t in c['actualObjects']['topics'] for o in t['occurrences']]
20 assert len(items) == 1 and items[0]['value'] == value
21 assert items[0]['datatype'] == 'http://www.w3.org/2001/XMLSchema#dateTime'
22 assert c['status'] == ('fail' if c['id'].endswith(('datetime2.ctm', 'timezone2.ctm')) else 'pass')
23 assert len(r['cases']) == 5
24
25
26 def canonical(text):
27 m = re.fullmatch(r'(-?[0-9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]+))?(Z|[+-]00:00)?', text)
28 assert m, 'oracle only called for supported lexical probes'
29 year, month, day, hour, minute, second, fraction, zone = m.groups()
30 y, mo, d, h, mi, se = map(int, (year, month, day, hour, minute, second))
31 fraction = (fraction or '').rstrip('0')
32 leap = se == 60 and zone and h == 23 and mi == 59 and (mo, d) in [(3,31), (6,30), (9,30), (12,31)]
33 if se == 60 and not leap:
34 assert (mo, d) not in [(3,31), (4,1), (6,30), (7,1), (9,30), (10,1), (12,31), (1,1)]
35 se = 0; mi += 1
36 if mi == 60:
37 mi = 0; h += 1
38 if h == 24:
39 h = 0; d += 1
40 days = [31, 29 if calendar.isleap(y) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][mo-1]
41 if d > days:
42 d = 1; mo += 1
43 if mo == 13:
44 mo = 1; y += 1
45 if y == 0: y = 1
46 ys = ('-' if y < 0 else '') + str(abs(y)).zfill(4)
47 return f'{ys}-{mo:02d}-{d:02d}T{h:02d}:{mi:02d}:{se:02d}' + ('.'+fraction if fraction else '') + ('Z' if zone else '')
48
49
50 for p in r['lexicalProbes']:
51 expected = canonical(p['input'])
52 assert expected == p['canonicalValue']
53 assert p['storedValue'] == (expected if p['mode'] == 'bare' else ' '+p['input']+'\t' if p['mode'] == 'qualified' else p['input'])
54 items = ET.fromstring(p['actualCXTM']).findall('topic/occurrence')
55 assert len(items) == 1 and items[0].findtext('value') == expected
56 assert items[0].findtext('datatype') == 'http://www.w3.org/2001/XMLSchema#dateTime'
57 offset = r['timezoneObservation']
58 assert offset == r['cases'][-1] and offset['status'] == 'fail'
59 assert [x['contractStatus'] for x in r['normativeDiscrepancies']] == ['fail', 'fail']
60 assert datetime.datetime(2011,1,1,tzinfo=datetime.timezone(datetime.timedelta(hours=2))).astimezone(datetime.timezone.utc).isoformat() == '2010-12-31T22:00:00+00:00'
61 print(f'DATETIME-INDEPENDENT-PASS: {len(r["cases"])} original model observations; {len(r["lexicalProbes"])} exact calendar/fraction probes; 2 documented discrepancies')