Skip to content

Instantly share code, notes, and snippets.

@WebFreak001
Last active March 11, 2020 17:00
Show Gist options
  • Save WebFreak001/ca9e84d6e18b76666d88879972ecd6c7 to your computer and use it in GitHub Desktop.
Save WebFreak001/ca9e84d6e18b76666d88879972ecd6c7 to your computer and use it in GitHub Desktop.
calculate from ISO week calendar date (year, week, weekday) into gregorian calendar date (year, month, date)
/* hoping to get this into phobos: https://github.com/dlang/phobos/pull/7420 */
Date dateFromISOWeek(short isoWeekYear, ubyte isoWeek, DayOfWeek weekday) @safe pure
{
immutable adjustedWeekday = weekday == DayOfWeek.sun ? 7 : weekday;
immutable dayOffset = (isoWeek - 1) * 7 + adjustedWeekday;
Date date = Date(isoWeekYear, Month.jan, 3);
immutable startOfYear = date.dayOfWeek;
return date + days(dayOffset - startOfYear);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment