Added custom date type
This commit is contained in:
@@ -128,9 +128,8 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Format UNIX timestamp as 'YYYY-MM-DD'
|
// Format UNIX timestamp as 'YYYY-MM-DD'
|
||||||
// unixTs, _ := strconv.Atoi(weather.Current.Timestamp)
|
|
||||||
utcTime := time.Unix(int64(weather.Current.Timestamp), 0)
|
utcTime := time.Unix(int64(weather.Current.Timestamp), 0)
|
||||||
weatherDate := utcTime.UTC()
|
weatherDate := &types.ZephyrDate{Time: utcTime.UTC()}
|
||||||
|
|
||||||
// Set condition accordingly to weather description
|
// Set condition accordingly to weather description
|
||||||
var condition string
|
var condition string
|
||||||
|
|||||||
35
types/date.go
Normal file
35
types/date.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ZephyrDate struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (date *ZephyrDate) UnmarshalJSON(b []byte) error {
|
||||||
|
s := strings.Trim(string(b), "\"")
|
||||||
|
if s == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
date.Time, err = time.Parse("Monday, 2006/01/02", s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (date *ZephyrDate) MarshalJSON() ([]byte, error) {
|
||||||
|
if date.Time.IsZero() {
|
||||||
|
return []byte("\"\""), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmtDate := date.Time.Format("Monday, 2006/01/02")
|
||||||
|
|
||||||
|
return []byte("\"" + fmtDate + "\""), nil
|
||||||
|
}
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
// The Weather data type, representing the weather of a certain city
|
// The Weather data type, representing the weather of a certain city
|
||||||
type Weather struct {
|
type Weather struct {
|
||||||
Date time.Time `json:"date"`
|
Date *ZephyrDate `json:"date"`
|
||||||
Temperature string `json:"temperature"`
|
Temperature string `json:"temperature"`
|
||||||
Condition string `json:"condition"`
|
Condition string `json:"condition"`
|
||||||
FeelsLike string `json:"feelsLike"`
|
FeelsLike string `json:"feelsLike"`
|
||||||
Emoji string `json:"emoji"`
|
Emoji string `json:"emoji"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user