
      Program Add_time
!
      implicit none
!
      character(len=10) :: c_datetime
      character(len=13) :: c_datetime_out
      character(len=3 ) :: c_addhours
      character(len=1 ) :: c_sign
      integer :: jyear
      integer :: jmonth
      integer :: jday
      integer :: jhour
      integer :: addhours
      integer :: ierror
!
      integer :: ichar
      integer :: iyear
      integer :: mdays(12)
      data mdays /31,28,31,30,31,30,31,31,30,31,30,31/
!
      ierror=0
!
      call GetArg( 1_4,c_datetime )
      call GetArg( 2_4, c_addhours)
      read (c_datetime,'(i4,3i2)') jyear,jmonth,jday,jhour
      read (c_addhours,'(a1,i2)')  c_sign,addhours
      if (c_sign=='-') addhours=-addhours      
!
! Check that yaer is in acceptable range (after 2000)
      iyear=jyear-2001
      if (iyear < 0) then ! leap year will not be determined correctly
        ierror=ierror+1
      endif 
!
! Determine if leap year
      if (mod(iyear,4) == 3) then   ! leap year
        mdays(2)=29
      endif
!
! Check that input date time is ok 
      if ( (jmonth < 1) .or. (jmonth > 12) ) then
        ierror=ierror+10
      endif
      if ( (jday < 1) .or. (jday > mdays(jmonth)) ) then
        ierror=ierror+100
      endif 
      if ( (jhour < 0) .or. (jhour > 24) )then
        ierror=ierror+1000
      endif
      if (abs(addhours)>23) then ! hours may not be added correctly
        ierror=ierror+10000
      endif
!
      if (ierror /= 0) then
        write (c_datetime,'(i10)') ierror
        write (*,*) c_datetime
        stop
      endif
!      
      jhour=jhour+addhours
      if (jhour > 23) then
        jhour=jhour-24 
        jday=jday+1
        if (jday>mdays(jmonth)) then
          jmonth=jmonth+1
          jday=1
          if (jmonth>12) then
            jmonth=jmonth-12
            jyear=jyear+1
          endif
        endif
      endif
      if (jhour < 0) then
        jhour=jhour+24 
        jday=jday-1
        if (jday<1) then
          jmonth=jmonth-1
          if (jmonth<1) then
            jmonth=12
            jyear=jyear-1
          endif
          jday=mdays(jmonth)
        endif  
      endif       
!         
      write (c_datetime_out,'(i4,3i3)') jyear,jmonth,jday,jhour
      do ichar=6,12,3
        if (c_datetime_out(ichar:ichar)==' ') then
          c_datetime_out(ichar:ichar)='0'
        endif
      enddo
      write (*,*) c_datetime_out
!
      end program Add_time


