Skip to content

Commit f7609e6

Browse files
committed
fix(python): prevent year 0 timestamp issue
relates to STACKITSDK-202 and stackitcloud/stackit-sdk-python#2039
1 parent 588f4f4 commit f7609e6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

templates/python/model_generic.mustache

Lines changed: 16 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,21 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
9192
{{/isContainer}}
9293
return value
9394
{{/isEnum}}
95+
96+
{{! BEGIN OF WORKAROUND - YEAR 0 ISSUE - see }}
97+
{{#isDateTime}}
98+
@field_validator('{{{name}}}', mode='before')
99+
def {{{name}}}_change_year_zero_to_one(cls, value):
100+
"""Workaround which prevents year 0 issue"""
101+
if isinstance(value, str):
102+
# Check for year "0000" at the beginning of the string
103+
# This assumes common date formats like YYYY-MM-DDTHH:MM:SS+00:00 or YYYY-MM-DDTHH:MM:SSZ
104+
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):
105+
# Workaround: Replace "0000" with "0001"
106+
return "0001" + value[4:] # Take "0001" and append the rest of the string
107+
return value
108+
{{/isDateTime}}
109+
{{! END OF WORKAROUND - YEAR 0 ISSUE }}
94110
{{/vars}}
95111

96112
model_config = ConfigDict(

0 commit comments

Comments
 (0)