Repository: JanDeDobbeleer/oh-my-posh
Pull request #2561 (2022-07-22 18:27:13)

withings fixes
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

### Description

- fix(withings): pick today's steps
- fix(withings): calculate total sleep time

relates to #2551


<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/segments/withings.go b/src/segments/withings.go
index 3f5c4ea6..b156153d 100644
--- a/src/segments/withings.go
+++ b/src/segments/withings.go
@@ -104,11 +104,16 @@ func (w *withingsAPI) GetActivities(activities string) (*WithingsData, error) {
 }
 
 func (w *withingsAPI) GetSleep() (*WithingsData, error) {
-	now := time.Now()
+	today := time.Now()
+	yesterday := today.AddDate(0, 0, -1)
+	// start from 21:00 yesterday
+	start := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 21, 0, 0, 0, time.UTC).Unix()
+	// end at 12PM today
+	end := time.Date(today.Year(), today.Month(), today.Day(), 12, 0, 0, 0, time.UTC).Unix()
 	formData := url.Values{
 		"action":    {"get"},
-		"startdate": {strconv.FormatInt(now.AddDate(0, -1, 0).Unix(), 10)},
-		"enddate":   {strconv.FormatInt(now.Unix(), 10)},
+		"startdate": {strconv.FormatInt(start, 10)},
+		"enddate":   {strconv.FormatInt(end, 10)},
 	}
 	return w.getWithingsData("https://wbsapi.withings.net/v2/sleep", formData)
 }
@@ -179,8 +184,15 @@ func (w *Withings) getActivities() bool {
 	if err != nil || len(data.Body.Activities) == 0 {
 		return false
 	}
-	w.Steps = data.Body.Activities[0].Steps
-	return true
+	today := time.Now().Format("2006-01-02")
+	for _, activity := range data.Body.Activities {
+		if activity.Date != today {
+			continue
+		}
+		w.Steps = activity.Steps
+		return true
+	}
+	return false
 }
 
 func (w *Withings) getSleep() bool {
@@ -188,8 +200,17 @@ func (w *Withings) getSleep() bool {
 	if err != nil || len(data.Body.Series) == 0 {
 		return false
 	}
-	sleepStart := time.Unix(data.Body.Series[0].Startdate, 0)
-	sleepEnd := time.Unix(data.Body.Series[0].Enddate, 0)
+	var sleepStart, sleepEnd time.Time
+	for _, series := range data.Body.Series {
+		start := time.Unix(series.Startdate, 0)
+		if sleepStart.IsZero() || start.Before(sleepStart) {
+			sleepStart = start
+		}
+		end := time.Unix(series.Enddate, 0)
+		if sleepStart.IsZero() || start.After(sleepEnd) {
+			sleepEnd = end
+		}
+	}
 	sleepHours := sleepEnd.Sub(sleepStart).Hours()
 	w.SleepHours = fmt.Sprintf("%0.1f", sleepHours)
 	return true


Test patch:
diff --git a/src/segments/withings_test.go b/src/segments/withings_test.go
index 13d87c94..02fe209c 100644
--- a/src/segments/withings_test.go
+++ b/src/segments/withings_test.go
@@ -5,6 +5,7 @@ import (
 	"oh-my-posh/mock"
 	"oh-my-posh/properties"
 	"testing"
+	"time"
 
 	"github.com/stretchr/testify/assert"
 	mock2 "github.com/stretchr/testify/mock"
@@ -84,8 +85,13 @@ func TestWithingsSegment(t *testing.T) {
 			WithingsData: &WithingsData{
 				Body: &Body{
 					Activities: []*Activity{
+						{
+							Steps: 5066,
+							Date:  time.Now().AddDate(0, 0, -1).Format("2006-01-02"),
+						},
 						{
 							Steps: 7077,
+							Date:  time.Now().Format("2006-01-02"),
 						},
 					},
 				},
@@ -127,6 +133,7 @@ func TestWithingsSegment(t *testing.T) {
 					Activities: []*Activity{
 						{
 							Steps: 976,
+							Date:  time.Now().Format("2006-01-02"),
 						},
 					},
 				},