!= 譌･譎ゅ�譁�ｭ怜�縺ｸ縺ｮ螟画鋤
!= Convert date into a string
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: dccaldatetochar.f90,v 1.5 2009-10-18 02:34:48 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2009-. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
! 縺薙�繝輔ぃ繧､繝ｫ縺ｫ險倩ｼ峨＆繧後ｋ謇狗ｶ壹″鄒､縺ｯ dc_calendar 繝｢繧ｸ繝･繝ｼ繝ｫ縺九ｉ謠蝉ｾ帙＆繧後∪縺�.
!
! Procedures described in this file are provided from "dc_calendar" module.
!

function DCCalDateToChar1( year, month, day, hour, min, sec, zone ) &
  & result(result)
  !
  ! 蟷ｴ譛域律譎ょ�遘偵ｒ譁�ｭ怜梛螟画焚 (gtool4 netCDF 隕冗ｴ��5.5 譌･譎ょｽ｢蠑上阪↓貅匁侠)
  ! 縺ｸ螟画鋤縺励※霑斐＠縺ｾ縺�.
  !
  ! Convert year, month, day, hour, minute, second into a string
  ! (conformed to gtool4 netCDF Convention
  ! "5.5 Expression of date and time").
  !
  use dc_types, only: STRING, TOKEN, DP
  use dc_string, only: toChar, CPrintf, StoA, RoundNum
  use dc_message, only: MessageNotify
  implicit none
  character(STRING):: result
                              ! 譌･譎よュ蝣ｱ繧定｡ｨ縺呎枚蟄怜�.
                              ! 陦ｨ遉ｺ蠖｢蠑上↓縺､縺�※縺ｯ gtool4 netCDF 隕冗ｴ�
                              ! 5.5 譌･譎ょｽ｢蠑上ｒ蜿ら�縺ｮ縺薙→.
                              !
                              ! Strings that express date and time.
                              ! See gtool4 netCDF Convention
                              ! 5.5 Expression of date and time for details.
  integer, intent(in):: year                     ! 蟷ｴ. Year.
  integer, intent(in):: month                    ! 譛�. Month.
  integer, intent(in):: day                      ! 譌･. Day.
  integer, intent(in):: hour                     ! 譎�. Hour.
  integer, intent(in):: min                      ! 蛻�. Minute.
  real(DP), intent(in):: sec                     ! 遘�. Sec.
  character(*), intent(in), optional:: zone      ! UTC 縺九ｉ縺ｮ譎ょｷｮ. Time-zone.
  integer:: csec_len
  character(TOKEN):: csec, zonew
continue

  if ( present(zone) ) then
    zonew = zone
  else
    zonew = ''
  end if

  csec = toChar(sec)
  csec = RoundNum( csec )
  if ( trim(csec) == '-0.' ) csec = '0.'
  do while ( index('123456789.', csec(len_trim(csec):len_trim(csec)) ) == 0 )
    if ( len_trim(csec) < 2 ) exit
    csec = csec(1:len_trim(csec)-1)
  end do
  if (int(sec) > -1 .and. int(sec) < 10)  csec = '0' // csec
  csec_len = len(trim(adjustl(csec)))
  if (csec(csec_len:csec_len) == '.') csec = csec(1:csec_len-1)

  result = CPrintf('%04d-%02d-%02dT%02d:%02d:%c%c', &
    & i=(/year, month, day, hour, min/), &
    & c1=trim(csec), c2=trim(zonew))

end function DCCalDateToChar1