Redo XSD Datetime, Date, Time, Duration parser and serializers (#2929) · RDFLib/rdflib@9c469b5 (original) (raw)
`@@ -21,7 +21,6 @@
`
21
21
`from typing import Any, Callable, Dict, NoReturn, Optional, Tuple, Union, overload
`
22
22
`from urllib.parse import quote
`
23
23
``
24
``
`-
import isodate
`
25
24
`from pyparsing import ParseResults
`
26
25
``
27
26
`from rdflib.namespace import RDF, XSD
`
`@@ -47,6 +46,7 @@
`
47
46
`URIRef,
`
48
47
`Variable,
`
49
48
`)
`
``
49
`+
from rdflib.xsd_datetime import Duration, parse_datetime # type: ignore[attr-defined]
`
50
50
``
51
51
``
52
52
`def Builtin_IRI(expr: Expr, ctx: FrozenBindings) -> URIRef:
`
`@@ -521,8 +521,13 @@ def Builtin_TZ(e: Expr, ctx) -> Literal:
`
521
521
`if not d.tzinfo:
`
522
522
`return Literal("")
`
523
523
`n = d.tzinfo.tzname(d)
`
524
``
`-
if n == "UTC":
`
``
524
`+
if n is None:
`
``
525
`+
n = ""
`
``
526
`+
elif n == "UTC":
`
525
527
`n = "Z"
`
``
528
`+
elif n.startswith("UTC"):
`
``
529
`+
Replace tzname like "UTC-05:00" with simply "-05:00" to match Jena tz fn
`
``
530
`+
n = n[3:]
`
526
531
`return Literal(n)
`
527
532
``
528
533
``
`@@ -687,7 +692,7 @@ def default_cast(e: Expr, ctx: FrozenBindings) -> Literal: # type: ignore[retur
`
687
692
`if x.datatype and x.datatype not in (XSD.dateTime, XSD.string):
`
688
693
`raise SPARQLError("Cannot cast %r to XSD:dateTime" % x.datatype)
`
689
694
`try:
`
690
``
`-
return Literal(isodate.parse_datetime(x), datatype=e.iri)
`
``
695
`+
return Literal(parse_datetime(x), datatype=e.iri)
`
691
696
`except: # noqa: E722
`
692
697
`raise SPARQLError("Cannot interpret '%r' as datetime" % x)
`
693
698
``
`@@ -1085,7 +1090,7 @@ def dateTimeObjects(expr: Literal) -> Any:
`
1085
1090
`def isCompatibleDateTimeDatatype( # type: ignore[return]
`
1086
1091
`obj1: Union[py_datetime.date, py_datetime.datetime],
`
1087
1092
`dt1: URIRef,
`
1088
``
`-
obj2: Union[isodate.Duration, py_datetime.timedelta],
`
``
1093
`+
obj2: Union[Duration, py_datetime.timedelta],
`
1089
1094
`dt2: URIRef,
`
1090
1095
`) -> bool:
`
1091
1096
`"""
`
`@@ -1098,7 +1103,7 @@ def isCompatibleDateTimeDatatype( # type: ignore[return]
`
1098
1103
`return True
`
1099
1104
`elif dt2 == XSD.dayTimeDuration or dt2 == XSD.Duration:
`
1100
1105
`# checking if the dayTimeDuration has no Time Component
`
1101
``
`-
else it wont be compatible with Date Literal
`
``
1106
`+
else it won't be compatible with Date Literal
`
1102
1107
`if "T" in str(obj2):
`
1103
1108
`return False
`
1104
1109
`else:
`
`@@ -1110,7 +1115,7 @@ def isCompatibleDateTimeDatatype( # type: ignore[return]
`
1110
1115
`elif dt2 == XSD.dayTimeDuration or dt2 == XSD.Duration:
`
1111
1116
`# checking if the dayTimeDuration has no Date Component
`
1112
1117
`# (by checking if the format is "PT...." )
`
1113
``
`-
else it wont be compatible with Time Literal
`
``
1118
`+
else it won't be compatible with Time Literal
`
1114
1119
`if "T" == str(obj2)[1]:
`
1115
1120
`return True
`
1116
1121
`else:
`
`@@ -1139,7 +1144,7 @@ def calculateDuration(
`
1139
1144
`def calculateFinalDateTime(
`
1140
1145
`obj1: Union[py_datetime.date, py_datetime.datetime],
`
1141
1146
`dt1: URIRef,
`
1142
``
`-
obj2: Union[isodate.Duration, py_datetime.timedelta],
`
``
1147
`+
obj2: Union[Duration, py_datetime.timedelta],
`
1143
1148
`dt2: URIRef,
`
1144
1149
`operation: str,
`
1145
1150
`) -> Literal:
`