Help us understand the problem. What is going on with this article?

Golangでプレミアムフライデーかどうか判定する

More than 3 years have passed since last update.

PHPでプレミアムフライデーかどうか判定する のGolang版。

Goは日付の扱いが難しいですね。もっといい方法がありそうです。

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)
}
ytkhs
A web developer based in Tokyo, Yokohama and Niigata.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした