Added sunrise data
This commit is contained in:
parent
b89d04e581
commit
fe800b7632
83
astroinfo.go
83
astroinfo.go
|
@ -119,7 +119,7 @@ func (a *AstronomicalInfo) SunsetByTime(t time.Time) DateTime {
|
|||
return DateTime{na: true}
|
||||
}
|
||||
return DateTime{
|
||||
dt: a.Run,
|
||||
dt: cdd.DateTime.Time,
|
||||
n: FieldSunset,
|
||||
s: SourceForecast,
|
||||
dv: *cdd.Sunset,
|
||||
|
@ -146,3 +146,84 @@ func (a *AstronomicalInfo) SunsetByDateString(ds string) DateTime {
|
|||
}
|
||||
return a.SunsetByTime(t)
|
||||
}
|
||||
|
||||
// SunsetAll returns a slice of all sunset data points in the given
|
||||
// AstronomicalInfo instance as DateTime types. If no sunset data
|
||||
// is available it will return an empty slice
|
||||
func (a *AstronomicalInfo) SunsetAll() []DateTime {
|
||||
var sss []DateTime
|
||||
for _, cd := range a.DailyData {
|
||||
if cd.DateTime.IsZero() {
|
||||
continue
|
||||
}
|
||||
sss = append(sss, a.SunsetByTime(cd.DateTime.Time))
|
||||
}
|
||||
|
||||
return sss
|
||||
}
|
||||
|
||||
// SunriseByTime returns the date and time of the sunrise 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.
|
||||
//
|
||||
// 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) SunriseByTime(t time.Time) DateTime {
|
||||
if len(a.DailyData) < 1 {
|
||||
return DateTime{na: true}
|
||||
}
|
||||
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{
|
||||
dt: cdd.DateTime.Time,
|
||||
n: FieldSunrise,
|
||||
s: SourceForecast,
|
||||
dv: *cdd.Sunrise,
|
||||
}
|
||||
}
|
||||
|
||||
// Sunrise returns the date and time of the sunrise 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) Sunrise() DateTime {
|
||||
return a.SunriseByTime(time.Now())
|
||||
}
|
||||
|
||||
// SunriseByDateString returns the date and time of the sunrise 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) SunriseByDateString(ds string) DateTime {
|
||||
t, err := time.Parse("2006-01-02", ds)
|
||||
if err != nil {
|
||||
return DateTime{na: true}
|
||||
}
|
||||
return a.SunriseByTime(t)
|
||||
}
|
||||
|
||||
// SunriseAll returns a slice of all sunrise data points in the given
|
||||
// AstronomicalInfo instance as DateTime types. If no sunrise data
|
||||
// is available it will return an empty slice
|
||||
func (a *AstronomicalInfo) SunriseAll() []DateTime {
|
||||
var sss []DateTime
|
||||
for _, cd := range a.DailyData {
|
||||
if cd.DateTime.IsZero() {
|
||||
continue
|
||||
}
|
||||
sss = append(sss, a.SunriseByTime(cd.DateTime.Time))
|
||||
}
|
||||
|
||||
return sss
|
||||
}
|
||||
|
|
|
@ -103,6 +103,7 @@ func TestAstronomicalInfo_SunsetByDateString(t *testing.T) {
|
|||
return
|
||||
}
|
||||
ti := time.Date(2023, 5, 28, 21, 16, 37, 0, loc)
|
||||
ddt := time.Date(2023, 5, 28, 0, 0, 0, 0, time.UTC)
|
||||
c := New(withMockAPI())
|
||||
ai, err := c.AstronomicalInfoByCoordinates(la, lo)
|
||||
if err != nil {
|
||||
|
@ -125,6 +126,10 @@ func TestAstronomicalInfo_SunsetByDateString(t *testing.T) {
|
|||
t.Errorf("SunsetByTime failed, expected sunset: %s, got: %s",
|
||||
ti.Format(time.RFC3339), ai.SunsetByTime(ti).String())
|
||||
}
|
||||
if ai.SunsetByTime(ti).DateTime().Format(time.RFC3339) != ddt.Format(time.RFC3339) {
|
||||
t.Errorf("SunsetByTime failed, expected sunset: %s, got: %s",
|
||||
ddt.Format(time.RFC3339), ai.SunsetByTime(ti).DateTime().Format(time.RFC3339))
|
||||
}
|
||||
if ai.SunsetByDateString(ti.Format(DateFormat)).Value().UnixMilli() != ti.UnixMilli() {
|
||||
t.Errorf("SunsetByDateString failed, expected sunset: %s, got: %s",
|
||||
ti.String(), ai.SunsetByDateString(ti.Format(DateFormat)).Value().String())
|
||||
|
@ -139,3 +144,54 @@ func TestAstronomicalInfo_SunsetByDateString(t *testing.T) {
|
|||
ai.SunsetByTime(ti).Value().String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAstronomicalInfo_SunriseByDateString(t *testing.T) {
|
||||
la := 52.5067296
|
||||
lo := 13.2599306
|
||||
loc, err := time.LoadLocation("Europe/Berlin")
|
||||
if err != nil {
|
||||
t.Errorf("failed to load time location data for Europe/Berlin: %s", err)
|
||||
return
|
||||
}
|
||||
ti := time.Date(2023, 5, 28, 4, 51, 48, 0, loc)
|
||||
ddt := time.Date(2023, 5, 28, 0, 0, 0, 0, time.UTC)
|
||||
c := New(withMockAPI())
|
||||
ai, err := c.AstronomicalInfoByCoordinates(la, lo)
|
||||
if err != nil {
|
||||
t.Errorf("failed to fetch astronomical information: %s", err)
|
||||
return
|
||||
}
|
||||
if !ai.SunriseByTime(ti).IsAvailable() {
|
||||
t.Errorf("SunriseByTime failed, expected entry, but got 'not available'")
|
||||
return
|
||||
}
|
||||
if ai.SunriseByTime(ti).Value().UnixMilli() != ti.UnixMilli() {
|
||||
t.Errorf("SunriseByTime failed, expected sunrise: %s, got: %s",
|
||||
ti.String(), ai.SunriseByTime(ti).Value().String())
|
||||
}
|
||||
if !ai.SunriseByDateString(ti.Format(DateFormat)).IsAvailable() {
|
||||
t.Errorf("SunriseByDateString failed, expected entry, but got 'not available'")
|
||||
return
|
||||
}
|
||||
if ai.SunriseByTime(ti).String() != ti.Format(time.RFC3339) {
|
||||
t.Errorf("SunriseByTime failed, expected sunrise: %s, got: %s",
|
||||
ti.Format(time.RFC3339), ai.SunriseByTime(ti).String())
|
||||
}
|
||||
if ai.SunriseByTime(ti).DateTime().Format(time.RFC3339) != ddt.Format(time.RFC3339) {
|
||||
t.Errorf("SunriseByTime failed, expected sunrise: %s, got: %s",
|
||||
ddt.Format(time.RFC3339), ai.SunriseByTime(ti).DateTime().Format(time.RFC3339))
|
||||
}
|
||||
if ai.SunriseByDateString(ti.Format(DateFormat)).Value().UnixMilli() != ti.UnixMilli() {
|
||||
t.Errorf("SunriseByDateString failed, expected sunrise: %s, got: %s",
|
||||
ti.String(), ai.SunriseByDateString(ti.Format(DateFormat)).Value().String())
|
||||
}
|
||||
ti = time.Time{}
|
||||
if ai.SunriseByTime(ti).IsAvailable() {
|
||||
t.Errorf("SunriseByTime failed, expected no entry, but got: %s",
|
||||
ai.SunriseByTime(ti).Value().String())
|
||||
}
|
||||
if !ai.SunriseByTime(ti).Value().IsZero() {
|
||||
t.Errorf("SunriseByTime failed, expected no entry, but got: %s",
|
||||
ai.SunriseByTime(ti).Value().String())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,8 @@ const (
|
|||
FieldPressureMSL
|
||||
// FieldPressureQFE represents the PressureQFE data point
|
||||
FieldPressureQFE
|
||||
// FieldSunrise represents the Sunrise data point
|
||||
FieldSunrise
|
||||
// FieldSunset represents the Sunset data point
|
||||
FieldSunset
|
||||
// FieldTemperature represents the Temperature data point
|
||||
|
|
|
@ -18,6 +18,11 @@ func (dt DateTime) IsAvailable() bool {
|
|||
return !dt.na
|
||||
}
|
||||
|
||||
// DateTime returns the timestamp for that specific DateTime type
|
||||
func (dt DateTime) DateTime() time.Time {
|
||||
return dt.dt
|
||||
}
|
||||
|
||||
// Value returns the time.Time value of an DateTime value
|
||||
// If the DateTime is not available in the WeatherData
|
||||
// Value will return time.Time with a zero value instead.
|
||||
|
|
Loading…
Reference in a new issue