Merge pull request #3281 from nanoant/patch/lib-pure-times-hotfix
times: Export & document countLeapYears/Years/Days
This commit is contained in:
commit
4fefdb4d91
1 changed files with 17 additions and 13 deletions
|
|
@ -1051,26 +1051,30 @@ proc parse*(value, layout: string): TimeInfo =
|
||||||
return info
|
return info
|
||||||
|
|
||||||
# Leap year calculations are adapted from:
|
# Leap year calculations are adapted from:
|
||||||
# from http://www.codeproject.com/Articles/7358/Ultra-fast-Algorithms-for-Working-with-Leap-Years
|
# http://www.codeproject.com/Articles/7358/Ultra-fast-Algorithms-for-Working-with-Leap-Years
|
||||||
# The dayOfTheWeek procs are adapated from:
|
# The dayOfTheWeek procs are adapated from:
|
||||||
# from http://stason.org/TULARC/society/calendars/2-5-What-day-of-the-week-was-2-August-1953.html
|
# http://stason.org/TULARC/society/calendars/2-5-What-day-of-the-week-was-2-August-1953.html
|
||||||
|
|
||||||
# Note: for leap years, start date is assumed to be 1 AD.
|
proc countLeapYears*(yearSpan: int): int =
|
||||||
# counts the number of leap years up to January 1st of a given year.
|
## Returns the number of leap years spanned by a given number of years.
|
||||||
# Keep in mind that if specified year is a leap year, the leap day
|
##
|
||||||
# has not happened before January 1st of that year.
|
## Note: for leap years, start date is assumed to be 1 AD.
|
||||||
proc countLeapYears(yearSpan: int): int =
|
## counts the number of leap years up to January 1st of a given year.
|
||||||
|
## Keep in mind that if specified year is a leap year, the leap day
|
||||||
|
## has not happened before January 1st of that year.
|
||||||
(((yearSpan - 1) / 4) - ((yearSpan - 1) / 100) + ((yearSpan - 1) / 400)).int
|
(((yearSpan - 1) / 4) - ((yearSpan - 1) / 100) + ((yearSpan - 1) / 400)).int
|
||||||
|
|
||||||
proc countDays(yearSpan: int): int =
|
proc countDays*(yearSpan: int): int =
|
||||||
|
## Returns the number of days spanned by a given number of years.
|
||||||
(yearSpan - 1) * 365 + countLeapYears(yearSpan)
|
(yearSpan - 1) * 365 + countLeapYears(yearSpan)
|
||||||
|
|
||||||
proc countYears(daySpan: int): int =
|
proc countYears*(daySpan: int): int =
|
||||||
# counts the number of years spanned by a given number of days.
|
## Returns the number of years spanned by a given number of days.
|
||||||
((daySpan - countLeapYears(daySpan div 365)) div 365)
|
((daySpan - countLeapYears(daySpan div 365)) div 365)
|
||||||
|
|
||||||
proc countYearsAndDays(daySpan: int): tuple[years: int, days: int] =
|
proc countYearsAndDays*(daySpan: int): tuple[years: int, days: int] =
|
||||||
# counts the number of years spanned by a given number of days and the remainder as days.
|
## Returns the number of years spanned by a given number of days and the
|
||||||
|
## remainder as days.
|
||||||
let days = daySpan - countLeapYears(daySpan div 365)
|
let days = daySpan - countLeapYears(daySpan div 365)
|
||||||
result.years = days div 365
|
result.years = days div 365
|
||||||
result.days = days mod 365
|
result.days = days mod 365
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue