mirror of
https://github.com/sstent/go-garth.git
synced 2026-02-02 04:21:43 +00:00
Fix: Garmin Connect authentication and profile fetching (profile URL and GarminTime parsing)
This commit is contained in:
@@ -1,12 +1,45 @@
|
||||
package types
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GarminTime represents Garmin's timestamp format with custom JSON parsing
|
||||
type GarminTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
// It parses Garmin's specific timestamp format.
|
||||
func (gt *GarminTime) UnmarshalJSON(b []byte) (err error) {
|
||||
s := strings.Trim(string(b), `"`)
|
||||
if s == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Try parsing with milliseconds (e.g., "2018-09-01T00:13:25.000")
|
||||
// Garmin sometimes returns .0 for milliseconds, which Go's time.Parse handles as .000
|
||||
// The 'Z' in the layout indicates a UTC time without a specific offset, which is often how these are interpreted.
|
||||
// If the input string does not contain 'Z', it will be parsed as local time.
|
||||
// For consistency, we'll assume UTC if no timezone is specified.
|
||||
layouts := []string{
|
||||
"2006-01-02T15:04:05.0", // Example: 2018-09-01T00:13:25.0
|
||||
"2006-01-02T15:04:05", // Example: 2018-09-01T00:13:25
|
||||
"2006-01-02", // Example: 2018-09-01
|
||||
}
|
||||
|
||||
for _, layout := range layouts {
|
||||
if t, err := time.Parse(layout, s); err == nil {
|
||||
gt.Time = t
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot parse %q into a GarminTime", s)
|
||||
}
|
||||
|
||||
// SessionData represents saved session information
|
||||
type SessionData struct {
|
||||
Domain string `json:"domain"`
|
||||
|
||||
Reference in New Issue
Block a user