Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@ytkhs

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)
}
2
Help us understand the problem. What is going on with this article?
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
ytkhs
A web developer based in Tokyo, Yokohama and Niigata.

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?