#!/usr/bin/perl -w my $usage = "Usage: $0 -adjust [-][hh:m]m:ss [-file wwwww.www] [-start hh:mm:ss]"; use Date::Calc qw( Decode_Date_US Add_Delta_DHMS Delta_DHMS); my $debug; ##my $debug = 1; my ($adjsec,$startsec); my %trans; while(@ARGV) { my ($opt,$val) = (shift,shift); $opt =~ s/^(\-\w)\w*$/$1/; if($opt eq "-a") { ($adjsec) = &hhmmss_toseconds($val) or die "Cannot convert $opt $val: $!\n"; print STDERR "adjusting by $adjsec seconds\n"; } elsif ($opt eq "-f") { &get_trans("$val"); } elsif ($opt eq "-s") { ($startsec) = &hhmmss_toseconds($val) or die "Cannot convert $opt $val: $!\n"; } else { die "Unknown option $opt\n",$usage; } } while(<>) { ### 48778 Thu Feb 22 15:42:39 2001 546.jpg chomp; my ($size,$day,$mo,$date,$time,$year,$file) = split; ### print STDERR "time is $time\n"; my ($timesec) = &hhmmss_toseconds($time); ### print STDERR "timesec is $timesec\n"; my $fn = $file; $fn =~ s/\..*$//; if(exists $trans{$fn}) { my $tr = $trans{$fn}; if($tr =~ /^-?\d{1,2}$/) { $timesec += $tr; print STDERR "$fn: Time adjusted by $tr\n"; } elsif($tr =~ /:/ and $tr =~ /^[\d:]+/) { my $oldsec = $timesec; $timesec = $startsec + &hhmmss_toseconds($tr); my $change = $timesec - $oldsec; print STDERR "$fn: Time changed by $change\n"; } else { print STDERR "$fn: Cannot parse transition $tr\n"; } } elsif (defined $adjsec) { $timesec += $adjsec; } else { print STDERR "$fn: No change\n"; } ($time) = &seconds_tohmmss($timesec); print join(' ',$size,$day,$mo,$date,$time,$year,$file),"\n"; } exit; sub hhmmss_toseconds { my ($t) = @_; my $sign = ($t =~ s|^\s*-||) ? -1 : +1; if ($t =~ m|^([\d]+):([\d]+):([\d\.]+)$|) { $t = 3600*$1 + 60*$2 + $3; } elsif ($t =~ m|^([\d]+):([\d\.]+)$|) { $t = 60*$1 + $2; } else { die "hhmmss_toseconds: bad arg <$t> sign <$sign>"; } return ($sign*$t); } sub seconds_tohmmss { my ($t) = @_; ### print STDERR "stoh: $t\n"; my (undef,undef,$day,$hour,$minute,$second) = Add_Delta_DHMS(1,1,1,0,0,0,0,0,0,$t); return (sprintf("%02i:%02i:%02i",$hour,$minute,$second)); if($hour) { $t = sprintf("%i:%02i:%02i",$hour,$minute,$second) } elsif ($minute) { $t = sprintf("%i:%02i",$minute,$second) } else { $t = sprintf("%i",$second) } return ($t); } sub pretty_date { return (join('/',$_[1],$_[2],$_[0]).' '.sprintf("%02i:%02i:%02i",@_[3..$#_])); } sub get_trans { my ($infile) = @_; open(TS,"<$infile") or die "Cannot open <$infile: $!"; while() { chomp; next unless $_; my @a = split; my ($i,$tr) = ($a[0],$a[$#a]); if($tr =~ /^[\d:]+$/) { $trans{$i} = $tr; } else { print STDERR "Bad time in $_\n"; } } if($debug) {for my $i (sort {$a<=>$b}keys %trans) {print STDERR "$i $trans{$i}\n"}} close(TS) and print STDERR "Read transition file $infile\n"; return }