Skip to content

Commit 62dee53

Browse files
authored
Fix is_xs_date* in Python to match first (#307)
We make the verification functions ``is_xs_date*`` to match against the patterns first to avoid bugs related to unexpected date/time formats.
1 parent 6a2d079 commit 62dee53

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

test_data/python/test_main/aas_core_meta.v3rc2/expected_output/verification.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,15 +1274,14 @@ def _is_leap_year(year: int) -> bool:
12741274

12751275

12761276
def is_xs_date(value: str) -> bool:
1277-
"""
1278-
Check that :paramref:`value` is a valid ``xs:date``.
1279-
1280-
We can not use :py:func:`datetime.date.strptime` as it does not
1281-
handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
1282-
"""
1283-
if matches_xs_date(value) is False:
1277+
"""Check that :paramref:`value` is a valid ``xs:date``."""
1278+
if not matches_xs_date(value):
12841279
return False
12851280

1281+
# NOTE (mristin, 2022-11-23):
1282+
# We can not use :py:func:`datetime.datetime.strptime` as it does not
1283+
# handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
1284+
12861285
# NOTE (mristin, 2022-10-30):
12871286
# We need to match the prefix as zone offsets are allowed in the dates. Optimally,
12881287
# we would re-use the pattern matching from :py:func`matches_xs_date`, but this
@@ -1312,39 +1311,34 @@ def is_xs_date(value: str) -> bool:
13121311
else:
13131312
max_days = _DAYS_IN_MONTH[month]
13141313

1315-
# We accept the zero year for astronomical settings,
1316-
# see: https://en.wikipedia.org/wiki/Year_zero
1317-
13181314
if day > max_days:
13191315
return False
13201316

13211317
return True
13221318

13231319

13241320
def is_xs_date_time(value: str) -> bool:
1325-
"""
1326-
Check that :paramref:`value` is a valid ``xs:dateTime``.
1321+
"""Check that :paramref:`value` is a valid ``xs:dateTime``."""
1322+
# NOTE (mristin, 2022-11-23):
1323+
# We can not use :py:func:`datetime.datetime.strptime` as it does not
1324+
# handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
13271325

1328-
We can not use :py:func:`datetime.datetime.strptime` as it does not
1329-
handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
1330-
"""
1331-
if matches_xs_date_time(value) is False:
1326+
if not matches_xs_date_time(value):
13321327
return False
13331328

13341329
date, _ = value.split('T')
13351330
return is_xs_date(date)
13361331

13371332

13381333
def is_xs_date_time_stamp(value: str) -> bool:
1339-
"""
1340-
Check that :paramref:`value` is a valid ``xs:dateTimeStamp``.
1341-
1342-
We can not use :py:func:`datetime.datetime.strptime` as it does not
1343-
handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
1344-
"""
1345-
if matches_xs_date_time_stamp(value) is False:
1334+
"""Check that :paramref:`value` is a valid ``xs:dateTimeStamp``."""
1335+
if not matches_xs_date_time_stamp(value):
13461336
return False
13471337

1338+
# NOTE (mristin, 2022-11-23):
1339+
# We can not use :py:func:`datetime.datetime.strptime` as it does not
1340+
# handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
1341+
13481342
date, _ = value.split('T')
13491343
return is_xs_date(date)
13501344

test_data/python/test_main/aas_core_meta.v3rc2/input/snippets/Verification/value_consistent_with_xsd_type.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ def _is_leap_year(year: int) -> bool:
5959

6060

6161
def is_xs_date(value: str) -> bool:
62-
"""
63-
Check that :paramref:`value` is a valid ``xs:date``.
64-
65-
We can not use :py:func:`datetime.date.strptime` as it does not
66-
handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
67-
"""
68-
if matches_xs_date(value) is False:
62+
"""Check that :paramref:`value` is a valid ``xs:date``."""
63+
if not matches_xs_date(value):
6964
return False
7065

66+
# NOTE (mristin, 2022-11-23):
67+
# We can not use :py:func:`datetime.datetime.strptime` as it does not
68+
# handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
69+
7170
# NOTE (mristin, 2022-10-30):
7271
# We need to match the prefix as zone offsets are allowed in the dates. Optimally,
7372
# we would re-use the pattern matching from :py:func`matches_xs_date`, but this
@@ -97,39 +96,34 @@ def is_xs_date(value: str) -> bool:
9796
else:
9897
max_days = _DAYS_IN_MONTH[month]
9998

100-
# We accept the zero year for astronomical settings,
101-
# see: https://en.wikipedia.org/wiki/Year_zero
102-
10399
if day > max_days:
104100
return False
105101

106102
return True
107103

108104

109105
def is_xs_date_time(value: str) -> bool:
110-
"""
111-
Check that :paramref:`value` is a valid ``xs:dateTime``.
106+
"""Check that :paramref:`value` is a valid ``xs:dateTime``."""
107+
# NOTE (mristin, 2022-11-23):
108+
# We can not use :py:func:`datetime.datetime.strptime` as it does not
109+
# handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
112110

113-
We can not use :py:func:`datetime.datetime.strptime` as it does not
114-
handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
115-
"""
116-
if matches_xs_date_time(value) is False:
111+
if not matches_xs_date_time(value):
117112
return False
118113

119114
date, _ = value.split('T')
120115
return is_xs_date(date)
121116

122117

123118
def is_xs_date_time_stamp(value: str) -> bool:
124-
"""
125-
Check that :paramref:`value` is a valid ``xs:dateTimeStamp``.
126-
127-
We can not use :py:func:`datetime.datetime.strptime` as it does not
128-
handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
129-
"""
130-
if matches_xs_date_time_stamp(value) is False:
119+
"""Check that :paramref:`value` is a valid ``xs:dateTimeStamp``."""
120+
if not matches_xs_date_time_stamp(value):
131121
return False
132122

123+
# NOTE (mristin, 2022-11-23):
124+
# We can not use :py:func:`datetime.datetime.strptime` as it does not
125+
# handle years below 1000 correctly on Windows (*e.g.*, ``-999-01-01``).
126+
133127
date, _ = value.split('T')
134128
return is_xs_date(date)
135129

0 commit comments

Comments
 (0)