imclerran / roc-isodate

A Roc package for parsing ISO Date/Time strings. 📆⏰📦

Home Page:https://www.imclerran.com/roc-isodate/

Repository from Github https://github.comimclerran/roc-isodateRepository from Github https://github.comimclerran/roc-isodate

Expose more functions, including isLeapYear and more

ageron opened this issue · comments

I'm working on a new exercise for the Exercism Roc track, and I had to rewrite isLeapYear because it is not exposed. Other functions that would really be useful are daysInMonth and weekday. Here are my implementations (I've submitted PR #27 for weekday).

daysInMonth : I64, U8 -> U8
daysInMonth = \year, month ->
    maybeDays =
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        |> List.get (month - 1 |> Num.toU64)
    when maybeDays is
        Ok days -> if Date.isLeapYear year && month == 2 then 29 else days
        Err OutOfBounds -> crash "Month is assumed to be between 1 and 12"

weekday : I64, U8, U8 -> U8
weekday = \year, month, day ->
    year2xxx = (year % 400) + 2400 # to handle years before the epoch
    date = Date.fromYmd year2xxx month day
    daysSinceEpoch = Date.toNanosSinceEpoch date // Const.nanosPerDay
    (daysSinceEpoch + 4) % 7 |> Num.toU8

All functions added as of #27 and #29