From 754b8082fb14fdd41b495a035df2ff236eb319a1 Mon Sep 17 00:00:00 2001 From: Robin Martijn Date: Sun, 25 Aug 2019 22:35:01 +0200 Subject: [PATCH] time/vlib: fix day_of_week() with sakamoto's algorithm --- vlib/time/time.v | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/vlib/time/time.v b/vlib/time/time.v index 30b56dcdde..82eec3345c 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -366,9 +366,14 @@ pub fn (t Time) relative() string { } pub fn day_of_week(y, m, d int) int { - // TODO please no - //# return (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7; - return 0 + // Sakomotho's algorithm is explained here: + // https://stackoverflow.com/a/6385934 + t := [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4] + mut sy := y + if (m < 3) { + sy = sy - 1 + } + return ( sy + sy/4 - sy/100 + sy/400 + t[m-1] + d - 1) % 7 + 1 } pub fn (t Time) day_of_week() int {