
      program mydate
      implicit none
      external iargc
      integer iargc

      character*4 cry
      character*2 crm
      character*2 crd
      character*2 crh

      character*8 chh

      integer ry,rm,rd,rh,hh

      integer mo(12)/31,28,31,30,31,30,31,31,30,31,30,31/

      logical leap/.false./

      integer i,shift

      if ( iargc() .ne. 5 ) then
          write(0,*)'Need 5 parameters. Usage: ./mydate RY RM RD RH HH'
          call exit
      endif

      call getarg(1,cry)
      call getarg(2,crm)
      call getarg(3,crd)
      call getarg(4,crh)
      call getarg(5,chh)

      read(cry,*) ry
      read(crm,*) rm
      read(crd,*) rd
      read(crh,*) rh
      read(chh,*) hh

      if ( ry.lt.1900 .or. ry.gt.2200 ) then
          write(0,*) 'Cannot deal with year:', ry
          call exit
      endif

      if ( rm.lt.1 .or. rm.gt.12 ) then
          write(0,*) 'Cannot deal with month:', rm
          call exit
      endif

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c find if this is a leap year
c
      if ( mod(ry,100) .ne. 0 ) then
          if ( mod(ry,4).eq.0 ) leap = .true.
      else
          if ( mod(ry,400).eq.0 ) leap = .true.
      endif
      if ( leap ) then
          mo(2) = 29
      else 
          mo(2) = 28
      endif
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

      if ( rd.lt.1 .or. rd.gt.mo(rm) ) then
          write(0,*) 'Cannot deal with day ', rd, ' of month ', rm
c         print *, mo(rm)
          call exit
      endif
      
      if ( rh.lt.0 .or. rh.gt.24 ) then
          write(0,*) 'Cannot deal with hour ', rh
          call exit
      endif
c
c cycle through hours
c
      if ( hh .ge. 0 )  then
          do i = 1,hh

              rh = rh + 1
              if ( rh.eq.24 ) then
                  rh = 0

                  rd = rd + 1
                  if ( rd.gt.mo(rm) ) then
                      rd = 1

                      rm = rm + 1
                      if ( rm.gt.12 ) then
                          rm = 1

                          ry = ry + 1

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c find if this is a leap year
c
                          if ( mod(ry,100) .ne. 0 ) then
                              if ( mod(ry,4).eq.0 ) leap = .true.
                          else
                              if ( mod(ry,400).eq.0 ) leap = .true.
                          endif
                          if ( leap ) then
                              mo(2) = 29
                          else 
                              mo(2) = 28
                          endif
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

                      endif
                  endif
              endif
c             print *, ry,rm,rd,rh
          enddo
      else
          do i = -1,hh,-1

              rh = rh - 1
              if ( rh.eq.-1 ) then
                  rh = 23

                  rd = rd - 1
                  if ( rd.eq.0 ) then
c
c find previous date
c
                      if ( rm .eq. 1 ) then
                          rm = 12

                          ry = ry - 1

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c find if this is a leap year
c
                          if ( mod(ry,100) .ne. 0 ) then
                              if ( mod(ry,4).eq.0 ) leap = .true.
                          else
                              if ( mod(ry,400).eq.0 ) leap = .true.
                          endif
                          if ( leap ) then
                              mo(2) = 29
                          else 
                              mo(2) = 28
                          endif
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

                      else
                          rm = rm - 1
                      endif

                      rd = mo(rm)
                  endif
              endif
c             print *, ry, rm, rd, rh
          enddo
      endif

      print '(I4,3I3.2)', ry, rm, rd, rh

      end
