#!/usr/bin/env perl
# *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 
# ** Copyright UCAR (c) [RAP] 1996 - 2003. All Rights Reserved. 
# *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 
# author - Becky Ruttenberg
# modified by Wei Yu
## begin code
# required Perl module - strict is in the standard Perl distribution
use strict;            # enforces that variables are declared

my ($ccyymmddhhMM, $dm) = @ARGV;

my $ccyy = int ($ccyymmddhhMM / 100000000);
my $mmddhhMM = $ccyymmddhhMM % 100000000;
my $mm = int ($mmddhhMM / 1000000);
my $dd = int (($mmddhhMM % 1000000) / 10000);
my $hh = int($mmddhhMM % 10000/100);
my $MM = $mmddhhMM % 100;
my @mmday = qw(31 28 31 30 31 30 31 31 30 31 30 31);

my $nMM = $MM + $dm;

my $dh = int($nMM / 60);
$hh += $dh;

$MM = $nMM % 60;

#print "old hh is $hh, old dd is $dd\n";

while (($hh < 0) || ($hh > 23)) {
  if ($hh < 0) {
    $hh += 24;
    change_date(-1);
  } elsif ($hh > 23) {
    $hh -= 24;
    change_date(1);
  }
}

printf("%04d%02d%02d%02d%02d", $ccyy, $mm, $dd, $hh,$MM);

sub change_date {
  my $delta = $_[0];

  if (($ccyy % 4) == 0) {
    if (($ccyy % 400) == 0) {
       $mmday[1] = 29;
    } elsif (($ccyy % 100) != 0) {
       $mmday[1] = 29;
    }
  }

  $dd += $delta;
  if ($dd == 0) {
    $mm -= 1;
    if ($mm == 0) {
      $mm = 12;
      $ccyy -= 1;
    }
    $dd = $mmday[$mm-1];
  } elsif ($dd > $mmday[$mm-1]) {
    $dd = 1;
    $mm += 1;
    if ($mm > 12) {
      $mm = 1;
      $ccyy += 1;
    }
  }
}
