#!/usr/bin/perl
#: round file timestamps to midnight
#
# Find every file under the specified directories (or if none
#  specified, under the current directory), and set the daytime
#  part of its modtime to 00h00 GMT.
#
# THIS IS ALL ABOUT GMT, NOT LOCALTIME.
#
#======================================================================

#die "I don't run as a CGI" if $ENV{'REQUEST_METHOD'};
#   Well, no, but I might run under one, with its a
#   REQUEST_METHOD entry inherited in the env.

use strict;
#use warnings;
BEGIN { $^W = 1}
use File::Find;
my @to_change;

@ARGV = ('.') unless @ARGV;

use Cwd;
my($mtime, @smh);
use Time::Local qw(timegm);
find( sub {
  return if $_ eq '.';
  if(-f $_) {
    if(
      # If the filename looks like a date
      m{^((?:19|20)\d\d)-?([01]\d)-?([0123]\d)\D}
      and eval { $mtime = timegm( 0,0,12, $3, $2-1, $1-1900 ); 1 }
    ) {
      # OK, switching it to the apparent date worked.  Done.
      ;

    } else {
      # It's a file that doesn't look like it starts with date,
      #  or that date is somehow no good.

      $mtime = (stat(_))[9];
      @smh = ( gmtime($mtime) )[0,1,2];
      # ($sec,$min,$hr) would have been clearer.  Feh.
      $mtime -=  $smh[0]  +  ($smh[1] + 60 * $smh[2]) * 60;
        # bring back to previous midnight
    }

    utime $^T, $mtime, $_;
  }
}, @ARGV);
exit;

__END__
