!= æ—¥æ™‚ã®æ–‡å—åˆ—ã¸ã®å¤‰æ›
!= 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