summaryrefslogtreecommitdiff
path: root/rdflib/plugins
diff options
context:
space:
mode:
authorFlorian Ludwig <f.ludwig@greyrook.com>2020-08-31 18:09:48 +0200
committerFlorian Ludwig <f.ludwig@greyrook.com>2020-10-23 20:35:20 +0200
commit2a2bb0d3b2f2a89b00ed57e087cf658ea0cc4abf (patch)
tree72806e45bfcdf4b0e31257146512a9cfa0089a73 /rdflib/plugins
parentdde9db804d30fe0ab0c3291c105093ed691b0ef4 (diff)
downloadrdflib-2a2bb0d3b2f2a89b00ed57e087cf658ea0cc4abf.tar.gz
support day, nonth and year function for date
Diffstat (limited to 'rdflib/plugins')
-rw-r--r--rdflib/plugins/sparql/operators.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/rdflib/plugins/sparql/operators.py b/rdflib/plugins/sparql/operators.py
index 29bdd5c0..560baba6 100644
--- a/rdflib/plugins/sparql/operators.py
+++ b/rdflib/plugins/sparql/operators.py
@@ -12,6 +12,7 @@ import math
import random
import uuid
import hashlib
+import datetime as py_datetime # naming conflict with function within this module
from functools import reduce
@@ -449,17 +450,17 @@ def Builtin_NOW(e, ctx):
def Builtin_YEAR(e, ctx):
- d = datetime(e.arg)
+ d = date(e.arg)
return Literal(d.year)
def Builtin_MONTH(e, ctx):
- d = datetime(e.arg)
+ d = date(e.arg)
return Literal(d.month)
def Builtin_DAY(e, ctx):
- d = datetime(e.arg)
+ d = date(e.arg)
return Literal(d.day)
@@ -998,6 +999,18 @@ def datetime(e):
return e.toPython()
+def date(e) -> py_datetime.date:
+ if not isinstance(e, Literal):
+ raise SPARQLError("Non-literal passed as date: %r" % e)
+ if e.datatype not in (XSD.date, XSD.dateTime):
+ raise SPARQLError(
+ "Literal with wrong datatype passed as date: %r" % e)
+ result = e.toPython()
+ if isinstance(result, py_datetime.datetime):
+ return result.date()
+ return result
+
+
def string(s):
"""
Make sure the passed thing is a string literal