fix: Return date as a string instead of local time
This commit is contained in:
@@ -38,11 +38,12 @@ func newServices() (api.Services, func(), error) {
|
|||||||
return api.Services{}, nil, fmt.Errorf("failed to init db: %w", err)
|
return api.Services{}, nil, fmt.Errorf("failed to init db: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dbProvider := dbtimesprovider.New(db, diyanetAPIProvider)
|
|
||||||
if err := dbtimesprovider.Migrate(db.Db.(*sql.DB)); err != nil {
|
if err := dbtimesprovider.Migrate(db.Db.(*sql.DB)); err != nil {
|
||||||
return api.Services{}, nil, fmt.Errorf("failed to migrate database: %w", err)
|
return api.Services{}, nil, fmt.Errorf("failed to migrate database: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbProvider := dbtimesprovider.New(db, diyanetAPIProvider)
|
||||||
|
|
||||||
return api.Services{
|
return api.Services{
|
||||||
TimesProvider: dbProvider,
|
TimesProvider: dbProvider,
|
||||||
LocationTimesProvider: diyanetAPIProvider,
|
LocationTimesProvider: diyanetAPIProvider,
|
||||||
|
|||||||
@@ -42,18 +42,19 @@ func (p Provider) Name() string {
|
|||||||
|
|
||||||
func (p Provider) Get(ctx context.Context, location string) ([]prayer.Times, error) {
|
func (p Provider) Get(ctx context.Context, location string) ([]prayer.Times, error) {
|
||||||
times, err := p.loadTimes(ctx, location)
|
times, err := p.loadTimes(ctx, location)
|
||||||
if err == nil && len(times) != 0 {
|
if err != nil {
|
||||||
return times, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to load prayer times from db: %w", err)
|
return nil, fmt.Errorf("failed to load prayer times from db: %w", err)
|
||||||
}
|
}
|
||||||
|
if len(times) > 0 {
|
||||||
|
return times, nil
|
||||||
|
}
|
||||||
|
|
||||||
times, err = p.provider.Get(ctx, location)
|
times, err = p.provider.Get(ctx, location)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get prayer times: %w", err)
|
return nil, fmt.Errorf("failed to get prayer times: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(times) > 1 {
|
if len(times) > 0 {
|
||||||
if err := p.saveTimes(ctx, location, times); err != nil {
|
if err := p.saveTimes(ctx, location, times); err != nil {
|
||||||
return nil, fmt.Errorf("failed to save times to db: %w", err)
|
return nil, fmt.Errorf("failed to save times to db: %w", err)
|
||||||
}
|
}
|
||||||
@@ -112,7 +113,7 @@ func Migrate(con *sql.DB) error {
|
|||||||
type prayerTimesRow struct {
|
type prayerTimesRow struct {
|
||||||
ProviderID int64 `db:"provider_id"`
|
ProviderID int64 `db:"provider_id"`
|
||||||
LocationID string `db:"location_id"`
|
LocationID string `db:"location_id"`
|
||||||
Date time.Time `db:"date"`
|
Date string `db:"date"`
|
||||||
Fajr string `db:"fajr"`
|
Fajr string `db:"fajr"`
|
||||||
Sunrise string `db:"sunrise"`
|
Sunrise string `db:"sunrise"`
|
||||||
Dhuhr string `db:"dhuhr"`
|
Dhuhr string `db:"dhuhr"`
|
||||||
@@ -166,7 +167,7 @@ func (p Provider) saveTimes(ctx context.Context, locationID string, times []pray
|
|||||||
|
|
||||||
func (p Provider) loadTimes(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
func (p Provider) loadTimes(ctx context.Context, locationID string) ([]prayer.Times, error) {
|
||||||
now := p.clockFunc()
|
now := p.clockFunc()
|
||||||
today := now.Truncate(time.Hour * 24)
|
today := now.UTC().Truncate(time.Hour * 24)
|
||||||
|
|
||||||
q := p.db.
|
q := p.db.
|
||||||
From(goqu.T("prayer_times").As("pt")).
|
From(goqu.T("prayer_times").As("pt")).
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func (d Provider) Get(ctx context.Context, location string) ([]prayer.Times, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
row := prayer.Times{
|
row := prayer.Times{
|
||||||
Date: parsedDate,
|
Date: parsedDate.Format(time.DateOnly),
|
||||||
Fajr: el.Find("td:nth-of-type(2)").Text(),
|
Fajr: el.Find("td:nth-of-type(2)").Text(),
|
||||||
Sunrise: el.Find("td:nth-of-type(3)").Text(),
|
Sunrise: el.Find("td:nth-of-type(3)").Text(),
|
||||||
Dhuhr: el.Find("td:nth-of-type(4)").Text(),
|
Dhuhr: el.Find("td:nth-of-type(4)").Text(),
|
||||||
|
|||||||
@@ -73,17 +73,21 @@ func (d Provider) parseResponse(res *req.Response) ([]prayer.Times, error) {
|
|||||||
return nil, fmt.Errorf("received error: %s", res.String())
|
return nil, fmt.Errorf("received error: %s", res.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(response.ResultObject.PrayerTimes) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
var times []prayer.Times
|
var times []prayer.Times
|
||||||
const format = "15:04"
|
const format = "15:04"
|
||||||
now := time.Now()
|
today := time.Now().UTC().Truncate(time.Hour * 24)
|
||||||
for _, pt := range response.ResultObject.PrayerTimes {
|
for _, pt := range response.ResultObject.PrayerTimes {
|
||||||
then := pt.Date.UTC().Truncate(time.Hour * 24)
|
then := prayer.Date(pt.Date).Time()
|
||||||
if then.Before(now) {
|
if then.Before(today) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
times = append(times, prayer.Times{
|
times = append(times, prayer.Times{
|
||||||
Date: then,
|
Date: pt.Date.Format(time.DateOnly),
|
||||||
Fajr: pt.Fajr.Format(format),
|
Fajr: pt.Fajr.Format(format),
|
||||||
Sunrise: pt.Sunrise.Format(format),
|
Sunrise: pt.Sunrise.Format(format),
|
||||||
Dhuhr: pt.Dhuhr.Format(format),
|
Dhuhr: pt.Dhuhr.Format(format),
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ func TestDiyanetAPI_GetByCoords(t *testing.T) {
|
|||||||
times, err := p.Get(context.Background(), "11104")
|
times, err := p.Get(context.Background(), "11104")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotEmpty(t, times)
|
assert.NotEmpty(t, times)
|
||||||
t.Logf("%#+v", times[0])
|
for _, time := range times {
|
||||||
|
t.Log(time)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-1
@@ -2,6 +2,8 @@ package prayer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql/driver"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -23,8 +25,57 @@ type LocationTimesProvider interface {
|
|||||||
Name() string
|
Name() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Date time.Time
|
||||||
|
|
||||||
|
func (d Date) String() string {
|
||||||
|
return time.Time(d).Format(time.DateOnly)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Date) Scan(src any) error {
|
||||||
|
switch v := src.(type) {
|
||||||
|
case []byte:
|
||||||
|
return json.Unmarshal(v, d)
|
||||||
|
case string:
|
||||||
|
return json.Unmarshal([]byte(v), d)
|
||||||
|
case time.Time:
|
||||||
|
*d = Date(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Date) Value() (driver.Value, error) {
|
||||||
|
return json.Marshal(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Date) UnmarshalJSON(bytes []byte) error {
|
||||||
|
var t time.Time
|
||||||
|
if err := json.Unmarshal(bytes, &t); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, offset := t.Zone()
|
||||||
|
t = t.Add(time.Duration(offset * int(time.Second)))
|
||||||
|
t = t.UTC()
|
||||||
|
*d = Date(t)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Date) MarshalJSON() ([]byte, error) {
|
||||||
|
t := time.Time(d)
|
||||||
|
return json.Marshal(t.Format(time.DateOnly))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Date) Time() time.Time {
|
||||||
|
t := time.Time(d)
|
||||||
|
_, offset := t.Zone()
|
||||||
|
t = t.Add(time.Duration(offset * int(time.Second)))
|
||||||
|
t = t.UTC()
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
type Times struct {
|
type Times struct {
|
||||||
Date time.Time `json:"date"`
|
Date string `json:"date"`
|
||||||
Fajr string `json:"fajr"`
|
Fajr string `json:"fajr"`
|
||||||
Sunrise string `json:"sunrise"`
|
Sunrise string `json:"sunrise"`
|
||||||
Dhuhr string `json:"dhuhr"`
|
Dhuhr string `json:"dhuhr"`
|
||||||
|
|||||||
Reference in New Issue
Block a user