]> jfr.im git - rewrite-logs.git/blob - rewrite-logs.pl
rewrite-logs
[rewrite-logs.git] / rewrite-logs.pl
1 #find irclogs -type f -execdir perl -i.orig rewrite-logs.pl {} +
2
3 # Rewrite irssi's log files to have all lines in the same (timestamp) format
4
5 use strict;
6 use warnings;
7 use POSIX qw/strftime/;
8 use constant FMT => "%s [%m/%d/%y %H:%M:%S]";
9
10 $ENV{TZ} = 'America/Chicago';
11
12 my %months = reverse builtin::indexed qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
13
14 #strftime(fmt, sec, min, hour, mday, mon, year)
15 # all 0-based except mday (1+) and year = real_year-1900.
16
17 my ($sm, $sd, $sy);
18 while (<>) {
19 chomp;
20 if (/^--- (?:Log opened|Day changed) \w\w\w (?<b>\w\w\w) (?<d>\d\d) (?:(?<H>\d\d):\d\d:\d\d )?(?<Y>\d\d\d\d)/) {
21 # Timezone changed at:
22 # --- Log closed Wed May 17 20:23:54 2023
23 # --- Log opened Wed May 17 19:24:29 2023
24 if ($+{Y} eq '2023' && (($+{d} >= 17 && $months{$+{b}} == 4 && defined $+{H} && $+{H} >= 19) || $months{$+{b}} > 4)) {
25 $ENV{TZ} = 'America/Denver';
26 }
27
28 $sm = $months{$+{b}};
29 $sd = $+{d};
30 $sy = $+{Y}-1900; # year = %Y - 1900
31
32 } elsif (/^--- Log closed/) { # ignore
33
34 } elsif ($sd && s/^(?<H>\d\d):(?<M>\d\d) //) { # starts with HH:MM
35 my $newts = strftime(FMT, 0, $+{M}, $+{H}, $sd, $sm, $sy);
36 $_ = "$newts$_";
37
38 } elsif (s@^\[(?<m>\d\d)/(?<d>\d\d)/(?<y>\d\d) (?<H>\d\d):(?<M>\d\d):(?<S>\d\d)\]@@) { # starts with [mm/dd/yy HH:MM:SS]
39 my $newts = strftime(FMT, $+{S}, $+{M}, $+{H}, $+{d}, $+{m}-1, (100+$+{y})); # year = 2000 + %y - 1900
40 $_ = "$newts$_";
41
42 } elsif (/^1\d{9} \[/) { # starts with unixts
43 next; # leave it alone
44
45 } elsif (/^--- /) {
46 print STDERR "Unknown --- line: $ARGV:$. $_\n";
47 } elsif ($sd) {
48 print STDERR "Unknown other line: $ARGV:$. $_\n";
49 } else {
50 print STDERR "No date for line: $sm $sd $sy $ARGV:$. $_\n";
51 }
52
53 } continue {
54 print "$_\n";
55 if (eof) { # if we're about to move on to another file
56 $ENV{TZ} = 'America/Chicago'; # reset the timezone for the start of next file (it'll get switched back to Denver again as appropriate)
57 close ARGV;
58 undef $sm;
59 undef $sd;
60 undef $sy;
61 }
62 }