-
Notifications
You must be signed in to change notification settings - Fork 0
/
golunar.go
104 lines (87 loc) · 1.82 KB
/
golunar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"os"
"path"
"strconv"
"strings"
cc "github.com/lidaobing/chinese_calendar"
)
var inverse = flag.Bool("i", false, "from lunar to solar")
var leap = flag.Bool("l", false, "leap month")
func printToday() {
today := cc.Today()
fmt.Printf("today:\t%#v\n", today)
}
func printHelp() {
fmt.Printf("Usage: %s today\n", path.Base(os.Args[0]))
fmt.Printf("Usage: %s [options] [YEAR MONTH DAY]\n", path.Base(os.Args[0]))
fmt.Printf("\n")
fmt.Printf("Options:\n")
flag.PrintDefaults()
}
func printSolarCalendar(year, month, day int, isLeap bool) (err error) {
var res cc.ChineseCalendar
res.Year = year
res.Month = month
res.Day = day
res.IsLeapMonth = isLeap
t, err := res.ToTime()
if err != nil {
return err
}
fmt.Printf("Result: %s\n", t)
return nil
}
func printChineseCalendar(year, month, day int) (err error) {
res, err := cc.FromSolarDate(year, month, day)
if err != nil {
return err
}
fmt.Printf("Result:\t%#v\n", res)
return nil
}
func str2int(year, month, day string) (year2, month2, day2 int, err error) {
year2, err = strconv.Atoi(year)
if err != nil {
return
}
month2, err = strconv.Atoi(month)
if err != nil {
return
}
day2, err = strconv.Atoi(day)
if err != nil {
return
}
return
}
func main() {
flag.Usage = func() {
printHelp()
}
flag.Parse()
if len(flag.Args()) == 1 && strings.ToUpper(flag.Arg(0)) == "TODAY" {
printToday()
return
}
if len(flag.Args()) != 3 {
printHelp()
os.Exit(2)
}
year2, month2, day2, err := str2int(flag.Args()[0], flag.Args()[1], flag.Args()[2])
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
if *inverse {
err = printSolarCalendar(year2, month2, day2, *leap)
} else {
err = printChineseCalendar(year2, month2, day2)
}
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}