diff options
| author | Michael Manring <michael@manring> | 2022-07-26 02:15:52 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-07-26 02:15:52 +0700 |
| commit | 5c7b1e8c8ef87abb00288812ed613f05c4737a52 (patch) | |
| tree | a80a492acda250bd7793b2202f55798dcae1d5e7 | |
| parent | c38aacff5c67457eb9db74892307cec8d24226e9 (diff) | |
| download | perlweeklychallenge-club-5c7b1e8c8ef87abb00288812ed613f05c4737a52.tar.gz perlweeklychallenge-club-5c7b1e8c8ef87abb00288812ed613f05c4737a52.tar.bz2 perlweeklychallenge-club-5c7b1e8c8ef87abb00288812ed613f05c4737a52.zip | |
pwc013 - refactor task#1 to use go package
| -rw-r--r-- | challenge-013/pokgopun/go/ch-1.go | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/challenge-013/pokgopun/go/ch-1.go b/challenge-013/pokgopun/go/ch-1.go index d7e704436e..f11935cf4f 100644 --- a/challenge-013/pokgopun/go/ch-1.go +++ b/challenge-013/pokgopun/go/ch-1.go @@ -1,42 +1,37 @@ +/* https://theweeklychallenge.org/blog/perl-weekly-challenge-013/ + +Challenge #1 + + Write a script to print the date of last Friday of every month of a + given year. For example, if the given year is 2019 then it should + print the following: + +2019/01/25 +2019/02/22 +2019/03/29 +2019/04/26 +2019/05/31 +2019/06/28 +2019/07/26 +2019/08/30 +2019/09/27 +2019/10/25 +2019/11/29 +2019/12/27 +*/ package main import ( "fmt" + "os" + "strings" "time" + + "github.com/pokgopun/go/lastweekday" ) func main() { - fmt.Println(newLastWeekdayOfMonth(time.Friday, 2019)) -} -func monthdayCount(y int) (r [12]int) { - r = [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} - if y%4 == 0 { - r[time.February]++ - } - return r -} - -type lastWeekdayOfMonth [12]time.Time - -func (lwdom lastWeekdayOfMonth) String() (r string) { - for _, t := range lwdom { - r += fmt.Sprintln(t.Format("2006/01/02")) - } - return r -} -func newLastWeekdayOfMonth(wd time.Weekday, y int) (lwdom lastWeekdayOfMonth) { - for i, v := range monthdayCount(y) { - // Find offset between the last day of the month and the specified weekday - t := time.Date(y, time.Month(i+1), v, 0, 0, 0, 0, time.UTC) - o := int(wd - t.Weekday()) - // The offset is the number of day we need to go back to the latest specified weekday in certain month - if o != 0 { - // plus sign indicate the offset between specified weekday and the current weekday of the previous week - if o > 0 { - o -= 7 - } - } - lwdom[i] = t.AddDate(0, 0, o) - } - return lwdom + var year uint = 2019 + fmt.Sscanf(strings.Join(os.Args[1:], ""), "%d", &year) + fmt.Println(lastweekday.New(time.Friday, year, "2006/01/02")) } |
