user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] search: fix rt: w/ approxidate when TZ != UTC
@ 2021-09-17  4:40 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-09-17  4:40 UTC (permalink / raw)
  To: meta

While git respects a user's local timezone and returns
seconds-since-the-Epoch, we were unnecessarily and incorrectly
calling gmtime+strftime on its result.  So ignore calling
gmtime+strftime when the strftime format is "%s", just feed
the output time from git directly to Xapian.

This is mainly for lei, which will likely run in a variety of
timezones.  While we're at it, add a recommendation to use
TZ=UTC in public-inbox-httpd, in case there are (misguided :P)
sysadmins who set a non-UTC TZ.
---
 examples/public-inbox-httpd@.service |  3 +++
 lib/PublicInbox/Search.pm            | 11 +++++++----
 t/lei_store.t                        | 24 ++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/examples/public-inbox-httpd@.service b/examples/public-inbox-httpd@.service
index e811da40..147f7c6d 100644
--- a/examples/public-inbox-httpd@.service
+++ b/examples/public-inbox-httpd@.service
@@ -4,6 +4,8 @@
 # (e.g. public-inbox-httpd@1 public-inbox-httpd@2) with the intention
 # that they take turns running in-between upgrades.  This should
 # allow upgrading without downtime.
+# For servers expecting visitors from multiple timezones, TZ=UTC
+# is needed to ensure a consistent approxidate experience with search.
 
 [Unit]
 Description = public-inbox PSGI server %i
@@ -13,6 +15,7 @@ After = public-inbox-httpd.socket
 [Service]
 Environment = PI_CONFIG=/home/pi/.public-inbox/config \
 PATH=/usr/local/bin:/usr/bin:/bin \
+TZ=UTC \
 PERL_INLINE_DIRECTORY=/tmp/.pub-inline
 
 LimitNOFILE = 30000
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index e80a5944..af0a35d9 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -332,7 +332,7 @@ sub date_parse_prepare {
 			push @$to_parse, $x;
 			$x = "\0%s$#$to_parse\0";
 		}
-		$r[1] //= "\0%s+\0";
+		$r[1] //= "\0%s+\0"; # add 1 day
 	}
 	"$pfx:".join('..', @r).$end;
 }
@@ -342,9 +342,12 @@ sub date_parse_finalize {
 	# git-rev-parse can handle any number of args up to system
 	# limits (around (4096*32) bytes on Linux).
 	my @r = $git->date_parse(@$to_parse);
-	my $i;
-	$_[2] =~ s/\0(%[%YmdHMSs]+)([0-9\+]+)\0/strftime($1,
-		gmtime($2 eq '+' ? ($r[$i]+86400) : $r[$i=$2+0]))/sge;
+	# n.b. git respects TZ, times stored in SQLite/Xapian are always UTC,
+	# and gmtime doesn't seem to do the right thing when TZ!=UTC
+	my ($i, $t);
+	$_[2] =~ s/\0(%[%YmdHMSs]+)([0-9\+]+)\0/
+		$t = $2 eq '+' ? ($r[$i]+86400) : $r[$i=$2+0];
+		$1 eq '%s' ? $t : strftime($1, gmtime($t))/sge;
 }
 
 # n.b. argv never has NUL, though we'll need to filter it out
diff --git a/t/lei_store.t b/t/lei_store.t
index 73b5c74d..c31e27a2 100644
--- a/t/lei_store.t
+++ b/t/lei_store.t
@@ -124,4 +124,28 @@ SKIP: {
 		'kw_changed undef on unknown message');
 }
 
+SKIP: {
+	require_mods(qw(HTTP::Date), 1);
+	my $now = HTTP::Date::time2str(time);
+	$now =~ s/GMT/+0000/ or xbail "no GMT in $now";
+	my $eml = PublicInbox::Eml->new(<<"EOM");
+Received: (listserv\@example.com) by example.com via listexpand
+	id abcde (ORCPT <rfc822;u\@example.com>);
+	$now;
+Date: $now
+Subject: timezone-dependent test
+
+WHAT IS TIME ANYMORE?
+EOM
+
+	ok($sto->add_eml($eml), 'recently received message');
+	$sto->done;
+	local $ENV{TZ} = 'GMT+5';
+	my $lse = $sto->search;
+	my $qstr = 'rt:1.hour.ago.. s:timezone';
+	$lse->query_approxidate($lse->git, $qstr);
+	my $mset = $lse->mset($qstr);
+	is($mset->size, 1, 'rt:1.hour.ago.. works w/ local time');
+}
+
 done_testing;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-09-17  4:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17  4:40 [PATCH] search: fix rt: w/ approxidate when TZ != UTC Eric Wong

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

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).