add week command
This commit is contained in:
5
main.go
5
main.go
@@ -9,7 +9,8 @@ func commandsUsage() string {
|
||||
return fmt.Sprintf(
|
||||
`commands:
|
||||
path %v
|
||||
`, commandPathDescription)
|
||||
week %v
|
||||
`, commandPathDescription, commandWeekDescription)
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -17,6 +18,8 @@ func main() {
|
||||
switch os.Args[1] {
|
||||
case "path":
|
||||
commandPath(os.Args[1:])
|
||||
case "week":
|
||||
commandWeek(os.Args[1:])
|
||||
default:
|
||||
fmt.Printf("unknow command `%v`\n\n", os.Args[1])
|
||||
fmt.Printf("%v", commandsUsage())
|
||||
|
||||
36
week.go
Normal file
36
week.go
Normal file
@@ -0,0 +1,36 @@
|
||||
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"))
|
||||
}
|
||||
Reference in New Issue
Block a user