!= 譌･譎ゅ�險ｭ螳�
!= Setting of date
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: dccaldatecreate.f90,v 1.3 2010-09-24 07:07:31 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.
!

subroutine DCCalDateCreate1( year, month, day, hour, min, sec, date, zone, err )
  !
  ! 譌･譎ゅ�險ｭ螳壹ｒ陦後＞縺ｾ縺�.
  !
  ! YYYY-MM-DDThh:mm:ss.sTZD 縺ｮ繧医≧縺ｪ譁�ｭ怜�
  ! (YYYY 縺ｯ蟷ｴ, MM 縺ｯ譛�, DD 縺ｯ譌･, hh 縺ｯ譎�, mm 縺ｯ蛻�, ss.s 縺ｯ遘�,
  ! TZD 縺ｯ繧ｿ繧､繝�繧ｾ繝ｼ繝ｳ) 縺ｧ謖�ｮ壹☆繧句�ｴ蜷医↓縺ｯ
  ! 荳玖ｨ倥�蜷悟錐縺ｮ繧ｵ繝悶Ν繝ｼ繝√Φ繧剃ｽｿ逕ｨ縺励※荳九＆縺�.
  !
  ! 逵∫払蜿ｯ閭ｽ蠑墓焚 *date* 縺檎怐逡･縺輔ｌ縺溷�ｴ蜷医↓縺ｯ, dc_calendar 蜀�Κ縺ｧ
  ! 菫晄戟縺輔ｌ繧� "dc_calendar_types#DC_CAL_DATE" 蝙九�螟画焚縺ｫ譌･譎ゅ′
  ! 險ｭ螳壹＆繧後∪縺�. 縺昴�蠕後�謇狗ｶ壹″縺ｧ *date* 繧堤怐逡･縺励◆蝣ｴ蜷医↓縺ｯ
  ! 縺薙�譌･譎ゅ′菴ｿ逕ｨ縺輔ｌ縺ｾ縺�.
  ! *date* 縺檎怐逡･縺輔ｌ縺ｪ縺��ｴ蜷医↓縺ｯ縺昴�螟画焚縺ｫ譌･譎ゅ′險ｭ螳壹＆繧後∪縺�.
  ! 縺昴�譌･譎ゅｒ菴ｿ逕ｨ縺吶ｋ蝣ｴ蜷�, 謇狗ｶ壹″縺ｫ縺昴� "dc_calendar_types#DC_CAL_DATE" 蝙九�螟画焚
  ! 繧剃ｸ弱∴縺ｦ縺上□縺輔＞.
  !
  ! Set date.
  !
  ! If a string like as "YYYY-MM-DDThh:mm:ss.sTZD"
  ! (YYYY is year, MM is month, DD is day, hh is hour, mm is minute,
  ! ss.s is second, TZD is time zone) is used,
  ! use a following homonymous subroutine.
  !
  ! If an optional argument *date* is omitted,
  ! the date setting is stored to a "dc_calendar_types#DC_CAL_DATE"
  ! variable that is saved in the "dc_calendar".
  ! When *date* is omitted in subsequent procedures, the internal date
  ! is used.
  ! If *date* is not omitted, the settings is stored to the *date*.
  ! In order to use the date setting, use the "dc_calendar_types#DC_CAL_DATE"
  ! varieble to subsequent procedures.
  !

  use dc_calendar_types, only: DC_CAL_DATE
  use dc_calendar_internal, only: default_date
  use dc_regex, only: match
  use dc_message, only: MessageNotify
  use dc_string, only: LChar
  use dc_trace, only: BeginSub, EndSub
  use dc_error, only: StoreError, DC_NOERR, DC_EALREADYINIT, DC_EBADDATE
  use dc_types, only: STRING, DP
  implicit none
  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  ! 遘�. Second.
  type(DC_CAL_DATE), intent(out), optional, target:: date
                              ! 譌･譎よュ蝣ｱ繧貞庶繧√◆繧ｪ繝悶ず繧ｧ繧ｯ繝�.
                              ! 逵∫払縺励◆蝣ｴ蜷医↓縺ｯ, 繝�ヵ繧ｩ繝ｫ繝医�譌･譎ゅ→縺励※
                              ! 謖�ｮ壹＆繧後ｋ.
                              !
                              ! An object that stores information of
                              ! date and time.
                              ! If this is omitted, these information is
                              ! set as default date and time.
  character(*), intent(in), optional:: zone
                              ! UTC 縺九ｉ縺ｮ譎ょｷｮ. Time-zone.
  logical, intent(out), optional:: err
                              ! 萓句､門�逅�畑繝輔Λ繧ｰ.
                              ! 繝�ヵ繧ｩ繝ｫ繝医〒縺ｯ, 縺薙�謇狗ｶ壹″蜀�〒繧ｨ繝ｩ繝ｼ縺�
                              ! 逕溘§縺溷�ｴ蜷�, 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�＠縺ｾ縺�.
                              ! 蠑墓焚 *err* 縺御ｸ弱∴繧峨ｌ繧句�ｴ蜷�,
                              ! 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�○縺�, 莉｣繧上ｊ縺ｫ
                              ! *err* 縺ｫ .true. 縺御ｻ｣蜈･縺輔ｌ縺ｾ縺�.
                              !
                              ! Exception handling flag.
                              ! By default, when error occur in
                              ! this procedure, the program aborts.
                              ! If this *err* argument is given,
                              ! .true. is substituted to *err* and
                              ! the program does not abort.

  ! 菴懈･ｭ螟画焚
  ! Work variables
  !
  type(DC_CAL_DATE), pointer:: datep =>null()
  integer:: start, length
  integer:: stat
  character(STRING):: cause_c
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: dccaldatecreate.f90,v 1.3 2010-09-24 07:07:31 morikawa Exp $'
  character(*), parameter:: subname = 'DCCalDateCreate1'
continue
  call BeginSub( subname, version )
  stat = DC_NOERR
  cause_c = ''

  ! 繧ｪ繝悶ず繧ｧ繧ｯ繝医�繝昴う繝ｳ繧ｿ蜑ｲ莉�
  ! Associate pointer of an object
  !
  if ( present( date ) ) then
    datep => date
  else
    datep => default_date
  end if

!!$  ! 蛻晄悄險ｭ螳壹�繝√ぉ繝�け
!!$  ! Check initialization
!!$  !
!!$  if ( datep % initialized ) then
!!$    stat = DC_EALREADYINIT
!!$    cause_c = 'DC_CAL_DATE'
!!$    goto 999
!!$  end if

  ! 譌･譎ゅ�豁｣蠖捺ｧ縺ｮ繝√ぉ繝�け
  ! Validate date and time
  !
!!$  if ( year < 1 ) then
!!$    stat = DC_EBADDATE
!!$    call MessageNotify('W', subname, 'year=<%d> must be natural number', &
!!$      & i = (/ year /) )
!!$    goto 999
!!$  end if

  if ( month < 1 ) then
    stat = DC_EBADDATE
    call MessageNotify('W', subname, 'month=<%d> must be natural number', &
      & i = (/ month /) )
    goto 999
  end if

  if ( day < 1 ) then
    stat = DC_EBADDATE
    call MessageNotify('W', subname, 'day=<%d> must be natural number', &
      & i = (/ day /) )
    goto 999
  end if

  if ( hour < 0 ) then
    stat = DC_EBADDATE
    call MessageNotify('W', subname, 'hour=<%d> must not be negative', &
      & i = (/ hour /) )
    goto 999
  end if

  if ( min < 0 ) then
    stat = DC_EBADDATE
    call MessageNotify('W', subname, 'min=<%d> must not be negative', &
      & i = (/ min /) )
    goto 999
  end if

  if ( sec < 0.0_DP ) then
    stat = DC_EBADDATE
    call MessageNotify('W', subname, 'sec=<%f> must not be negative', &
      & d = (/ sec /) )
    goto 999
  end if

  call match( '^[#+-]#d+:#d+$', zone, & ! (in)
    &         start, length )           ! (out)
  if ( length > 0 ) then
    datep % zone = zone
  else
    datep % zone = ''
  end if

  ! 蜷�ｦ∫ｴ�縺ｸ縺ｮ蛟､縺ｮ險ｭ螳�
  ! Configure elements
  !
  datep % year  = year
  datep % month = month
  datep % day   = day
  datep % hour  = hour
  datep % min   = min
  datep % sec   = sec

  ! 邨ゆｺ��逅�, 萓句､門�逅�
  ! Termination and Exception handling
  !
  datep % initialized = .true.
999 continue
  nullify( datep )
  call StoreError( stat, subname, err, cause_c )
  call EndSub( subname )
end subroutine DCCalDateCreate1

subroutine DCCalDateCreate2( date_str, date, err )
  !
  ! 譌･譎ゅ�險ｭ螳壹ｒ陦後＞縺ｾ縺�.
  !
  ! *date_str* 縺ｫ YYYY-MM-DDThh:mm:ss.sTZD 縺ｮ蠖｢蠑上�譁�ｭ怜�
  ! (YYYY 縺ｯ蟷ｴ, MM 縺ｯ譛�, DD 縺ｯ譌･, hh 縺ｯ譎�, mm 縺ｯ蛻�, ss.s 縺ｯ遘�,
  ! TZD 縺ｯ繧ｿ繧､繝�繧ｾ繝ｼ繝ｳ) 繧呈欠螳壹＠縺ｦ縺上□縺輔＞.
  ! 蟷ｴ譛域律譎ょ�遘偵ｒ蜷�句､画焚縺ｧ謖�ｮ壹☆繧句�ｴ蜷医↓縺ｯ
  ! 荳願ｨ倥�蜷悟錐縺ｮ繧ｵ繝悶Ν繝ｼ繝√Φ繧剃ｽｿ逕ｨ縺励※荳九＆縺�.
  ! 譌･譎ゅ�譁�ｭ怜�蠖｢蠑上�
  ! gtool4 netCDF 隕冗ｴ��5.5 譌･譎ょｽ｢蠑上阪↓貅匁侠縺励※縺�∪縺�.
  !
  ! "s since 2009-06-17T11:23:45+09:00" 縺ｮ繧医≧縺ｪ譁�ｭ怜�繧剃ｸ弱∴縺溷�ｴ蜷医↓縺ｯ,
  ! "s since " 縺ｮ驛ｨ蛻�ｒ繧ｵ繝悶Ν繝ｼ繝√Φ蜀�〒閾ｪ蜍慕噪縺ｫ蛻�ｊ蜿悶▲縺ｦ,
  ! "2009-06-17T11:23:45+09:00" 縺ｮ驛ｨ蛻�ｒ險ｭ螳壹＠縺ｾ縺�.
  !
  ! 逵∫払蜿ｯ閭ｽ蠑墓焚 *date* 縺檎怐逡･縺輔ｌ縺溷�ｴ蜷医↓縺ｯ, dc_calendar 蜀�Κ縺ｧ
  ! 菫晄戟縺輔ｌ繧� "dc_calendar_types#DC_CAL_DATE" 蝙九�螟画焚縺ｫ譌･譎ゅ′
  ! 險ｭ螳壹＆繧後∪縺�. 縺昴�蠕後�謇狗ｶ壹″縺ｧ *date* 繧堤怐逡･縺励◆蝣ｴ蜷医↓縺ｯ
  ! 縺薙�譌･譎ゅ′菴ｿ逕ｨ縺輔ｌ縺ｾ縺�.
  ! *date* 縺檎怐逡･縺輔ｌ縺ｪ縺��ｴ蜷医↓縺ｯ縺昴�螟画焚縺ｫ譌･譎ゅ′險ｭ螳壹＆繧後∪縺�.
  ! 縺昴�譌･譎ゅｒ菴ｿ逕ｨ縺吶ｋ蝣ｴ蜷�, 謇狗ｶ壹″縺ｫ縺昴� "dc_calendar_types#DC_CAL_DATE" 蝙九�螟画焚
  ! 繧剃ｸ弱∴縺ｦ縺上□縺輔＞.
  !
  ! Set date.
  !
  ! Specify a string like as "YYYY-MM-DDThh:mm:ss.sTZD"
  ! (YYYY is year, MM is month, DD is day, hh is hour, mm is minute,
  ! ss.s is second, TZD is time zone).
  ! If individual variables (year, month, day, hour, minute, second, zone)
  ! are used, use a foregoing homonymous subroutine.
  ! Format of date is conformed to gtool4 netCDF Convention "5.5 Expression of date and time"
  !
  ! If a string like as "s since 2009-06-17T11:23:45+09:00" is specified,
  ! A part "s since " is truncated automatically, and
  ! a part "2009-06-17T11:23:45+09:00" is set.
  !
  ! If an optional argument *date* is omitted,
  ! the date setting is stored to a "dc_calendar_types#DC_CAL_DATE"
  ! variable that is saved in the "dc_calendar".
  ! When *date* is omitted in subsequent procedures, the internal date
  ! is used.
  ! If *date* is not omitted, the settings is stored to the *date*.
  ! In order to use the date setting, use the "dc_calendar_types#DC_CAL_DATE"
  ! varieble to subsequent procedures.
  !
  use dc_calendar_generic, only: DCCalDateParseStr, DCCalDateCreate
  use dc_calendar_types, only: DC_CAL_DATE
  use dc_calendar_internal, only: default_date
  use dc_message, only: MessageNotify
  use dc_types, only: DP, TOKEN
  use dc_trace, only: BeginSub, EndSub
  use dc_error, only: StoreError, DC_NOERR, DC_EALREADYINIT, DC_EBADDATE
  use dc_types, only: STRING
  implicit none
  character(*), intent(in):: date_str
                              ! 譌･譎よュ蝣ｱ繧定｡ｨ縺呎枚蟄怜�.
                              ! 陦ｨ遉ｺ蠖｢蠑上↓縺､縺�※縺ｯ gtool4 netCDF 隕冗ｴ�
                              ! 5.5 譌･譎ょｽ｢蠑上ｒ蜿ら�縺ｮ縺薙→.
                              !
                              ! Strings that express date and time.
                              ! See gtool4 netCDF Convention
                              ! 5.5 Expression of date and time for details.
  type(DC_CAL_DATE), intent(out), optional, target:: date
                              ! 譌･譎よュ蝣ｱ繧貞庶繧√◆繧ｪ繝悶ず繧ｧ繧ｯ繝�.
                              ! 逵∫払縺励◆蝣ｴ蜷医↓縺ｯ, 繝�ヵ繧ｩ繝ｫ繝医�譌･譎ゅ→縺励※
                              ! 謖�ｮ壹＆繧後ｋ.
                              !
                              ! An object that stores information of
                              ! date and time.
                              ! If this is omitted, these information is
                              ! set as default date and time.
  logical, intent(out), optional:: err
                              ! 萓句､門�逅�畑繝輔Λ繧ｰ.
                              ! 繝�ヵ繧ｩ繝ｫ繝医〒縺ｯ, 縺薙�謇狗ｶ壹″蜀�〒繧ｨ繝ｩ繝ｼ縺�
                              ! 逕溘§縺溷�ｴ蜷�, 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�＠縺ｾ縺�.
                              ! 蠑墓焚 *err* 縺御ｸ弱∴繧峨ｌ繧句�ｴ蜷�,
                              ! 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�○縺�, 莉｣繧上ｊ縺ｫ
                              ! *err* 縺ｫ .true. 縺御ｻ｣蜈･縺輔ｌ縺ｾ縺�.
                              !
                              ! Exception handling flag.
                              ! By default, when error occur in
                              ! this procedure, the program aborts.
                              ! If this *err* argument is given,
                              ! .true. is substituted to *err* and
                              ! the program does not abort.

  ! 菴懈･ｭ螟画焚
  ! Work variables
  !
  type(DC_CAL_DATE), pointer:: datep =>null()
  integer:: year  ! 蟷ｴ. Year.
  integer:: month ! 譛�. Month.
  integer:: day   ! 譌･. Day.
  integer:: hour  ! 譎�. Hour.
  integer:: min   ! 蛻�. Minute.
  real(DP):: sec  ! 遘�. Second.
  character(TOKEN):: zone
                  ! UTC 縺九ｉ縺ｮ譎ょｷｮ. Time-zone.
  integer:: stat
  character(STRING):: cause_c
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: dccaldatecreate.f90,v 1.3 2010-09-24 07:07:31 morikawa Exp $'
  character(*), parameter:: subname = 'DCCalDateCreate2'
continue
  call BeginSub( subname, version )
  stat = DC_NOERR
  cause_c = ''

  ! 繧ｪ繝悶ず繧ｧ繧ｯ繝医�繝昴う繝ｳ繧ｿ蜑ｲ莉�
  ! Associate pointer of an object
  !
  if ( present( date ) ) then
    datep => date
  else
    datep => default_date
  end if

!!$  ! 蛻晄悄險ｭ螳壹�繝√ぉ繝�け
!!$  ! Check initialization
!!$  !
!!$  if ( datep % initialized ) then
!!$    stat = DC_EALREADYINIT
!!$    cause_c = 'DC_CAL_DATE'
!!$    goto 999
!!$  end if

  ! 譌･譎ゅｒ陦ｨ迴ｾ縺励◆譁�ｭ怜�縺ｮ隗｣驥�
  ! Parse strings that express date and time
  !
  call DCCalDateParseStr( date_str, &           ! (in)
    & year, month, day, hour, min, sec, zone, & ! (out)
    & err = err )                               ! (out) optional
  if ( present(err) ) then
    if ( err ) then
      stat = DC_EBADDATE
      goto 999
    end if
  end if

  ! 繧ｪ繝悶ず繧ｧ繧ｯ繝医�菴懈�
  ! Create an object
  !
  call DCCalDateCreate( &
    & year, month, day, hour, min, sec, & ! (in)
    & datep, zone, err = err )            ! (out) optional
  if ( present(err) ) then
    if ( err ) then
      stat = DC_EBADDATE
      goto 999
    end if
  end if

  ! 邨ゆｺ��逅�, 萓句､門�逅�
  ! Termination and Exception handling
  !
999 continue
  nullify( datep )
  call StoreError( stat, subname, err, cause_c )
  call EndSub( subname )
end subroutine DCCalDateCreate2