support day, nonth and year function for date · RDFLib/rdflib@51a17fd (original) (raw)
`@@ -12,6 +12,7 @@
`
12
12
`import random
`
13
13
`import uuid
`
14
14
`import hashlib
`
``
15
`+
import datetime as py_datetime # naming conflict with function within this module
`
15
16
``
16
17
`from functools import reduce
`
17
18
``
`@@ -448,17 +449,17 @@ def Builtin_NOW(e, ctx):
`
448
449
``
449
450
``
450
451
`def Builtin_YEAR(e, ctx):
`
451
``
`-
d = datetime(e.arg)
`
``
452
`+
d = date(e.arg)
`
452
453
`return Literal(d.year)
`
453
454
``
454
455
``
455
456
`def Builtin_MONTH(e, ctx):
`
456
``
`-
d = datetime(e.arg)
`
``
457
`+
d = date(e.arg)
`
457
458
`return Literal(d.month)
`
458
459
``
459
460
``
460
461
`def Builtin_DAY(e, ctx):
`
461
``
`-
d = datetime(e.arg)
`
``
462
`+
d = date(e.arg)
`
462
463
`return Literal(d.day)
`
463
464
``
464
465
``
`@@ -955,6 +956,18 @@ def datetime(e):
`
955
956
`return e.toPython()
`
956
957
``
957
958
``
``
959
`+
def date(e) -> py_datetime.date:
`
``
960
`+
if not isinstance(e, Literal):
`
``
961
`+
raise SPARQLError("Non-literal passed as date: %r" % e)
`
``
962
`+
if e.datatype not in (XSD.date, XSD.dateTime):
`
``
963
`+
raise SPARQLError(
`
``
964
`+
"Literal with wrong datatype passed as date: %r" % e)
`
``
965
`+
result = e.toPython()
`
``
966
`+
if isinstance(result, py_datetime.datetime):
`
``
967
`+
return result.date()
`
``
968
`+
return result
`
``
969
+
``
970
+
958
971
`def string(s):
`
959
972
`"""
`
960
973
` Make sure the passed thing is a string literal
`