Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2014 19:05
Show Gist options
  • Save anonymous/998daa3b3c150dad2915 to your computer and use it in GitHub Desktop.
Save anonymous/998daa3b3c150dad2915 to your computer and use it in GitHub Desktop.
type
TDayRange = range[1..31]
TMonthRange = range[1..12]
TDate* = object
year: int
month: TMonthRange
day: TDayRange
const
DaysInMonth: array[TMonthRange, TDayRange] =
[TDayRange(31), 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
proc IsLeap (year: int): bool =
year mod 4 == 0 and (year mod 100 != 0 or year mod 400 == 0)
proc newDate(year: int, month: TMonthRange, day: TDayRange): TDate =
var
m = month
d = day
daysInM = DaysInMonth[m]
if d > daysInM:
m += 1
d -= daysInM
return TDate(year, m, d)
echo repr(newDate(2014,5,5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment