37 lines
936 B
Go
37 lines
936 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var commandWeekDescription = "get week in this year and week begin and end offset by this week"
|
|
|
|
func commandWeek(args []string) {
|
|
var err error
|
|
offset := 0
|
|
if len(args) == 2 {
|
|
offset, err = strconv.Atoi(args[1])
|
|
if err != nil {
|
|
fmt.Printf("expect `%v` to be a integer\n", args[1])
|
|
os.Exit(1)
|
|
}
|
|
} else if len(args) > 2 {
|
|
fmt.Printf("only accept 1 argument, got %v\n", len(args)-1)
|
|
os.Exit(1)
|
|
}
|
|
offsetTime := time.Hour * time.Duration(offset) * 7 * 24
|
|
targetTime := time.Now().Add(offsetTime)
|
|
_, week := targetTime.ISOWeek()
|
|
weekday := int(targetTime.Weekday())
|
|
// make Sunday = 7
|
|
if weekday == 0 {
|
|
weekday = 7
|
|
}
|
|
weekBegin := targetTime.Add(time.Hour * 24 * time.Duration(weekday-1) * -1)
|
|
weekEnd := targetTime.Add(time.Hour * 24 * time.Duration(7-weekday))
|
|
fmt.Printf("week-%v-%v-%v\n", week, weekBegin.Format("0102"), weekEnd.Format("0102"))
|
|
}
|