254 lines
5.8 KiB
Go
254 lines
5.8 KiB
Go
package astro
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
"time"
|
|
)
|
|
|
|
var isFormat bool
|
|
var jieqi string
|
|
|
|
func init() {
|
|
Cmd.PersistentFlags().Float64Var(&lon, "lon", -273, "经度,WGS84坐标系")
|
|
Cmd.PersistentFlags().Float64Var(&lat, "lat", -273, "纬度,WGS84坐标系")
|
|
Cmd.PersistentFlags().Float64Var(&height, "height", 0, "海拔高度")
|
|
CmdSun.Flags().StringVarP(&nowDay, "now", "n", "", "指定现在的时间")
|
|
CmdSun.Flags().BoolVarP(&isTimestamp, "timestamp", "t", false, "是否为时间戳")
|
|
CmdSun.Flags().BoolVarP(&isLive, "live", "v", false, "是否为实时")
|
|
CmdSun.Flags().BoolVarP(&isFormat, "format", "f", false, "格式化输出")
|
|
CmdSun.Flags().StringVarP(&city, "city", "c", "", "城市名")
|
|
|
|
CmdMoon.Flags().StringVarP(&nowDay, "now", "n", "", "指定现在的时间")
|
|
CmdMoon.Flags().BoolVarP(&isTimestamp, "timestamp", "t", false, "是否为时间戳")
|
|
CmdMoon.Flags().BoolVarP(&isLive, "live", "v", false, "是否为实时")
|
|
CmdMoon.Flags().BoolVarP(&isFormat, "format", "f", false, "格式化输出")
|
|
CmdMoon.Flags().StringVarP(&city, "city", "c", "", "城市名")
|
|
|
|
CmdStar.Flags().StringVarP(&nowDay, "now", "n", "", "指定现在的时间")
|
|
CmdStar.Flags().BoolVarP(&isTimestamp, "timestamp", "t", false, "是否为时间戳")
|
|
CmdStar.Flags().BoolVarP(&isLive, "live", "v", false, "是否为实时")
|
|
CmdStar.Flags().BoolVarP(&isFormat, "format", "f", false, "格式化输出")
|
|
CmdStar.Flags().StringVarP(&city, "city", "c", "", "城市名")
|
|
|
|
Cmd.AddCommand(CmdCal, CmdSun, CmdMoon, CmdStar)
|
|
}
|
|
|
|
var Cmd = &cobra.Command{
|
|
Use: "astro",
|
|
Short: "天文计算",
|
|
}
|
|
|
|
var CmdSun = &cobra.Command{
|
|
Use: "sun",
|
|
Short: "太阳计算",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
format := 0
|
|
if isFormat {
|
|
format = 1
|
|
}
|
|
isSet := CliLoadLonLatHeight()
|
|
var now = time.Now()
|
|
var err error
|
|
if nowDay != "" {
|
|
now, err = parseDate(now, nowDay, isTimestamp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
}
|
|
for {
|
|
if isSet {
|
|
fmt.Printf("经度: %f 纬度: %f 海拔: %f\n", lon, lat, height)
|
|
}
|
|
BasicSun(now, uint8(format))
|
|
if isSet {
|
|
SunDetail(now, lon, lat, height, uint8(format))
|
|
}
|
|
if !isLive {
|
|
break
|
|
}
|
|
time.Sleep(time.Nanosecond*
|
|
time.Duration(1000000000-time.Now().Nanosecond()) + 1)
|
|
if nowDay == "" {
|
|
now = time.Now()
|
|
now = now.Add(time.Duration(now.Nanosecond()*-1) * time.Nanosecond)
|
|
} else {
|
|
now = now.Add(time.Second)
|
|
}
|
|
ClearScreen()
|
|
}
|
|
},
|
|
}
|
|
|
|
var CmdMoon = &cobra.Command{
|
|
Use: "moon",
|
|
Short: "月亮计算",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
format := 0
|
|
if isFormat {
|
|
format = 1
|
|
}
|
|
isSet := CliLoadLonLatHeight()
|
|
var now = time.Now()
|
|
var err error
|
|
if nowDay != "" {
|
|
now, err = parseDate(now, nowDay, isTimestamp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
}
|
|
for {
|
|
if isSet {
|
|
fmt.Printf("经度: %f 纬度: %f 海拔: %f\n", lon, lat, height)
|
|
}
|
|
BasicMoon(now, uint8(format))
|
|
if isSet {
|
|
MoonDetail(now, lon, lat, height, uint8(format))
|
|
}
|
|
if !isLive {
|
|
break
|
|
}
|
|
time.Sleep(time.Nanosecond*
|
|
time.Duration(1000000000-time.Now().Nanosecond()) + 1)
|
|
if nowDay == "" {
|
|
now = time.Now()
|
|
now = now.Add(time.Duration(now.Nanosecond()*-1) * time.Nanosecond)
|
|
} else {
|
|
now = now.Add(time.Second)
|
|
}
|
|
ClearScreen()
|
|
}
|
|
},
|
|
}
|
|
|
|
var CmdStar = &cobra.Command{
|
|
Use: "star",
|
|
Short: "星星计算",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) == 0 {
|
|
fmt.Println("请输入星星名字")
|
|
return
|
|
}
|
|
format := 0
|
|
if isFormat {
|
|
format = 1
|
|
}
|
|
isSet := CliLoadLonLatHeight()
|
|
var now = time.Now()
|
|
var err error
|
|
if nowDay != "" {
|
|
now, err = parseDate(now, nowDay, isTimestamp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
}
|
|
for {
|
|
if isSet {
|
|
fmt.Printf("经度: %f 纬度: %f 海拔: %f\n", lon, lat, height)
|
|
}
|
|
BasicStar(now, args[0], uint8(format))
|
|
if isSet {
|
|
StarDetail(now, args[0], lon, lat, height, uint8(format))
|
|
}
|
|
if !isLive {
|
|
break
|
|
}
|
|
time.Sleep(time.Nanosecond*
|
|
time.Duration(1000000000-time.Now().Nanosecond()) + 1)
|
|
if nowDay == "" {
|
|
now = time.Now()
|
|
now = now.Add(time.Duration(now.Nanosecond()*-1) * time.Nanosecond)
|
|
} else {
|
|
now = now.Add(time.Second)
|
|
}
|
|
ClearScreen()
|
|
}
|
|
},
|
|
}
|
|
|
|
var CmdJieqi = &cobra.Command{
|
|
Use: "jq",
|
|
Short: "节气计算",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if len(args) == 0 {
|
|
fmt.Println("请输入年份或节气")
|
|
return
|
|
}
|
|
year := args[0]
|
|
if year == "" {
|
|
year = time.Now().Format("2006")
|
|
}
|
|
year = year[:4]
|
|
fmt.Println("年份: ", year)
|
|
/*
|
|
var jqname = map[string]int{
|
|
"春分": 0,
|
|
"清明": 15,
|
|
"谷雨": 30,
|
|
"立夏": 45,
|
|
"小满": 60,
|
|
"芒种": 75,
|
|
"夏至": 90,
|
|
"小暑": 105,
|
|
"大暑": 120,
|
|
"立秋": 135,
|
|
"处暑": 150,
|
|
"白露": 165,
|
|
"秋分": 180,
|
|
"寒露": 195,
|
|
"霜降": 210,
|
|
"立冬": 225,
|
|
"小雪": 240,
|
|
"大雪": 255,
|
|
"冬至": 270,
|
|
"小寒": 285,
|
|
"大寒": 300,
|
|
"立春": 315,
|
|
"雨水": 330,
|
|
"惊蛰": 345,
|
|
}
|
|
if jieqi != "" {
|
|
if v, ok := jqname[jieqi]; !ok {
|
|
fmt.Println("节气名错误")
|
|
return
|
|
} else {
|
|
fmt.Println("节气名: ", jieqi)
|
|
fmt.Println("时间: ", calendar.JieQi(year, v))
|
|
}
|
|
}
|
|
|
|
*/
|
|
},
|
|
}
|
|
|
|
func CliLoadLonLatHeight() bool {
|
|
if city != "" {
|
|
if !GetFromCity(city) {
|
|
fmt.Println("城市名错误")
|
|
return false
|
|
}
|
|
fmt.Println("城市名: ", city)
|
|
SetLonLatHeight(lon, lat, height)
|
|
return true
|
|
}
|
|
tlon, tlat, theight := lon, lat, height
|
|
LoadLonLatHeight()
|
|
if lon == -273 && lon != tlon {
|
|
lon = tlon
|
|
}
|
|
if lat == -273 && lat != tlat {
|
|
lat = tlat
|
|
}
|
|
if height == 0 && height != theight {
|
|
height = theight
|
|
}
|
|
SetLonLatHeight(lon, lat, height)
|
|
if lon == -273 || lat == -273 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|