]> Sergey Matveev's repositories - public-inbox.git/commitdiff
search: fix rt: w/ approxidate when TZ != UTC
authorEric Wong <e@80x24.org>
Fri, 17 Sep 2021 04:40:07 +0000 (13:40 +0900)
committerEric Wong <e@80x24.org>
Fri, 17 Sep 2021 04:41:11 +0000 (04:41 +0000)
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
lib/PublicInbox/Search.pm
t/lei_store.t

index e811da40497a402e88f1b58a614a8519e7980434..147f7c6dd9d709a5353dac4cb469086ebc35caad 100644 (file)
@@ -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
index e80a5944371ed418d105b9c373b00a1f2282440b..af0a35d9b599f1c991ba4c59b7c926178793d940 100644 (file)
@@ -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
index 73b5c74db0e3468cffe29dadd0054136a950c641..c31e27a2d481b9659c198075aedd592638414544 100644 (file)
@@ -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;