!= é–å¹´ã‹ã©ã†ã‹ã®åˆ¤å®š
!= Judge whether it is a leap year
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: dccaldatechkleapyear.f90,v 1.2 2010-10-06 01:48:49 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 DCCalDateChkLeapYear1( elapse_sec, date, cal ) result(result)
  ! é–å¹´ã‹ã©ã†ã‹ã®åˆ¤å®š.
  !
  ! çœç•¥å¯èƒ½å¼•æ•° *date* ãŒçœç•¥ã•ã‚ŒãŸå ´åˆã«ã¯, dc_calendar å†…éƒ¨ã§
  ! ä¿æŒã•ã‚Œã‚‹æ—¥æ™‚ãŒèµ·ç‚¹ã®æ—¥æ™‚ã¨ã—ã¦ç”¨ã„ã‚‰ã‚Œã¾ã™.
  ! *date* ãŒçœç•¥ã•ã‚Œãªã„å ´åˆã«ã¯ãã®å¤‰æ•°ã«è¨å®šã•ã‚ŒãŸæ—¥æ™‚ãŒ
  ! èµ·ç‚¹ã®æ—¥æ™‚ã¨ã—ã¦ç”¨ã„ã‚‰ã‚Œã¾ã™.
  !
  ! çœç•¥å¯èƒ½å¼•æ•° *cal* ãŒçœç•¥ã•ã‚ŒãŸå ´åˆã«ã¯, çµŒéŽç§’æ•° *elapse_sec*
  ! ã®å¹´æœˆæ—¥æ™‚åˆ†ã¸ã®å¤‰æ›ã« dc_calendar å†…éƒ¨ã§ä¿æŒã•ã‚Œã‚‹æš¦ãŒç”¨ã„ã‚‰ã‚Œã¾ã™.
  ! *cal* ãŒçœç•¥ã•ã‚Œãªã„å ´åˆã«ã¯ãã®å¤‰æ•°ã«è¨å®šã•ã‚ŒãŸæš¦ãŒç”¨ã„ã‚‰ã‚Œã¾ã™.
  !
  ! Judge whether it is a leap year.
  !
  ! If an optional argument *date* is omitted,
  ! information of date that is stored in the "dc_calendar"
  ! is used as date of origin,
  ! If *date* is not omitted, information of the variable is used as
  ! date of origin.
  !
  ! If an optional argument *cal* is omitted,
  ! information of calendar that is stored in the "dc_calendar"
  ! is used for conversion of elapsed seconds *elapse_sec* into
  ! year-month-day etc.
  ! If *cal* is not omitted, information of the variable is used.
  !
  use dc_calendar_types, only: DC_CAL_DATE, DC_CAL, &
    & CAL_USER_DEFINED, &
    & CAL_CYCLIC, CAL_NOLEAP, CAL_JULIAN, CAL_GREGORIAN, CAL_360DAY
  use dc_calendar_internal, only: default_cal, default_cal_set, default_date, &
    & dccaldate_normalize, dccaldate_ym2d
  use dc_calendar_generic, only: DCCalDateInquire
  use dc_types, only: DP
  implicit none
  logical:: result
                              ! é–å¹´ã§ã‚ã‚Œã° .true., ãã†ã§ãªã‘ã‚Œã° .false.
                              !
                              ! Leap year: .true., No leap year: .false.
  real(DP), intent(in):: elapse_sec
                              ! *date* ã‹ã‚‰ã®çµŒéŽç§’æ•°.
                              ! Elapsed seconds from *date*.
  type(DC_CAL_DATE), intent(in), optional, target:: date
                              ! èµ·ç‚¹ã¨ãªã‚‹æ—¥æ™‚æƒ…å ±ã‚’åŽã‚ãŸã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆ.
                              !
                              ! An object that stores information of
                              ! date of origin.
  type(DC_CAL), intent(in), optional, target:: cal
                              ! æš¦æƒ…å ±ã‚’åŽã‚ãŸã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆ.
                              !
                              ! An object that stores information of
                              ! calendar.
  ! ä½œæ¥å¤‰æ•°
  ! Work variables
  !
  type(DC_CAL_DATE), pointer:: datep =>null()
  type(DC_CAL), pointer:: calp =>null()
  integer:: year, month, day, hour, min
  real(DP):: sec
continue

  ! ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã®ãƒã‚¤ãƒ³ã‚¿å‰²ä»˜
  ! Associate pointer of an object
  !
  if ( present( date ) ) then
    datep => date
  else
    datep => default_date
  end if

  if ( present( cal ) ) then
    calp => cal
  else
    calp => default_cal
    if ( .not. calp % initialized ) call default_cal_set
  end if

  ! åˆæœŸè¨å®šã®ãƒã‚§ãƒƒã‚¯
  ! Check initialization
  !
  result = .false.
  if ( .not. datep % initialized ) return
  if ( .not. calp % initialized ) return

  ! çµŒéŽæ™‚é–“ã‚’ä¸ŽãˆãŸå ´åˆã®æ—¥æ™‚ã‚’å–å¾—
  ! Inquire date and time when elapse time is given
  !
  call DCCalDateInquire( year, month, day, hour, min, sec, & ! (out)
    & elapse_sec = elapse_sec, date = datep , cal = calp )   ! (in)

  ! é–å¹´ã®åˆ¤å®š
  ! Judge leap year
  !
  select case( calp % cal_type )
  case( CAL_JULIAN )
    if ( mod( year, 4 ) == 0 ) then
      result = .true.
    else
      result = .false.
    end if

  case( CAL_GREGORIAN )
    if ( mod( year, 400 ) == 0 ) then
      result = .true.
    elseif ( mod( year, 100 ) == 0 ) then
      result = .false.
    elseif ( mod( year, 4 ) == 0 ) then
      result = .true.
    else
      result = .false.
    end if

  case default
    result = .false.
  end select

end function DCCalDateChkLeapYear1