feat: Return location info and Hijri date from the prayer times endpoint
This commit is contained in:
+27
-19
@@ -7,6 +7,7 @@ import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"prayertimes/pkg/hijricalendar"
|
||||
"prayertimes/pkg/prayer"
|
||||
)
|
||||
|
||||
@@ -84,11 +85,11 @@ func (Provider) SearchLocations(_ context.Context, _ string) ([]prayer.Location,
|
||||
return nil, fmt.Errorf("failed to search locations: %w", errNotSupported)
|
||||
}
|
||||
|
||||
func (Provider) Get(_ context.Context, _ string) ([]prayer.Times, error) {
|
||||
return nil, fmt.Errorf("failed to get prayer times by location id: %w", errNotSupported)
|
||||
func (Provider) Get(_ context.Context, _ string) (prayer.TimesResult, error) {
|
||||
return prayer.TimesResult{}, fmt.Errorf("failed to get prayer times by location id: %w", errNotSupported)
|
||||
}
|
||||
|
||||
func (Provider) GetByCoords(_ context.Context, coords prayer.Coordinates) ([]prayer.Times, error) {
|
||||
func (Provider) GetByCoords(_ context.Context, coords prayer.Coordinates) (prayer.TimesResult, error) {
|
||||
offset := estimateUTCOffsetHours(coords.Longitude)
|
||||
todayUTC := time.Now().UTC().Truncate(24 * time.Hour)
|
||||
|
||||
@@ -98,18 +99,26 @@ func (Provider) GetByCoords(_ context.Context, coords prayer.Coordinates) ([]pra
|
||||
calculated := prayerTimes(coords.Latitude, coords.Longitude, day)
|
||||
|
||||
results = append(results, prayer.Times{
|
||||
Date: day.Format(time.DateOnly),
|
||||
Fajr: formatHHMM(calculated.Imsak, offset),
|
||||
Sunrise: formatHHMM(calculated.Sunrise, offset),
|
||||
Dhuhr: formatHHMM(calculated.Dhuhr, offset),
|
||||
Asr: formatHHMM(calculated.Asr, offset),
|
||||
Sunset: formatHHMM(calculated.Sunset, offset),
|
||||
Maghrib: formatHHMM(calculated.Maghrib, offset),
|
||||
Isha: formatHHMM(calculated.Isha, offset),
|
||||
Date: day,
|
||||
DateHijri: hijricalendar.ToISODate(day),
|
||||
Fajr: derefOrZero(calculated.Imsak),
|
||||
Sunrise: derefOrZero(calculated.Sunrise),
|
||||
Dhuhr: derefOrZero(calculated.Dhuhr),
|
||||
Asr: derefOrZero(calculated.Asr),
|
||||
Sunset: derefOrZero(calculated.Sunset),
|
||||
Maghrib: derefOrZero(calculated.Maghrib),
|
||||
Isha: derefOrZero(calculated.Isha),
|
||||
})
|
||||
}
|
||||
|
||||
return results, nil
|
||||
return prayer.TimesResult{
|
||||
Location: prayer.Location{
|
||||
Latitude: coords.Latitude,
|
||||
Longitude: coords.Longitude,
|
||||
Timezone: formatUTCOffsetTimezone(offset),
|
||||
},
|
||||
Times: results,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Convert a calendar date to a Julian Day Number.
|
||||
@@ -290,16 +299,15 @@ func prayerTimes(lat, lon float64, d time.Time) computedTimes {
|
||||
}
|
||||
}
|
||||
|
||||
// Convert UTC prayer time datetimes to HH:MM strings at a given UTC offset.
|
||||
//
|
||||
// tzOffset is UTC offset in hours, e.g. 3 for Turkey (UTC+3).
|
||||
func formatHHMM(dt *time.Time, tzOffset float64) string {
|
||||
func derefOrZero(dt *time.Time) time.Time {
|
||||
if dt == nil {
|
||||
return ""
|
||||
return time.Time{}
|
||||
}
|
||||
return dt.UTC()
|
||||
}
|
||||
|
||||
tz := time.FixedZone("estimated", int(tzOffset*3600))
|
||||
return dt.In(tz).Add(30 * time.Second).Format("15:04")
|
||||
func formatUTCOffsetTimezone(offset float64) string {
|
||||
return fmt.Sprintf("UTC%+d", int(offset))
|
||||
}
|
||||
|
||||
func estimateUTCOffsetHours(longitude float64) float64 {
|
||||
|
||||
Reference in New Issue
Block a user