user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 10/13] git: implement date_parse method
Date: Sun,  7 Feb 2021 23:05:18 -1000
Message-ID: <20210208090521.28909-11-e@80x24.org> (raw)
In-Reply-To: <20210208090521.28909-1-e@80x24.org>

Users are expected to be familiar with git's "approxidate"
functionality for parsing dates, so we'll expose that
in our UIs.  Xapian itself has limited date parsing functionality
and I can't expect users to learn it.

This takes around 4-5ms on my aging workstation, so it'll
probably be made acceptable for the WWW UI, even.

libgit2 has a git__date_parse function which I expect to have
less overhead, but it's only for internal use at the moment.
---
 lib/PublicInbox/Git.pm |  8 ++++++--
 t/git.t                | 15 +++++++++++++--
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm
index c6c1c802..9207962b 100644
--- a/lib/PublicInbox/Git.pm
+++ b/lib/PublicInbox/Git.pm
@@ -362,8 +362,7 @@ sub popen {
 
 # same args as popen above
 sub qx {
-	my $self = shift;
-	my $fh = $self->popen(@_);
+	my $fh = popen(@_);
 	if (wantarray) {
 		local $/ = "\n";
 		my @ret = <$fh>;
@@ -377,6 +376,11 @@ sub qx {
 	}
 }
 
+sub date_parse {
+	my $d = $_[0]->qx('rev-parse', "--since=$_[1]");
+	substr($d, length('--max-age='), -1)
+}
+
 # check_async and cat_async may trigger the other, so ensure they're
 # both completely done by using this:
 sub async_wait_all ($) {
diff --git a/t/git.t b/t/git.t
index 0c85e492..7b950d88 100644
--- a/t/git.t
+++ b/t/git.t
@@ -1,12 +1,11 @@
 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
-use warnings;
 use Test::More;
 use PublicInbox::TestCommon;
 my ($dir, $for_destroy) = tmpdir();
-use PublicInbox::Spawn qw(popen_rd);
 use PublicInbox::Import;
+use POSIX qw(strftime);
 
 use_ok 'PublicInbox::Git';
 
@@ -19,6 +18,18 @@ use_ok 'PublicInbox::Git';
 	xsys([qw(git fast-import --quiet)], { GIT_DIR => $dir }, $rdr);
 	is($?, 0, 'fast-import succeeded');
 }
+{
+	my $git = PublicInbox::Git->new($dir);
+	my $s = $git->date_parse('1970-01-01T00:00:00Z');
+	is($s, 0, 'parsed epoch');
+	local $ENV{TZ} = 'UTC';
+	$s = $git->date_parse('1993-10-02 01:02:09');
+	is(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($s)), '1993-10-02T01:02:09Z',
+		'round trips');
+	$s = $git->date_parse('1993-10-02');
+	is(strftime('%Y-%m-%d', gmtime($s)), '1993-10-02',
+		'round trips date-only');
+}
 
 {
 	my $gcf = PublicInbox::Git->new($dir);

  parent reply	other threads:[~2021-02-08  9:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-08  9:05 [PATCH 00/13] lei approxidate, startup fix, --alert Eric Wong
2021-02-08  9:05 ` [PATCHv2 01/13] lei q: improve remote mboxrd UX + MUA Eric Wong
2021-02-08  9:05 ` [PATCH 02/13] lei_xsearch: quiet Eml warnings from remote mboxrds Eric Wong
2021-02-08  9:05 ` [PATCH 03/13] lei q: SIGWINCH process group with the terminal Eric Wong
2021-02-08  9:05 ` [PATCH 04/13] lei q: support --alert=CMD for early MUA users Eric Wong
2021-02-08  9:05 ` [PATCH 05/13] tests: favor IPv6 Eric Wong
2021-02-08  9:05 ` [PATCH 06/13] ds: improve add_timer usability Eric Wong
2021-02-08  9:05 ` [PATCH 07/13] lei: start_pager: drop COLUMNS default Eric Wong
2021-02-08  9:05 ` [PATCH 08/13] lei: avoid racing on unlink + bind + listen Eric Wong
2021-02-08  9:05 ` [PATCH 09/13] lei: drop BSD::Resource usage Eric Wong
2021-02-08  9:05 ` Eric Wong [this message]
2021-02-08  9:05 ` [PATCH 11/13] lei q: use git approxidate with d:, dt: and rt: ranges Eric Wong
2021-02-10  9:59   ` [PATCH] search: fix argv handling of quoted phrases Eric Wong
2021-02-08  9:05 ` [PATCH 12/13] search: use one git-rev-parse process for all dates Eric Wong
2021-02-08  9:05 ` [PATCH 13/13] spawnpp: raise exception on E2BIG errors Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210208090521.28909-11-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 10/13] git: implement date_parse method' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/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://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git