C $Header: /u/gcmpack/MITgcm/pkg/cal/cal_subdates.F,v 1.2 2003/10/09 04:19:19 edhill Exp $
C $Name:  $

#include "CAL_OPTIONS.h"

      subroutine CAL_SUBDATES(
     I                         finaldate,
     I                         initialdate,
     O                         diffdate,
     I                         mythid
     &                       )

c     ==================================================================
c     SUBROUTINE cal_SubDates
c     ==================================================================
c
c     o Subtract two dates. In case calendar dates are given finaldate
c       must be after initialdate.
c              
c     started: Christian Eckert eckert@mit.edu  30-Jun-1999
c
c     changed: Christian Eckert eckert@mit.edu  29-Dec-1999
c
c              - restructured the original version in order to have a
c                better interface to the MITgcmUV.
c
c              Christian Eckert eckert@mit.edu  03-Feb-2000
c
c              - Introduced new routine and function names, cal_,
c                for verion 0.1.3.
c
c     ==================================================================
c     SUBROUTINE cal_SubDates
c     ==================================================================

      implicit none

c     == global variables ==

#include "cal.h"

c     == routine arguments ==

      integer finaldate(4)
      integer initialdate(4)
      integer diffdate(4)
      integer mythid

c     == local variables ==

      integer workdate(4)
      integer ierr

c     == end of interface ==

      if ((initialdate(4) .gt. 0) .eqv.
     &    (  finaldate(4) .gt. 0)) then


        if (initialdate(4) .eq. -1) then
c         The time interval is subtracted.
          workdate(1) = -initialdate(1)
          workdate(2) = -initialdate(2)
          workdate(3) =  0
          workdate(4) = -1
          call CAL_ADDTIME( finaldate, workdate, diffdate, mythid )
        else
c         The time interval between initial and final date is calculated.
          call CAL_TIMEPASSED( 
     &          initialdate, finaldate, diffdate, mythid )
        endif
      else

        ierr = 801
        call CAL_PRINTERROR( ierr, mythid )
        stop ' stopped in cal_SubDates.'

      endif

 100  continue

      return
      end