user/dev discussion of public-inbox itself
 help / color / Atom feed
From: "Eric Wong (Contractor, The Linux Foundation)" <e@80x24.org>
To: meta@public-inbox.org
Subject: [WIP 15/17] import: pass "raw" dates to git-fast-import(1)
Date: Thu, 15 Feb 2018 11:08:38 +0000
Message-ID: <20180215110840.30413-16-e@80x24.org> (raw)
In-Reply-To: <20180215110840.30413-1-e@80x24.org>

For LKML, it appears we need an even more liberal parser than
RFC2822 date parser in git.  I have not validated Date::Parse
parses dates correctly, but this at least prevents
git-fast-import(1) from choking.
---
 lib/PublicInbox/Import.pm | 65 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 49 insertions(+), 16 deletions(-)

diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index 811e355..845fbb6 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -12,6 +12,8 @@ use PublicInbox::Spawn qw(spawn);
 use PublicInbox::MID qw(mid_mime mid2path);
 use PublicInbox::Address;
 use PublicInbox::ContentId qw(content_id);
+use Date::Parse qw(str2time);
+use Time::Zone qw(tz_offset);
 
 sub new {
 	my ($class, $git, $name, $email, $ibx) = @_;
@@ -57,7 +59,7 @@ sub gfi_start {
 	chomp($self->{tip} = $git->qx(qw(rev-parse --revs-only), $self->{ref}));
 
 	my @cmd = ('git', "--git-dir=$git_dir", qw(fast-import
-			--quiet --done --date-format=rfc2822));
+			--quiet --done --date-format=raw));
 	my $rdr = { 0 => fileno($out_r), 1 => fileno($in_w) };
 	my $pid = spawn(\@cmd, undef, $rdr);
 	die "spawn fast-import failed: $!" unless defined $pid;
@@ -74,14 +76,7 @@ sub gfi_start {
 
 sub wfail () { die "write to fast-import failed: $!" }
 
-sub now2822 () {
-	my @t = gmtime(time);
-	my $day = qw(Sun Mon Tue Wed Thu Fri Sat)[$t[6]];
-	my $mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$t[4]];
-
-	sprintf('%s, %2d %s %d %02d:%02d:%02d +0000',
-		$day, $t[3], $mon, $t[5] + 1900, $t[2], $t[1], $t[0]);
-}
+sub now_raw () { time . ' +0000' }
 
 sub norm_body ($) {
 	my ($mime) = @_;
@@ -189,7 +184,7 @@ sub remove {
 		print $w "reset $ref\n" or wfail;
 	}
 	my $ident = $self->{ident};
-	my $now = now2822();
+	my $now = now_raw();
 	$msg ||= 'rm';
 	my $len = length($msg) + 1;
 	print $w "commit $ref\nmark :$commit\n",
@@ -206,6 +201,43 @@ sub remove {
 	(($self->{tip} = ":$commit"), $cur);
 }
 
+sub parse_date ($) {
+	my ($mime) = @_;
+	my $hdr = $mime->header_obj;
+	my $date = $hdr->header_raw('Date');
+	my ($ts, $zone);
+	my $mid = $hdr->header_raw('Message-ID');
+	if ($date) {
+		$ts = eval { str2time($date) };
+		if ($@) {
+			warn "bad Date: $date in $mid: $@\n";
+		} elsif ($date =~ /\s+([\+\-]\d+)\s*\z/) {
+			$zone = $1;
+		}
+	}
+	unless ($ts) {
+		my @recvd = $hdr->header_raw('Received');
+		foreach my $r (@recvd) {
+			$zone = undef;
+			$r =~ /\s*(\d+\s+[[:alpha:]]+\s+\d{2,4}\s+
+				\d+\D\d+(?:\D\d+)\s+([\+\-]\d+))/osx or next;
+			$zone = $2;
+			$ts = eval { str2time($1) } and last;
+			warn "no date in Received: $r\n";
+		}
+	}
+	$zone ||= '+0000';
+	# "-1200" is the furthest westermost zone offset,
+	# but git fast-import is liberal so we use "-1400"
+	if ($zone >= 1400 || $zone <= -1400) {
+		warn "bogus TZ offset: $zone, ignoring and assuming +0000\n";
+		$zone = '+0000';
+	}
+	$ts ||= time;
+	$ts = 0 if $ts < 0; # git uses unsigned times
+	"$ts $zone";
+}
+
 # returns undef on duplicate
 # returns the :MARK of the most recent commit
 sub add {
@@ -220,7 +252,7 @@ sub add {
 	# <CAD0k6qSUYANxbjjbE4jTW4EeVwOYgBD=bXkSu=akiYC_CB7Ffw@mail.gmail.com>
 	$name =~ tr/<>//d;
 
-	my $date = $mime->header('Date');
+	my $date_raw = parse_date($mime);
 	my $subject = $mime->header('Subject');
 	$subject = '(no subject)' unless defined $subject;
 	my $path_type = $self->{path_type};
@@ -246,10 +278,11 @@ sub add {
 		$mime = $check_cb->($mime) or return;
 	}
 
-	$mime = $mime->as_string;
 	my $blob = $self->{mark}++;
-	print $w "blob\nmark :$blob\ndata ", length($mime), "\n" or wfail;
-	print $w $mime, "\n" or wfail;
+	my $str = $mime->as_string;
+	print $w "blob\nmark :$blob\ndata ", length($str), "\n" or wfail;
+	print $w $str, "\n" or wfail;
+	$str = undef;
 
 	# v2: we need this for Xapian
 	if ($self->{want_object_id}) {
@@ -269,8 +302,8 @@ sub add {
 	utf8::encode($subject);
 	# quiet down wide character warnings:
 	print $w "commit $ref\nmark :$commit\n",
-		"author $name <$email> $date\n",
-		"committer $self->{ident} ", now2822(), "\n" or wfail;
+		"author $name <$email> $date_raw\n",
+		"committer $self->{ident} ", now_raw(), "\n" or wfail;
 	print $w "data ", (length($subject) + 1), "\n",
 		$subject, "\n\n" or wfail;
 	if ($tip ne '') {
-- 
EW


  parent reply index

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-09 20:51 [v2] one file to rule them all? Eric Wong
2018-02-15 10:55 ` Eric Wong
2018-02-15 11:08   ` [WIP 0/17] initial v2 work based on one-file tree Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 01/17] AUTHORS: add The Linux Foundation Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 02/17] watch_maildir: allow '-' in mail filename Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 03/17] scripts/import_vger_from_mbox: relax From_ line match slightly Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 04/17] import: stop writing legacy ssoma.index by default Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 05/17] import: begin supporting this without ssoma.lock Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 06/17] import: initial handling for v2 Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 07/17] t/import: test for last_object_id insertion Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 08/17] content_id: add test case Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 09/17] searchmsg: add mid_mime import for _extract_mid Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 10/17] scripts/import_vger_from_mbox: support --dry-run option Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 11/17] import: APIs to support v2 use Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 12/17] search: free up 'Q' prefix for a real unique identifier Eric Wong (Contractor, The Linux Foundation)
2018-02-22 21:08       ` Eric Wong
2018-02-15 11:08     ` [WIP 13/17] searchidx: fix comment around next_thread_id Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 14/17] address: extract more characters from email addresses Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` Eric Wong (Contractor, The Linux Foundation) [this message]
2018-02-15 11:08     ` [WIP 16/17] scripts/import_vger_from_mbox: use v2 layout for import Eric Wong (Contractor, The Linux Foundation)
2018-02-15 11:08     ` [WIP 17/17] import: quiet down warnings from bogus From: lines Eric Wong (Contractor, The Linux Foundation)

Reply instructions:

You may reply publically 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=20180215110840.30413-16-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /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

Archives are clonable:
	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

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.org/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

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