Skip to content

Commit d2cd4c0

Browse files
committed
Change relative date's format to further simplify it
A relative date doesn't need to be prefixed by @ anymore. The relative date pattern now displays named groups. Digits have been change to [0-9] to avoid other digit characters. Removed the @ character in tests. Updated subsequent documentation.
1 parent f0aca5e commit d2cd4c0

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

beets/dbcore/query.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -554,22 +554,25 @@ def parse(cls, string):
554554
relative.
555555
An absolute date has to be like one of the date_formats '%Y' or '%Y-%m'
556556
or '%Y-%m-%d'
557-
A relative date begins by '@ 'and has to follow the pattern_dq format
558-
'@([+|-]?)(\d+)([y|m|w|d])'
559-
- '@' indicates it's a date relative to now()
560-
- the optional '+' or '-' sign, which defaults to '+' will increment or
561-
decrement now() by a certain quantity
562-
- that quantity can be expressed in days, weeks, months or years
563-
respectively 'd', 'w', 'm', 'y'
564-
Please note that this relative calculation is rather approximate as it
565-
makes the assumption of 30 days per month and 365 days per year
557+
A relative date consists of three parts:
558+
- a ``+`` or ``-`` sign is optional and defaults to ``+``. The ``+``
559+
sign will add a time quantity to the current date while the ``-`` sign
560+
will do the opposite
561+
- a number follows and indicates the amount to add or substract
562+
- a final letter ends and represents the amount in either days, weeks,
563+
months or years (``d``, ``w``, ``m`` or ``y``)
564+
Please note that this relative calculation makes the assumption of 30
565+
days per month and 365 days per year.
566566
"""
567-
pattern_dq = '@([+|-]?)(\d+)([y|m|w|d])'
567+
568+
pattern_dq = '(?P<sign>[+|-]?)(?P<quantity>[0-9]+)(?P<timespan>[y|m|w|d])' # noqa: E501
568569
match_dq = re.match(pattern_dq, string)
570+
# test if the string matches the relative date pattern, add the parsed
571+
# quantity to now in that case
569572
if match_dq is not None:
570-
sign = match_dq.group(1)
571-
quantity = match_dq.group(2)
572-
timespan = match_dq.group(3)
573+
sign = match_dq.group('sign')
574+
quantity = match_dq.group('quantity')
575+
timespan = match_dq.group('timespan')
573576
multiplier = -1 if sign == '-' else 1
574577
days = cls.relative[timespan]
575578
date = datetime.now() + multiplier * timedelta(

docs/reference/query.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ You can also use relative dates to the current time.
166166
A relative date begins with an ``@``.
167167
It looks like ``@-3w``, ``@2m`` or ``@-4d`` which means the date 3 weeks ago,
168168
the date 2 months from now and the date 4 days ago.
169-
A relative date consists of four parts:
170-
- ``@`` indicates it's a date relative from now
169+
A relative date consists of three parts:
171170
- ``+`` or ``-`` sign is optional and defaults to ``+``. The ``+`` sign will
172171
add a time quantity to the current date while the ``-`` sign will do the
173172
opposite
@@ -180,11 +179,11 @@ month and 365 days per year.
180179

181180
Here is an example that finds all the albums added between now and last week::
182181

183-
$ beet ls -a 'added:@-1w..'
182+
$ beet ls -a 'added:-1w..'
184183

185-
Find all items added in a 2 weeks period 4 weeks ago::
184+
Find all items added in a 2 weeks period 4 weeks ago::
186185

187-
$ beet ls -a 'added:@-6w..@-4w'
186+
$ beet ls -a 'added:-6w..-4w'
188187

189188
Date *intervals*, like the numeric intervals described above, are separated by
190189
two dots (``..``). You can specify a start, an end, or both.

test/test_datequery.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def test_year_precision_intervals(self):
4747
self.assertContains('..2001', '2001-12-31T23:59:59')
4848
self.assertExcludes('..2001', '2002-01-01T00:00:00')
4949

50-
self.assertContains('@-1d..@1d', _datepattern(datetime.now()))
51-
self.assertExcludes('@-2d..@-1d', _datepattern(datetime.now()))
50+
self.assertContains('-1d..1d', _datepattern(datetime.now()))
51+
self.assertExcludes('-2d..-1d', _datepattern(datetime.now()))
5252

5353
def test_day_precision_intervals(self):
5454
self.assertContains('2000-06-20..2000-06-20', '2000-06-20T00:00:00')
@@ -168,37 +168,37 @@ def setUp(self):
168168

169169
def test_relative(self):
170170
for timespan in ['d', 'w', 'm', 'y']:
171-
query = DateQuery('added', '@-4' + timespan + '..@+4' + timespan)
171+
query = DateQuery('added', '-4' + timespan + '..+4' + timespan)
172172
matched = self.lib.items(query)
173173
self.assertEqual(len(matched), 1)
174174

175175
def test_relative_fail(self):
176176
for timespan in ['d', 'w', 'm', 'y']:
177-
query = DateQuery('added', '@-2' + timespan + '..@-1' + timespan)
177+
query = DateQuery('added', '-2' + timespan + '..-1' + timespan)
178178
matched = self.lib.items(query)
179179
self.assertEqual(len(matched), 0)
180180

181181
def test_start_relative(self):
182182
for timespan in ['d', 'w', 'm', 'y']:
183-
query = DateQuery('added', '@-4' + timespan + '..')
183+
query = DateQuery('added', '-4' + timespan + '..')
184184
matched = self.lib.items(query)
185185
self.assertEqual(len(matched), 1)
186186

187187
def test_start_relative_fail(self):
188188
for timespan in ['d', 'w', 'm', 'y']:
189-
query = DateQuery('added', '@4' + timespan + '..')
189+
query = DateQuery('added', '4' + timespan + '..')
190190
matched = self.lib.items(query)
191191
self.assertEqual(len(matched), 0)
192192

193193
def test_end_relative(self):
194194
for timespan in ['d', 'w', 'm', 'y']:
195-
query = DateQuery('added', '..@+4' + timespan)
195+
query = DateQuery('added', '..+4' + timespan)
196196
matched = self.lib.items(query)
197197
self.assertEqual(len(matched), 1)
198198

199199
def test_end_relative_fail(self):
200200
for timespan in ['d', 'w', 'm', 'y']:
201-
query = DateQuery('added', '..@-4' + timespan)
201+
query = DateQuery('added', '..-4' + timespan)
202202
matched = self.lib.items(query)
203203
self.assertEqual(len(matched), 0)
204204

0 commit comments

Comments
 (0)