Skip to content

Commit 24acafe

Browse files
committed
Handle iso8601 month start and end logic
1 parent 66921e1 commit 24acafe

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

terminusdb_client/woqlquery/woql_query.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,109 @@ def sequence(self, value, start, end, step=None, count=None):
22962296
self._cursor["count"] = self._clean_object(count)
22972297
return self
22982298

2299+
def month_start_date(self, year_month, date):
2300+
"""Computes the first day of the month for a given xsd:gYearMonth.
2301+
2302+
Parameters
2303+
----------
2304+
year_month : str or dict
2305+
a gYearMonth value (e.g. 2024-01) or variable
2306+
date : str or dict
2307+
the resulting xsd:date or variable
2308+
2309+
Returns
2310+
-------
2311+
WOQLQuery object
2312+
query object that can be chained and/or execute
2313+
"""
2314+
if year_month is None or date is None:
2315+
raise ValueError("MonthStartDate takes two parameters")
2316+
if self._cursor.get("@type"):
2317+
self._wrap_cursor_with_and()
2318+
self._cursor["@type"] = "MonthStartDate"
2319+
self._cursor["year_month"] = self._clean_object(year_month)
2320+
self._cursor["date"] = self._clean_object(date)
2321+
return self
2322+
2323+
def month_end_date(self, year_month, date):
2324+
"""Computes the last day of the month for a given xsd:gYearMonth.
2325+
Handles leap years correctly.
2326+
2327+
Parameters
2328+
----------
2329+
year_month : str or dict
2330+
a gYearMonth value (e.g. 2024-02) or variable
2331+
date : str or dict
2332+
the resulting xsd:date or variable
2333+
2334+
Returns
2335+
-------
2336+
WOQLQuery object
2337+
query object that can be chained and/or execute
2338+
"""
2339+
if year_month is None or date is None:
2340+
raise ValueError("MonthEndDate takes two parameters")
2341+
if self._cursor.get("@type"):
2342+
self._wrap_cursor_with_and()
2343+
self._cursor["@type"] = "MonthEndDate"
2344+
self._cursor["year_month"] = self._clean_object(year_month)
2345+
self._cursor["date"] = self._clean_object(date)
2346+
return self
2347+
2348+
def month_start_dates(self, date, start, end):
2349+
"""Generator: produces every first-of-month date in [start, end).
2350+
2351+
Parameters
2352+
----------
2353+
date : str
2354+
variable for the generated first-of-month date
2355+
start : str or dict
2356+
the inclusive start date
2357+
end : str or dict
2358+
the exclusive end date
2359+
2360+
Returns
2361+
-------
2362+
WOQLQuery object
2363+
query object that can be chained and/or execute
2364+
"""
2365+
if date is None or start is None or end is None:
2366+
raise ValueError("MonthStartDates takes three parameters")
2367+
if self._cursor.get("@type"):
2368+
self._wrap_cursor_with_and()
2369+
self._cursor["@type"] = "MonthStartDates"
2370+
self._cursor["date"] = self._clean_object(date)
2371+
self._cursor["start"] = self._clean_object(start)
2372+
self._cursor["end"] = self._clean_object(end)
2373+
return self
2374+
2375+
def month_end_dates(self, date, start, end):
2376+
"""Generator: produces every last-of-month date in [start, end).
2377+
2378+
Parameters
2379+
----------
2380+
date : str
2381+
variable for the generated last-of-month date
2382+
start : str or dict
2383+
the inclusive start date
2384+
end : str or dict
2385+
the exclusive end date
2386+
2387+
Returns
2388+
-------
2389+
WOQLQuery object
2390+
query object that can be chained and/or execute
2391+
"""
2392+
if date is None or start is None or end is None:
2393+
raise ValueError("MonthEndDates takes three parameters")
2394+
if self._cursor.get("@type"):
2395+
self._wrap_cursor_with_and()
2396+
self._cursor["@type"] = "MonthEndDates"
2397+
self._cursor["date"] = self._clean_object(date)
2398+
self._cursor["start"] = self._clean_object(start)
2399+
self._cursor["end"] = self._clean_object(end)
2400+
return self
2401+
22992402
def opt(self, query=None):
23002403
"""The Query in the Optional argument is specified as optional
23012404

0 commit comments

Comments
 (0)