More astronomical data

This commit is contained in:
Winni Neessen 2023-05-27 18:41:05 +02:00
parent 56b17ffcf3
commit 7ffd3943e0
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 41 additions and 7 deletions

View file

@ -96,17 +96,26 @@ func (c *Client) AstronomicalInfoByLocation(lo string) (AstronomicalInfo, error)
return c.AstronomicalInfoByCoordinates(gl.Latitude, gl.Longitude)
}
// Sunset returns the date and time of the sunset on the current day
// as DateTime type
// SunsetByTime returns the date and time of the sunset on the give
// time as DateTime type.
// If the data point is not available in the AstronomicalInfo it will
// return DateTime in which the "not available" field will be true.
func (a *AstronomicalInfo) Sunset() DateTime {
n := time.Now()
//
// Please keep in mind that the API only returns 14 days in the future.
// Any date given that exceeds that time, wil always return a
// "not available" value.
func (a *AstronomicalInfo) SunsetByTime(t time.Time) DateTime {
if len(a.DailyData) < 1 {
return DateTime{na: true}
}
cdd := a.DailyData[0]
if cdd.DateTime.Format("2006-01-02") != n.Format("2006-01-02") {
var cdd APIAstronomicalDailyData
for i := range a.DailyData {
if a.DailyData[i].DateTime.Format(DateFormat) != t.Format(DateFormat) {
continue
}
cdd = a.DailyData[i]
}
if cdd.DateTime.IsZero() {
return DateTime{na: true}
}
return DateTime{
@ -116,3 +125,24 @@ func (a *AstronomicalInfo) Sunset() DateTime {
dv: *cdd.Sunset,
}
}
// Sunset returns the date and time of the sunset on the current date
// as DateTime type.
// If the data point is not available in the AstronomicalInfo it will
// return DateTime in which the "not available" field will be true.
func (a *AstronomicalInfo) Sunset() DateTime {
return a.SunsetByTime(time.Now())
}
// SunsetByDateString returns the date and time of the sunset at a
// given date string as DateTime type. Expected format is 2006-01-02.
// If the date wasn't able to be parsed or if the data point is not
// available in the AstronomicalInfo it will return DateTime in
// which the "not available" field will be true.
func (a *AstronomicalInfo) SunsetByDateString(ds string) DateTime {
t, err := time.Parse("2006-01-02", ds)
if err != nil {
return DateTime{na: true}
}
return a.SunsetByTime(t)
}

View file

@ -14,6 +14,10 @@ import (
// data point is not available
const DataUnavailable = "Data unavailable"
// DateFormat is the parsing format that is used for datetime strings
// that only hold the date but no time
const DateFormat = "2006-02-01"
// Enum for different Fieldname values
const (
// FieldDewpoint represents the Dewpoint data point
@ -128,7 +132,7 @@ func (a *APIDate) UnmarshalJSON(s []byte) error {
var err error
switch len(d) {
case 10:
pd, err = time.Parse("2006-01-02", d)
pd, err = time.Parse(DateFormat, d)
case 20:
pd, err = time.Parse(time.RFC3339, d)
default: