! 發展單位:風禹科技驗證有限公司 ! 撰寫人:鄭子璉(Tzu-Lien, Cheng, 璉璉) ,成大水博肄,微軟最有價值專家 ! Web: http://tlcheng.twbbs.org/TLCheng/ E-Mail: qvb3377@ms5.hinet.net ! -------------------------------------------------------------------------------------- Module modDateTime ! 階段數常數 Integer, Parameter :: SC_Year = 1 Integer, Parameter :: SC_Month = 12 Integer, Parameter :: SC_TenDays = 36 ! 旬 Integer, Parameter :: SC_Day = 365 Integer, Parameter :: SC_Days = 366 Contains Integer Function GetStageDays(vYear, vIndex, StageCount) Integer :: vYear, vIndex, StageCount Logical :: bAddDay Integer :: nDays bAddDay = .False. Select Case (StageCount) Case (SC_Year) nDays = 365 bAddDay = .True. Case (SC_Month) Select Case (vIndex) Case (2) nDays = 28 bAddDay = .True. Case (4, 6, 9, 11) nDays = 30 Case DEFAULT nDays = 31 End Select Case (SC_TenDays) Select Case (vIndex) Case (6) nDays = 8 bAddDay = .True. Case (3, 9, 15, 21, 24, 30, 36) nDays = 11 Case DEFAULT nDays = 10 End Select Case (SC_Day, SC_Days) nDays = 1 End Select If (bAddDay) Then If (IsLeapYear(vYear)) Then nDays = nDays + 1 End If End If GetStageDays = nDays End Function Logical Function IsLeapYear(vYear) ! 是否為閏年 Integer :: vYear Logical :: bLeapYear bLeapYear = .False. If (Mod(vYear, 4) == 0) Then bLeapYear = .True. If (Mod(vYear, 100) == 0) Then bLeapYear = .False. If (Mod(vYear, 400) == 0) Then bLeapYear = .True. End If End If End If IsLeapYear = bLeapYear End Function Integer Function GetStageIndex(vYear, vMonth, vDay, StageCount, bNoLeapDay) Integer :: vYear, vMonth, vDay, StageCount Integer :: nIndex, nMonth, i Logical :: bNoLeapDay, pNoLeapDay Optional :: bNoLeapDay If (Present(bNoLeapDay)) Then pNoLeapDay = bNoLeapDay Else pNoLeapDay = .False. ! 預設有閏日 End If Select Case (StageCount) Case (SC_Year) nIndex = 1 Case (SC_Month) nIndex = vMonth Case (SC_TenDays) nMonth = (vMonth - 1) * 3 If (vDay .le. 10) Then nIndex = 1 Else If (vDay .le. 20) Then nIndex = 2 Else nIndex = 3 End If nIndex = nIndex + nMonth Case (SC_Day, SC_Days) nIndex = vDay Do i = 1, vMonth - 1 nIndex = nIndex + GetStageDays(vYear, i, SC_Month) End Do If (pNoLeapDay .And. IsLeapYear(vYear) .And. vMonth > 2) Then nIndex = nIndex - 1 End If End Select GetStageIndex = nIndex End Function Subroutine DateAdd(vYear, vMonth, vDay, addDay) Integer :: vYear, vMonth, vDay, addDay Integer :: nDay vDay = vDay + addDay nDay = GetMonthDays(vYear, vMonth) Do While (vDay > nDay) vDay = vDay - nDay vMonth = vMonth + 1 If (vMonth > SC_Month) Then vMonth = 1 vYear = vYear + 1 End If nDay = GetMonthDays(vYear, vMonth) End Do End Subroutine Integer Function GetMonthDays(vYear, vMonth) Integer :: vYear, vMonth Select Case (vMonth) Case (1, 3, 5, 7, 8, 10, 12) GetMonthDays = 31 Case (4, 6, 9, 11) GetMonthDays = 30 Case (2) If (IsLeapYear(vYear)) Then GetMonthDays = 29 Else GetMonthDays = 28 End If End Select End Function End Module