SUBROUTINE DATTIM(PJUL,KYMD,KHM)
USE PARKIND1  ,ONLY : JPIM     ,JPRB , JPRD, JPIB

!**** *DATTIM*  - Gives time of the model in the format yyyymmdd and hhmm

!     Purpose.
!     --------
!     Gives time of the model

!**   Interface.
!     ----------
!        *CALL* *DATTIM

!        Explicit arguments :
!        --------------------

!     KYMD   CURRENT DATE IN THE MODEL (YYYYMMDD)      OUTPUT
!     KHM    CURRENT TIME IN THE MODEL (HHMM)          OUTPUT
!     PJUL   Current fractional Julian day, i.e. a counter in days
!             that corresponds to 0 at 1 January of the first year
!             of simulation

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        None

!     Reference.
!     ----------

!     Author.
!     -------
!        Pedro Viterbo  *ECMWF*

!     Modifications.
!     --------------
!        Original : 96-03-03
!        E. Dutra : Sep 2018: Replace incdat by updcal
!                   incdat had some bugs 
!        E. Dutra : Nov 2018 : Add JPIB for runs>69 years

!     ------------------------------------------------------------------


USE YOMDYN1S , ONLY : NSTEP    ,TSTEP
USE YOMCST   , ONLY : RDAY
USE YOMRIP   , ONLY : RTIMST   ,NINDAT   ,NSSSSS

IMPLICIT NONE

#include "fcttim.h"
#include "updcal.intfb.h"

!* arguments
!
REAL(KIND=JPRB),INTENT(OUT)    :: PJUL 
INTEGER(KIND=JPIM),INTENT(OUT)    :: KYMD 
INTEGER(KIND=JPIM),INTENT(OUT)    :: KHM 

!* Local variables
!
REAL(KIND=JPRB) :: ZTIME,ZTIMCUR,ZTIM1JAN
INTEGER(KIND=JPIM) :: ISS,IHH,IMM,IYY,IDAY,IDAYI

!     ------------------------------------------------------------------

!*       1.   TIME OF THE MODEL.
!             ------------------

ZTIME=REAL(NSTEP,KIND=JPRD)*TSTEP
ISS=MOD(INT(ZTIME,KIND=JPIB)+NSSSSS,86400)
IHH=ISS/3600
IMM=MOD(ISS,3600)/60
KHM=100*IHH+IMM
! ztimcur is the julian time at the beginning of the time step
ztimcur=RTIMST+ztime

!*       2.   DATE OF THE MODEL.
!             ------------------

IDAYI=(INT(ZTIME,KIND=JPIB)+NSSSSS)/86400
IYY=NCCAA(NINDAT)
IMM=NMM(NINDAT)
IDAY=NDD(NINDAT)
CALL UPDCAL(IDAY,IMM,IYY,IDAYI,IDAY,IMM,IYY)
KYMD=NCONSTRUCT_DATEY(IYY,IMM,IDAY)

!CBH!*       3.   Fractional Julian day, with 0 being 1 January 00 UT, in the
!CBH!             first year of the simulation
!*       3.   Fractional Julian day, with 1 being 1 January 00 UT, in the
!             first year of the simulation

iyy=nccaa(nindat)
ztim1jan=rtime(iyy,1,1,0)
!CBH      pjul=(ztimcur-ztim1jan+1.)/rday
pjul=(ztimcur-ztim1jan)/rday+1.

RETURN
END SUBROUTINE DATTIM
