Skip to content

Commit 37e371f

Browse files
authored
fix(python): prevent year 0 timestamp issue (#197)
relates to STACKITSDK-202 and stackitcloud/stackit-sdk-python#2039
1 parent 082f924 commit 37e371f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

templates/python/model_generic.mustache

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import json
99
{{#vendorExtensions.x-py-model-imports}}
1010
{{{.}}}
1111
{{/vendorExtensions.x-py-model-imports}}
12+
from pydantic import field_validator
1213
from typing import Optional, Set
1314
from typing_extensions import Self
1415

@@ -91,6 +92,22 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
9192
{{/isContainer}}
9293
return value
9394
{{/isEnum}}
95+
96+
{{! BEGIN OF WORKAROUND - YEAR 0 ISSUE - see STACKITSDK-202 }}
97+
{{! Workaround should be removed during work on STACKITSDK-260 }}
98+
{{#isDateTime}}
99+
@field_validator('{{{name}}}', mode='before')
100+
def {{{name}}}_change_year_zero_to_one(cls, value):
101+
"""Workaround which prevents year 0 issue"""
102+
if isinstance(value, str):
103+
# Check for year "0000" at the beginning of the string
104+
# This assumes common date formats like YYYY-MM-DDTHH:MM:SS+00:00 or YYYY-MM-DDTHH:MM:SSZ
105+
if value.startswith("0000-01-01T") and re.match(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\+\d{2}:\d{2}|Z)$', value):
106+
# Workaround: Replace "0000" with "0001"
107+
return "0001" + value[4:] # Take "0001" and append the rest of the string
108+
return value
109+
{{/isDateTime}}
110+
{{! END OF WORKAROUND - YEAR 0 ISSUE }}
94111
{{/vars}}
95112

96113
model_config = ConfigDict(

0 commit comments

Comments
 (0)