以下假设公历将在未来的五百八十四亿五千万年内继续有效。但是要做好失望的准备;随着我们的太阳开始膨胀,改变地球的轨道和一年的长度,日历可能会被废除,并且当地球在七十五亿年后坠入太阳时,很可能会采用其他措施。
顺便说一下,我甚至不尝试处理格里高利历之前的日期。我只返回日期在1582年10月15日之前发生的天数,需要能够表达这种返回值的原因是GetDateFromSerial
函数具有asString
参数。
Sub GetDateFromSerial(ByVal dateSerial As ULong, ByRef year As Long, ByRef month As Integer, ByRef dayOfMonth As Integer, ByRef secondsIntoDay As Integer, ByRef asString As String)
Const SecondsInOneDay As ULong = 86400 ' 24 hours * 60 minutes per hour * 60 seconds per minute
'Dim startOfGregorianCalendar As DateTime = New DateTime(1582, 10, 15)
'Dim startOfGregorianCalendarInSeconds As ULong = (startOfGregorianCalendar - New DateTime(1, 1, 1)).TotalSeconds
Const StartOfGregorianCalendarInSeconds As ULong = 49916304000
secondsIntoDay = dateSerial Mod SecondsInOneDay
If dateSerial < StartOfGregorianCalendarInSeconds Then
year = -1
month = -1
dayOfMonth = -1
Dim days As Integer = (StartOfGregorianCalendarInSeconds - dateSerial) \ SecondsInOneDay
asString = days & IIf(days = 1, " day", " days") & " before the adoption of the Gregorian calendar on October 15, 1582"
Else
'Dim maximumDateValueInSeconds As ULong = (DateTime.MaxValue - New DateTime(1, 1, 1)).TotalSeconds
Const MaximumDateValueInSeconds As ULong = 315537897600
If dateSerial <= MaximumDateValueInSeconds Then
Dim parsedDate As DateTime = DateTime.MinValue.AddSeconds(dateSerial)
year = parsedDate.Year
month = parsedDate.Month
dayOfMonth = parsedDate.Day
Else
' Move the date back into the range that DateTime can parse, by stripping away blocks of
' 400 years. Aim to put the date within the range of years 2001 to 2400.
Dim dateSerialInDays As ULong = dateSerial \ SecondsInOneDay
Const DaysInFourHundredYears As Integer = 365 * 400 + 97 ' Three multiple-of-4 years in each 400 are not leap years.
Dim fourHundredYearBlocks As Integer = dateSerialInDays \ DaysInFourHundredYears
Dim blocksToFactorInLater As Integer = fourHundredYearBlocks - 5
Dim translatedDateSerialInDays As ULong = dateSerialInDays - blocksToFactorInLater * CLng(DaysInFourHundredYears)
' Parse the date as normal now.
Dim parsedDate As DateTime = DateTime.MinValue.AddDays(translatedDateSerialInDays)
year = parsedDate.Year
month = parsedDate.Month
dayOfMonth = parsedDate.Day
' Factor back in the years we took out earlier.
year += blocksToFactorInLater * 400L
End If
asString = New DateTime(2000, month, dayOfMonth).ToString("dd MMM") & ", " & year
End If
End Sub
Function GetSerialFromDate(ByVal year As Long, ByVal month As Integer, ByVal dayOfMonth As Integer, ByVal secondsIntoDay As Integer) As ULong
Const SecondsInOneDay As Integer = 86400 ' 24 hours * 60 minutes per hour * 60 seconds per minute
If (year < 1582) Or _
((year = 1582) And (month < 10)) Or _
((year = 1582) And (month = 10) And (dayOfMonth < 15)) Then
Throw New Exception("The specified date value has no meaning because it falls before the point at which the Gregorian calendar was adopted.")
End If
' Use DateTime for what we can -- which is years prior to 9999 -- and then factor the remaining years
' in. We do this by translating the date back by blocks of 400 years (which are always the same length,
' even factoring in leap years), and then factoring them back in after the fact.
Dim fourHundredYearBlocks As Integer = year \ 400
Dim blocksToFactorInLater As Integer = fourHundredYearBlocks - 5
If blocksToFactorInLater < 0 Then blocksToFactorInLater = 0
year = year - blocksToFactorInLater * 400L
Dim dateValue As DateTime = New DateTime(year, month, dayOfMonth)
Dim translatedDateSerialInDays As ULong = (dateValue - New DateTime(1, 1, 1)).TotalDays
Const DaysInFourHundredYears As ULong = 365 * 400 + 97 ' Three multiple-of-4 years in each 400 are not leap years.
Dim dateSerialInDays As ULong = translatedDateSerialInDays + blocksToFactorInLater * DaysInFourHundredYears
Dim dateSerial As ULong = dateSerialInDays * SecondsInOneDay + secondsIntoDay
Return dateSerial
End Function