#!/usr/pkg/bin/perl -w

#  Copyright 2008 Google Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

use strict;
use Date::Calc qw(Add_Delta_Days);
use Getopt::Long;

my ($help, $s, $e, $f, $inc, $show_version);
$s = $e = '';
$f = 'YYYY-MM-DD';
$inc = 1;
Getopt::Long::Configure( "no_ignore_case" );
&GetOptions("help" => \$help,
            "start=s" => \$s,
            "end=s" => \$e,
            "format=s" => \$f,
            "increment=n" => \$inc,
            "Version" => \$show_version,
           );

if ($show_version) {
  crush_version();
  exit(0);
}
if ($help ||                       # user asked for it
    $s !~ /^\d{4}-\d{2}-\d{2}$/ || # malformed start date
    $e !~ /^\d{4}-\d{2}-\d{2}$/ || # malformed end date
    $e lt $s) {                    # end is before start
  usage();
  exit(1);
}

my $c = $s;

do {
  my @a = split(/-/, $c);
  print date_format($f, @a), qq(\n);
  $c = sprintf("%.4d-%.2d-%.2d", Add_Delta_Days(@a, $inc));
} while ($c le $e);

exit(0);

sub usage {
  print STDERR <<ENDUSAGE;

prints all of the dates within a range

usage: $0 <-s YYYY-MM-DD> <-e YYYY-MM-DD>
options:
  -s, --start <DATE>   beginning of the range
  -e, --end <DATE>     end of the range
  -f, --format <STR>   specifies an alternate date format
  -i, --increment <N>  specifies the number of days by which to
                       increment

Date formats may consist of patterns with YYYY, MM, DD, HH, MI, and SS.
Default is YYYY-MM-DD (ISO 8601).

ENDUSAGE
}


sub date_parse {
  my $fmt = shift;
  my $datestr = shift;

  return undef unless ($fmt && $datestr);

  my $YY  = index($fmt, 'YYYY');
  my $MM  = index($fmt, 'MM');
  my $DD  = index($fmt, 'DD');
  my $HH  = index($fmt, 'HH');
  my $MI  = index($fmt, 'MI');
  my $SS  = index($fmt, 'SS');

  my ($y, $m, $d, $h, $i, $s) = (0, 0, 0, 0, 0, 0);
  $y = substr($datestr, $YY, 4) if($YY != -1);
  $m = substr($datestr, $MM, 2) if($MM != -1);
  $d = substr($datestr, $DD, 2) if($DD != -1);
  $h = substr($datestr, $HH, 2) if($HH != -1);
  $i = substr($datestr, $MI, 2) if($MI != -1);
  $s = substr($datestr, $SS, 2) if($SS != -1);

  return ($y, $m, $d, $h, $i, $s);
}

sub date_format {
  my $fmt = shift;
  my ($y, $m, $d, $h, $i, $s) = @_;

  return undef unless ($fmt);

  $y = sprintf("%.4d", $y) if defined($y);
  $m = sprintf("%.2d", $m) if defined($m);
  $d = sprintf("%.2d", $d) if defined($d);
  $h = sprintf("%.2d", $h) if defined($h);
  $i = sprintf("%.2d", $i) if defined($i);
  $s = sprintf("%.2d", $s) if defined($s);

  $fmt =~ s/YYYY/$y/g;
  $fmt =~ s/MM/$m/g;
  $fmt =~ s/DD/$d/g;
  $fmt =~ s/HH/$h/g;
  $fmt =~ s/MI/$i/g;
  $fmt =~ s/SS/$s/g;

  return $fmt;
}

1;


sub crush_version {
  print STDERR "CRUSH tools release 2013-04 compiled at 2024-12-07-13:43:05\n";
}

1;
