times: Export & document countLeapYears/Years/Days

Previously countYears & countDays procs were not public, but there were unused,
causing unnecessary warnings generated by standard library.

Since these procs seems to be useful it is better to just export them than
remove them completely. Also turning inline comments into proper documentation.
This commit is contained in:
Adam Strzelecki 2015-09-03 10:34:55 +02:00
commit ef5f0b1ac9

View file

@ -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.
(((yearSpan - 1) / 4) - ((yearSpan - 1) / 100) + ((yearSpan - 1)/400)).int ## 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
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