Last active
June 6, 2017 23:08
-
-
Save sekky0905/149bf22d6fc48d1a67dc278eb633b5b4 to your computer and use it in GitHub Desktop.
Go言語で今月から標準入力で受けたyyyy-mmまでの各月の月初と月末を一覧表示する簡単なコマンドラインアプリを作ってみた ref: http://qiita.com/Sekky0905/items/68fa4fa09d7176ac1878
This file contains 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" | |
"log" | |
"regexp" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func main() { | |
// 遡る日付をyyyy-mmで入力を受け付ける | |
fmt.Println("いつまで遡りますか?\nyyyy-mmで入力してください。(現在と同じかそれよりも前を指定してください。)") | |
var input string | |
var limitYear int | |
var limitMonth int | |
// 適切な入力がなされない限りループ | |
for { | |
// 標準入力をスキャン | |
fmt.Scan(&input) | |
// 正規表現のチェック | |
if b, err := regexp.MatchString(`^(\d{4})-(0[1-9]|1[0-2])$`, input); !b || err != nil { | |
fmt.Println("入力いただいた文字列が不適切です。 yyyy-mmで入力してください。") | |
} else { | |
// 入力された文字列を"-"で分割 | |
s := strings.Split(input, "-") | |
// 遡る西暦の限界値 | |
limitYear, err = strconv.Atoi(s[0]) | |
if err != nil { | |
log.Printf("can not strconv.Atoi :%v \n", err) | |
} | |
// 遡る月の限界値 | |
limitMonth, err = strconv.Atoi(s[1]) | |
if err != nil { | |
log.Printf("can not strconv.Atoi :%v \n", err) | |
} | |
// 入力で受け付けた日付をDateに変換 | |
inputDate := time.Date(limitYear, time.Month(limitMonth), 0, 0, 0, 0, 0, time.UTC) | |
// 現在よりも未来の日付にならないようにする | |
if !inputDate.After(time.Now()) { | |
// 適切な値が入力されたら、ループを抜ける | |
break | |
} | |
fmt.Println("現在と同じかそれよりも前を指定してください。") | |
} | |
} | |
ShowSpecificTermList(limitYear, limitMonth) | |
} | |
// 現在の時刻から指定した日付まで遡った月初と月末の日付の一覧を表示する | |
func ShowSpecificTermList(limitYear, limitMonth int) { | |
// 現在の時刻を取得 | |
now := time.Now() | |
// 基準となる日付を設定 | |
criterionDate := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC) | |
// 終了の条件となる日付を設定 | |
finDate := time.Date(limitYear, time.Month(limitMonth), 30, 0, 0, 0, 0, time.UTC) | |
i := 0 | |
for { | |
// 月初を設定 | |
beginningOfTheMonth := criterionDate.AddDate(0, -i, 0) | |
// 月末を設定 | |
endOfTheMonth := criterionDate.AddDate(0, -i+1, -1) | |
fmt.Printf("月初:%v", beginningOfTheMonth) | |
fmt.Printf("月末:%v\n", endOfTheMonth) | |
// 終了条件の日付以前の日にちまで遡ったら、breakしてループを抜ける | |
if beginningOfTheMonth.Before(finDate) { | |
fmt.Println("終了") | |
break | |
} | |
// インクリメント | |
i++ | |
} | |
} | |
This file contains 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
いつまで遡りますか? | |
yyyy-mmで入力してください。(現在と同じかそれよりも前を指定してください。) | |
2020-03 // <= 入力 | |
現在と同じかそれよりも前を指定してください | |
2016-01 // 入力 | |
月初:2017-06-01 12:00:00 +0000 UTC月末:2017-06-30 12:00:00 +0000 UTC | |
月初:2017-05-01 12:00:00 +0000 UTC月末:2017-05-31 12:00:00 +0000 UTC | |
月初:2017-04-01 12:00:00 +0000 UTC月末:2017-04-30 12:00:00 +0000 UTC | |
月初:2017-03-01 12:00:00 +0000 UTC月末:2017-03-31 12:00:00 +0000 UTC | |
月初:2017-02-01 12:00:00 +0000 UTC月末:2017-02-28 12:00:00 +0000 UTC | |
月初:2017-01-01 12:00:00 +0000 UTC月末:2017-01-31 12:00:00 +0000 UTC | |
月初:2016-12-01 12:00:00 +0000 UTC月末:2016-12-31 12:00:00 +0000 UTC | |
月初:2016-11-01 12:00:00 +0000 UTC月末:2016-11-30 12:00:00 +0000 UTC | |
月初:2016-10-01 12:00:00 +0000 UTC月末:2016-10-31 12:00:00 +0000 UTC | |
月初:2016-09-01 12:00:00 +0000 UTC月末:2016-09-30 12:00:00 +0000 UTC | |
月初:2016-08-01 12:00:00 +0000 UTC月末:2016-08-31 12:00:00 +0000 UTC | |
月初:2016-07-01 12:00:00 +0000 UTC月末:2016-07-31 12:00:00 +0000 UTC | |
月初:2016-06-01 12:00:00 +0000 UTC月末:2016-06-30 12:00:00 +0000 UTC | |
月初:2016-05-01 12:00:00 +0000 UTC月末:2016-05-31 12:00:00 +0000 UTC | |
月初:2016-04-01 12:00:00 +0000 UTC月末:2016-04-30 12:00:00 +0000 UTC | |
月初:2016-03-01 12:00:00 +0000 UTC月末:2016-03-31 12:00:00 +0000 UTC | |
月初:2016-02-01 12:00:00 +0000 UTC月末:2016-02-29 12:00:00 +0000 UTC | |
月初:2016-01-01 12:00:00 +0000 UTC月末:2016-01-31 12:00:00 +0000 UTC | |
終了 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment