You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/astro/astro.go

75 lines
1.3 KiB
Go

5 days ago
package astro
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
)
//go:embed city.json
var cityByte []byte
type City struct {
Code int `json:"code"`
CityName string `json:"city_name"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
var lon, lat, height, loc float64
var city string
func SetLonLatHeight(lo, la, he float64) error {
lon, lat, height = lo, la, he
return os.WriteFile(".ast.env", []byte(fmt.Sprintf("%f,%f,%f,%f", lon, lat, height, loc)), 0644)
}
func LoadLonLatHeight() error {
if _, err := os.Stat(".ast.env"); errors.Is(err, os.ErrNotExist) {
return nil
}
b, err := os.ReadFile(".ast.env")
if err != nil {
return err
}
_, err = fmt.Sscanf(string(b), "%f,%f,%f,%f", &lon, &lat, &height, &loc)
return err
}
func GetLonLatHeight() (float64, float64, float64) {
return lon, lat, height
}
func Lon() float64 {
return lon
}
func Lat() float64 {
return lat
}
func Height() float64 {
return height
}
func GetFromCity(name string) bool {
var c []City
err := json.Unmarshal(cityByte, &c)
if err != nil {
return false
}
for _, v := range c {
if strings.Contains(v.CityName, name) {
lon, lat = v.Lon, v.Lat
loc = 8
time.Local = time.FixedZone("Local", int(loc*3600))
return true
}
}
return false
}