Skip to content

model.datatypes: Fix xs:gYear and xs:gYearMonth rejecting valid XSD year values #566

@s-heppner

Description

@s-heppner

Currently, _parse_xsd_gyear and _parse_xsd_gyearmonth in basyx/aas/model/datatypes.py reject any xs:gYear or xs:gYearMonth
value whose year has more than 4 digits or starts with a negative sign. For example, "-2001" (valid xs:gYear), "20000" (valid xs:gYear),
"-2001-10" (valid xs:gYearMonth), and "-20000-04" (valid xs:gYearMonth) all raise:

ValueError: Value is not a valid XSD GYear/GYearMonth string

The root cause is in the two regexes at lines 639–642:

GYEAR_RE      = re.compile(r'^(\d\d\d\d)([+\-]\d\d:\d\d|Z)?$')
GYEARMONTH_RE = re.compile(r'^(\d\d\d\d)-(\d\d)([+\-]\d\d:\d\d|Z)?$')

Both only match exactly 4-digit, non-negative years. The XSD specification for xs:gYear and xs:gYearMonth allows years with at least 4 digits
and an optional leading minus sign.

Notably, GYear and GYearMonth store year as a plain Python int, so there is no representation barrier here (unlike xs:date, #565), which is bounded by datetime.date's year range. The fix is purely in the regexes and the sign handling in the two parser functions.
_parse_xsd_gyear and _parse_xsd_gyearmonth then need to apply the sign from match[1] and use the renumbered capture groups.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsdkSomething to do with the `sdk` package

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions