mirror of
https://github.com/sstent/go-garminconnect.git
synced 2026-02-01 12:01:38 +00:00
ficing all the build errors - checkpoint 2
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -22,6 +23,44 @@ func (t Time) Format(layout string) string {
|
||||
return time.Time(t).Format(layout)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler interface
|
||||
func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Try multiple time formats that Garmin might use
|
||||
formats := []string{
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
"2006-01-02T15:04:05Z",
|
||||
"2006-01-02T15:04:05.000",
|
||||
"2006-01-02T15:04:05",
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
if parsedTime, err := time.Parse(format, s); err == nil {
|
||||
*t = Time(parsedTime)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// If none of the formats work, try parsing as RFC3339
|
||||
parsedTime, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = Time(parsedTime)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler interface
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Time(t).Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// BodyComposition represents body composition metrics from Garmin Connect
|
||||
type BodyComposition struct {
|
||||
BoneMass float64 `json:"boneMass"` // Grams
|
||||
@@ -35,4 +74,4 @@ type BodyComposition struct {
|
||||
type BodyCompositionRequest struct {
|
||||
StartDate Time `json:"startDate"`
|
||||
EndDate Time `json:"endDate"`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user