Change Now() and DefaultClock to use UTC instead of local time
Changed quando.Now() and DefaultClock.Now() to return dates in UTC
timezone instead of local server timezone. This aligns with industry
standards for date/time libraries and prevents server-timezone-dependent
behavior.
Changes:
- date.go: Now() uses time.Now().UTC() instead of time.Now()
- Updated documentation comments to reflect UTC default
- date_test.go: TestNow() now verifies UTC location
- clock_test.go: TestDefaultClock_Now() now verifies UTC location
- parse_test.go: TestParseRelative() verifies all results are UTC
- parse.go: Updated comment from "local timezone" to "UTC timezone"
Users needing local time can use:
- quando.From(time.Now()) for explicit local time
- quando.Now().In("Europe/Berlin") to convert to specific timezone
Closes: quando-67n
This commit is contained in:
parent
f8c486132d
commit
f47897f3fd
6 changed files with 46 additions and 10 deletions
|
|
@ -738,8 +738,7 @@ func TestParseRelativeErrors(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestParseRelative(t *testing.T) {
|
||||
// Test the production function (uses system clock)
|
||||
// Just verify it doesn't error on valid inputs
|
||||
// Test the production function (uses system clock with UTC)
|
||||
validInputs := []string{
|
||||
"today",
|
||||
"tomorrow",
|
||||
|
|
@ -751,12 +750,38 @@ func TestParseRelative(t *testing.T) {
|
|||
|
||||
for _, input := range validInputs {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
_, err := ParseRelative(input)
|
||||
result, err := ParseRelative(input)
|
||||
if err != nil {
|
||||
t.Errorf("ParseRelative(%q) unexpected error: %v", input, err)
|
||||
}
|
||||
|
||||
// Verify result is in UTC timezone
|
||||
if result.Time().Location() != time.UTC {
|
||||
t.Errorf("ParseRelative(%q) location = %v, want UTC", input, result.Time().Location())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Specifically test "today" to verify UTC behavior
|
||||
t.Run("today returns UTC date", func(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
|
||||
|
||||
result, err := ParseRelative("today")
|
||||
if err != nil {
|
||||
t.Fatalf("ParseRelative(\"today\") error: %v", err)
|
||||
}
|
||||
|
||||
// Expect UTC location
|
||||
if result.Time().Location() != time.UTC {
|
||||
t.Errorf("ParseRelative(\"today\").Location() = %v, want UTC", result.Time().Location())
|
||||
}
|
||||
|
||||
// Verify it matches expected date
|
||||
if !result.Time().Equal(today) {
|
||||
t.Errorf("ParseRelative(\"today\") = %v, want %v", result.Time(), today)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseRelativeImmutability(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue