The article is written in
Go

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package main /* Determination of the day of the week Jan 1st 1 AD is a Monday in Gregorian calendar. So Jan 0th 1 AD is a Sunday [It does not exist technically]. Every 4 years we have a leap year. But xy00 cannot be a leap unless xy divides 4 with remainder 0. y/4 - y/100 + y/400 : this gives the number of leap years from 1AD to the given year. As each year has 365 days (divides 7 with remainder 1), unless it is a leap year or the date is in Jan or Feb, the day of a given date changes by 1 each year. In other cases it increases by 2. y -= m<3 : If the month is not Jan or Feb, we do not count the 29th Feb (if it exists) of the given year. So y + y/4 - y/100 + y/400 gives the day of Jan 0th (Dec 31st of prev year) of the year. (This gives the remainder with 7 of the number of days passed before the given year began.) Array t: Number of days passed before the month 'm+1' begins. So t[m-1]+d is the number of days passed in year 'y' up to the given date. (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7 is remainder of the number of days from Jan 0 1AD to the given date which will be the day (0=Sunday,6=Saturday). Description credits: Sai Teja Pratap (quora.com/How-does-Tomohiko-Sakamotos-Algorithm-work). Hola Pasku */ import "fmt" func dow(y, m, d int) int { t := []int{0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4} if m < 3 { y = y - 1 } return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7 } func main() { days := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} fmt.Printf("%s\n", days[dow(2013, 5, 13)]) } |

Category: go

*This article was last modified: June 17, 2016, 10 a.m.*

wikicoding is a free and open source project under the GPLv3 license.You can find the source code and contribute to the project via github - wikicoding

Every code has a silver lining.