unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH] search: expand "d:" to "dt:" for precision with approxidate
@ 2021-04-16 23:43 Eric Wong
  2021-04-17  0:00 ` [SQUASH] fix "d:YYYYMMDD..$NEEDS_APPROXIDATE" case Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2021-04-16 23:43 UTC (permalink / raw)
  To: meta

If a user specifies "d:" with a higher precision than it was
traditionally able to handle, switch transparently to "dt:".
This lowers the learning curve and improves DWIM-ness.
---
 lib/PublicInbox/Search.pm | 18 +++++++++++++++---
 t/search.t                |  9 +++++----
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index ab04d430..e0e8164d 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -275,7 +275,9 @@ sub date_parse_prepare {
 
 	# expand "d:20101002" => "d:20101002..20101003" and like
 	# n.b. git doesn't do YYYYMMDD w/o '-', it needs YYYY-MM-DD
+	# We upgrade "d:" to "dt:" to iff using approxidate
 	if ($pfx eq 'd') {
+		my $fmt = "\0%Y%m%d";
 		if (!defined($r[1])) {
 			if ($r[0] =~ /\A([0-9]{4})([0-9]{2})([0-9]{2})\z/) {
 				push @$to_parse, "$1-$2-$3";
@@ -283,14 +285,24 @@ sub date_parse_prepare {
 				# to parse anyways for "d+" below
 			} else {
 				push @$to_parse, $r[0];
+				if ($r[0] !~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/) {
+					$pfx = 'dt';
+					$fmt = "\0%Y%m%d%H%M%S";
+				}
 			}
-			$r[0] = "\0%Y%m%d$#$to_parse\0";
-			$r[1] = "\0%Y%m%d+\0";
+			$r[0] = "$fmt+$#$to_parse\0";
+			$r[1] = "$fmt+\0";
 		} else {
 			for my $x (@r) {
 				next if $x eq '' || $x =~ /\A[0-9]{8}\z/;
 				push @$to_parse, $x;
-				$x = "\0%Y%m%d$#$to_parse\0";
+				if ($x !~ /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/) {
+					$pfx = 'dt';
+				}
+				$x = "$fmt$#$to_parse\0";
+			}
+			if ($pfx eq 'dt') {
+				s/\0%Y%m%d/\0%Y%m%d%H%M%S/ for @r;
 			}
 		}
 	} elsif ($pfx eq 'dt') {
diff --git a/t/search.t b/t/search.t
index 124c9acf..b63a7672 100644
--- a/t/search.t
+++ b/t/search.t
@@ -561,9 +561,9 @@ SKIP: {
 	is(strftime('%Y-%m-%d', gmtime($1//0)), '2010-10-02', 'rt: end expand');
 	$q = $s->query_argv_to_string($g, [qw(something dt:2010-10-02..)]);
 	like($q, qr/\Asomething dt:20101002\d{6}\.\./, 'dt: expansion');
-	$q = $s->query_argv_to_string($g, [qw(x d:yesterday.. y)]);
-	is($q, strftime('x d:%Y%m%d.. y', gmtime(time - 86400)),
-		'"yesterday" handled');
+	$q = $s->query_argv_to_string($g, [qw(x dt:yesterday.. y)]);
+	my $exp = strftime('%Y%m%d', gmtime(time - 86400));
+	like($q, qr/x dt:$exp[0-9]{6}\.\. y/, '"yesterday" handled');
 	$q = $s->query_argv_to_string($g, [qw(x dt:20101002054123)]);
 	is($q, 'x dt:20101002054123..20101003054123', 'single dt: expanded');
 	$q = $s->query_argv_to_string($g, [qw(x dt:2010-10-02T05:41:23Z)]);
@@ -627,7 +627,8 @@ SKIP: {
 	my $x_days_ago = strftime('%Y%m%d', gmtime(time - (5 * 86400)));
 	$orig = $qs = qq[broken d:5.days.ago..];
 	$s->query_approxidate($g, $qs);
-	is($qs, qq[broken d:$x_days_ago..], 'date.phrase.with.dots');
+	like($qs, qr/\Abroken dt:$x_days_ago[0-9]{6}\.\./,
+		'date.phrase.with.dots');
 
 	$ENV{TEST_EXPENSIVE} or
 		skip 'TEST_EXPENSIVE not set for argv overflow check', 1;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [SQUASH] fix "d:YYYYMMDD..$NEEDS_APPROXIDATE" case
  2021-04-16 23:43 [PATCH] search: expand "d:" to "dt:" for precision with approxidate Eric Wong
@ 2021-04-17  0:00 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2021-04-17  0:00 UTC (permalink / raw)
  To: meta

diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index e0e8164d..fbcff2c3 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -302,7 +302,10 @@ sub date_parse_prepare {
 				$x = "$fmt$#$to_parse\0";
 			}
 			if ($pfx eq 'dt') {
-				s/\0%Y%m%d/\0%Y%m%d%H%M%S/ for @r;
+				for (@r) {
+					s/\0%Y%m%d/\0%Y%m%d%H%M%S/;
+					s/\A([0-9]{8})\z/${1}000000/;
+				}
 			}
 		}
 	} elsif ($pfx eq 'dt') {
diff --git a/t/search.t b/t/search.t
index b63a7672..47a67f7f 100644
--- a/t/search.t
+++ b/t/search.t
@@ -630,6 +630,11 @@ SKIP: {
 	like($qs, qr/\Abroken dt:$x_days_ago[0-9]{6}\.\./,
 		'date.phrase.with.dots');
 
+	$orig = $qs = 'd:20101002..now';
+	$s->query_approxidate($g, $qs);
+	like($qs, qr/\Adt:20101002000000\.\.[0-9]{14}\z/,
+		'approxidate on range-end only');
+
 	$ENV{TEST_EXPENSIVE} or
 		skip 'TEST_EXPENSIVE not set for argv overflow check', 1;
 	my @w;

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-17  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-16 23:43 [PATCH] search: expand "d:" to "dt:" for precision with approxidate Eric Wong
2021-04-17  0:00 ` [SQUASH] fix "d:YYYYMMDD..$NEEDS_APPROXIDATE" case Eric Wong

unofficial mirror of meta@public-inbox.org

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://yhetil.org/meta

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ https://yhetil.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.mail.public-inbox.meta
	nntp://news.public-inbox.org/inbox.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general


AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git