fix: Return date as a string instead of local time

This commit is contained in:
2023-04-05 07:48:19 +02:00
parent 64a4814eef
commit ccc04dd9ee
6 changed files with 87 additions and 28 deletions
+58 -7
View File
@@ -2,6 +2,8 @@ package prayer
import (
"context"
"database/sql/driver"
"encoding/json"
"errors"
"time"
)
@@ -23,12 +25,61 @@ type LocationTimesProvider interface {
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 {
Date time.Time `json:"date"`
Fajr string `json:"fajr"`
Sunrise string `json:"sunrise"`
Dhuhr string `json:"dhuhr"`
Asr string `json:"asr"`
Maghrib string `json:"maghrib"`
Isha string `json:"isha"`
Date string `json:"date"`
Fajr string `json:"fajr"`
Sunrise string `json:"sunrise"`
Dhuhr string `json:"dhuhr"`
Asr string `json:"asr"`
Maghrib string `json:"maghrib"`
Isha string `json:"isha"`
}