Created
March 6, 2017 12:29
-
-
Save ytkhs/b16e79c1fe6a04fec534caa3b3905dc1 to your computer and use it in GitHub Desktop.
Golangでプレミアムフライデーかどうか判定する ref: http://qiita.com/qube81/items/1e93c837c0a7e3d99a10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
fmt.Println(IsPremiumFriday("2017-02-24")) // true | |
fmt.Println(IsPremiumFriday("2017-02-25")) // false | |
fmt.Println(IsPremiumFriday("2017-03-31")) // true | |
fmt.Println(IsPremiumFriday("2017-12-29")) // true | |
} | |
// IsPremiumFriday returns bool | |
func IsPremiumFriday(datestr string) bool { | |
// 入力日付 | |
d1, err := time.Parse("2006-01-02", datestr) | |
if err != nil { | |
return false | |
} | |
// 翌月の初日を得る | |
d2 := d1.AddDate(0, 1, 0) | |
d2 = time.Date(d2.Year(), d2.Month(), 1, 0, 0, 0, 0, time.UTC) | |
// 一日ずつ減らして最初の金曜日を設定 | |
for d2.Weekday() != time.Friday { | |
d2 = d2.AddDate(0, 0, -1) | |
} | |
return d1.Equal(d2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment