Skip to content

Commit

Permalink
Optional second default argument to functions, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 1, 2022
1 parent 090d9db commit 8356683
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ select

[Try that query](https://latest-with-plugins.datasette.io/fixtures?sql=select%0D%0A++dateutil_parse%28%2210+october+2020+3pm%22%29%2C%0D%0A++dateutil_parse_fuzzy%28%22This+is+due+10+september%22%29%2C%0D%0A++dateutil_parse%28%221%2F2%2F2020%22%29%2C%0D%0A++dateutil_parse%28%222020-03-04%22%29%2C%0D%0A++dateutil_parse_dayfirst%28%222020-03-04%22%29%3B)

### Optional default dates

The `dateutil_parse()`, `dateutil_parse_fuzzy()`, `dateutil_parse_dayfirst()` and `dateutil_parse_fuzzy_dayfirst()` functions all accept an optional second argument specifying a "default" datetime to consider if some of the details are missing. For example, the following:
```sql
select dateutil_parse('1st october', '1985-01-01')
```
Will return `1985-10-01T00:00:00` - the missing year is replaced with the year from the default date.

[Example query demonstrating the default date argument](https://latest-with-plugins.datasette.io/fixtures?sql=with+times+as+%28%0D%0A++select%0D%0A++++datetime%28%27now%27%29+as+t%0D%0A++union%0D%0A++select%0D%0A++++datetime%28%27now%27%2C+%27-1+year%27%29%0D%0A++union%0D%0A++select%0D%0A++++datetime%28%27now%27%2C+%27-3+years%27%29%0D%0A%29%0D%0Aselect+t%2C+dateutil_parse_fuzzy%28%22This+is+due+10+september%22%2C+t%29+from+times)

### Calculating Easter

- `dateutil_easter(year)` - returns the date for Easter in that year, for example `dateutil_easter("2020")` returns `2020-04-12`.
Expand Down
26 changes: 18 additions & 8 deletions datasette_dateutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@


def _dateutil_parse_shared(s, **kwargs):
default = kwargs.get("default")
if default is not None:
kwargs["default"] = parse(kwargs["default"])
if not kwargs.get("dayfirst"):
kwargs["dayfirst"] = False
if not s:
Expand All @@ -21,20 +24,20 @@ def _dateutil_parse_shared(s, **kwargs):
return None


def dateutil_parse(s):
return _dateutil_parse_shared(s)
def dateutil_parse(s, default=None):
return _dateutil_parse_shared(s, default=default)


def dateutil_parse_fuzzy(s):
return _dateutil_parse_shared(s, fuzzy=True)
def dateutil_parse_fuzzy(s, default=None):
return _dateutil_parse_shared(s, fuzzy=True, default=default)


def dateutil_parse_dayfirst(s):
return _dateutil_parse_shared(s, dayfirst=True)
def dateutil_parse_dayfirst(s, default=None):
return _dateutil_parse_shared(s, dayfirst=True, default=default)


def dateutil_parse_fuzzy_dayfirst(s):
return _dateutil_parse_shared(s, fuzzy=True, dayfirst=True)
def dateutil_parse_fuzzy_dayfirst(s, default=None):
return _dateutil_parse_shared(s, fuzzy=True, dayfirst=True, default=default)


def dateutil_easter(year):
Expand Down Expand Up @@ -102,6 +105,13 @@ def prepare_connection(conn):
conn.create_function(
"dateutil_parse_fuzzy_dayfirst", 1, dateutil_parse_fuzzy_dayfirst
)
# The two argument version of these (default date is second argument)
conn.create_function("dateutil_parse", 2, dateutil_parse)
conn.create_function("dateutil_parse_fuzzy", 2, dateutil_parse_fuzzy)
conn.create_function("dateutil_parse_dayfirst", 2, dateutil_parse_dayfirst)
conn.create_function(
"dateutil_parse_fuzzy_dayfirst", 2, dateutil_parse_fuzzy_dayfirst
)
conn.create_function("dateutil_easter", 1, dateutil_easter)
conn.create_function("dateutil_rrule", 1, dateutil_rrule)
conn.create_function("dateutil_rrule", 2, dateutil_rrule)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dateutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async def test_plugin_is_installed():
@pytest.mark.parametrize(
"sql,expected",
[
# dateutil_parse variants with one argument
("select dateutil_parse('1st october 2009')", "2009-10-01T00:00:00"),
("select dateutil_parse('invalid')", None),
("select dateutil_parse('due on 1st october 2009')", None),
Expand All @@ -32,8 +33,28 @@ async def test_plugin_is_installed():
"select dateutil_parse_fuzzy_dayfirst('due on 1/2/2003')",
"2003-02-01T00:00:00",
),
# dateutil_parse variants with the optional second default datetime argument
(
"select dateutil_parse('1st october 2009', '10th september 2020')",
"2009-10-01T00:00:00",
),
(
"select dateutil_parse('1st october', '10th september 2020')",
"2020-10-01T00:00:00",
),
(
"select dateutil_parse_fuzzy('due on 1st october', '2020-01-01')",
"2020-10-01T00:00:00",
),
("select dateutil_parse_dayfirst('1/2', '1981-01-01')", "1981-02-01T00:00:00"),
(
"select dateutil_parse_fuzzy_dayfirst('due on 1/2', '1765-01-01')",
"1765-02-01T00:00:00",
),
# dateutil_easter
("select dateutil_easter(2020)", "2020-04-12"),
("select dateutil_easter('invalid')", None),
# dateutil_rrule
(
"select dateutil_rrule('DTSTART:20200101\nFREQ=DAILY;INTERVAL=10;COUNT=5')",
'["2020-01-01T00:00:00", "2020-01-11T00:00:00", "2020-01-21T00:00:00", "2020-01-31T00:00:00", "2020-02-10T00:00:00"]',
Expand All @@ -50,6 +71,7 @@ async def test_plugin_is_installed():
"select dateutil_rrule_date('FREQ=DAILY;INTERVAL=10;COUNT=5', '2020-01-01')",
'["2020-01-01", "2020-01-11", "2020-01-21", "2020-01-31", "2020-02-10"]',
),
# dateutil_dates_between
(
"select dateutil_dates_between('1 january 2020', '5 jan 2020', 0)",
'["2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04"]',
Expand Down

0 comments on commit 8356683

Please sign in to comment.