Bri*_*ter 8 math datetime date formula
输入:自0001年1月1日起的秒数
输出:此期间的全年数
我开发了一种我认为不是最佳解决方案的算法.我认为应该有一个不涉及循环的解决方案.有关算法的信息,请参阅代码块1. A)确定天数和B)根据闰年从迭代年份中迭代减去366或365,同时增加年份总数
这并不像Divide DayCount那样简单365.2425和截断,因为我们在1月1日遇到了失败点,0002(31536000秒/(365.2425*24*60*60))= 0.99934.
从0001年1月1日上午12:00开始,从非循环方法中提取年数的任何想法?
我需要弄明白这一点,因为我需要一个长时间嵌入的日期(存储秒数),以便我可以用1秒的精度跟踪多达1200万.
代码块1 - 从秒数(包括闰年)获得年数的低效算法
Dim Days, Years As Integer
'get Days
Days = Ticks * (1 / 24) * (1 / 60) * (1 / 60) 'Ticks = Seconds from Year 1, January 1
'get years by counting up from the beginning
Years = 0
While True
'if leap year
If (Year Mod 4 = 0) AndAlso (Year Mod 100 <> 0) OrElse (Year Mod 400 = 0) Then
If Days >= 366 Then 'if we have enough days left to increment the year
Years += 1
Days -= 366
Else
Exit While
End If
'if not leap year
Else
If Days >= 365 Then 'if we have enough days left to increment the year
Years += 1
Days -= 365
Else
Exit While
End If
End If
End While
Return Years
Run Code Online (Sandbox Code Playgroud)
编辑:我的解决方案是跳过在8位内嵌入日期的内存节省,并将每个值(几秒到几年)存储在单独的整数中.这会以内存为代价导致即时检索.
Edit2:第一次编辑中的错字(8位)
Blu*_*eft 22
如果您需要精确到秒,您可能需要商业级日期时间包; 使用简单的算法准确地完成它太复杂了.例如:
由于这些和更多的复杂性,你最好不要自己编写代码,除非你可以放松你需要准确的约束到超过1200万年的第二个.
"1582年10月4日 - 阿维拉的圣特雷莎去世.第二天,10月15日,她被埋葬了."