优化定时抢购界面
This commit is contained in:
@@ -593,6 +593,11 @@ func parseStartTime(text string) (time.Time, error) {
|
||||
if ts, err := strconv.ParseInt(text, 10, 64); err == nil {
|
||||
return time.Unix(ts, 0), nil
|
||||
}
|
||||
for _, layout := range []string{"15:04:05", "15:04"} {
|
||||
if t, err := time.ParseInLocation(layout, text, time.Local); err == nil {
|
||||
return nextClockTime(time.Now(), t.Hour(), t.Minute(), t.Second()), nil
|
||||
}
|
||||
}
|
||||
for _, layout := range []string{time.RFC3339, "2006-01-02 15:04:05", "2006-01-02 15:04"} {
|
||||
if t, err := time.ParseInLocation(layout, text, time.Local); err == nil {
|
||||
return t, nil
|
||||
@@ -601,6 +606,14 @@ func parseStartTime(text string) (time.Time, error) {
|
||||
return time.Time{}, fmt.Errorf("无法解析开始时间: %s", text)
|
||||
}
|
||||
|
||||
func nextClockTime(now time.Time, hour, minute, second int) time.Time {
|
||||
target := time.Date(now.Year(), now.Month(), now.Day(), hour, minute, second, 0, now.Location())
|
||||
if !target.After(now) {
|
||||
target = target.Add(24 * time.Hour)
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
func waitUntil(ctx context.Context, when time.Time) error {
|
||||
for {
|
||||
remaining := time.Until(when)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package gui
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSplitCookieLinesNormalizesLineBreaks(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -44,3 +47,33 @@ func TestSplitCookieLinesNormalizesLineBreaks(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseStartTimeAcceptsClockOnly(t *testing.T) {
|
||||
t.Run("future_today", func(t *testing.T) {
|
||||
now := time.Date(2026, 7, 6, 10, 20, 30, 0, time.Local)
|
||||
got := nextClockTime(now, 12, 5, 6)
|
||||
want := time.Date(2026, 7, 6, 12, 5, 6, 0, time.Local)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("nextClockTime() = %s, want %s", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("past_tomorrow", func(t *testing.T) {
|
||||
now := time.Date(2026, 7, 6, 10, 20, 30, 0, time.Local)
|
||||
got := nextClockTime(now, 9, 5, 6)
|
||||
want := time.Date(2026, 7, 7, 9, 5, 6, 0, time.Local)
|
||||
if !got.Equal(want) {
|
||||
t.Fatalf("nextClockTime() = %s, want %s", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("parse_hms", func(t *testing.T) {
|
||||
got, err := parseStartTime("23:59:58")
|
||||
if err != nil {
|
||||
t.Fatalf("parseStartTime() error = %v", err)
|
||||
}
|
||||
if got.Hour() != 23 || got.Minute() != 59 || got.Second() != 58 {
|
||||
t.Fatalf("parseStartTime() = %s, want 23:59:58", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user