From 6bb69b0c27435ad613325586db6233a5184727a1 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 25 Dec 2025 00:56:39 +0800 Subject: [PATCH] fix daylight saving time is not calculated correctly when checking whether a transaction can be edited --- pkg/models/user.go | 18 +++++++++--------- pkg/utils/datetimes.go | 6 ++++++ pkg/utils/datetimes_test.go | 10 ++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/models/user.go b/pkg/models/user.go index e6e165c7..da6f2ea2 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -242,14 +242,14 @@ func (u *User) CanEditTransactionByTransactionTime(transactionTime int64, client transactionUnixTime := utils.GetUnixTimeFromTransactionTime(transactionTime) if u.TransactionEditScope == TRANSACTION_EDIT_SCOPE_LAST_24H_OR_LATER { - return transactionUnixTime >= now.Unix()-24*60*60 + return transactionUnixTime >= now.Add(-24*time.Hour).Unix() } clientNow := now.In(clientTimezone) - clientTodayFirstUnixTime := clientNow.Unix() - int64(clientNow.Hour()*60*60+clientNow.Minute()*60+clientNow.Second()) + clientTodayStartTime := utils.GetStartOfDay(clientNow) if u.TransactionEditScope == TRANSACTION_EDIT_SCOPE_TODAY_OR_LATER { - return transactionUnixTime >= clientTodayFirstUnixTime + return transactionUnixTime >= clientTodayStartTime.Unix() } else if u.TransactionEditScope == TRANSACTION_EDIT_SCOPE_THIS_WEEK_OR_LATER { dayOfWeek := int(now.Weekday()) - int(u.FirstDayOfWeek) @@ -257,14 +257,14 @@ func (u *User) CanEditTransactionByTransactionTime(transactionTime int64, client dayOfWeek += 7 } - clientWeekFirstUnixTime := clientTodayFirstUnixTime - int64(dayOfWeek*24*60*60) - return transactionUnixTime >= clientWeekFirstUnixTime + clientWeekStartTime := clientTodayStartTime.AddDate(0, 0, -dayOfWeek) + return transactionUnixTime >= clientWeekStartTime.Unix() } else if u.TransactionEditScope == TRANSACTION_EDIT_SCOPE_THIS_MONTH_OR_LATER { - clientMonthFirstUnixTime := clientTodayFirstUnixTime - int64((now.Day()-1)*24*60*60) - return transactionUnixTime >= clientMonthFirstUnixTime + clientMonthStartTime := clientTodayStartTime.AddDate(0, 0, -(now.Day() - 1)) + return transactionUnixTime >= clientMonthStartTime.Unix() } else if u.TransactionEditScope == TRANSACTION_EDIT_SCOPE_THIS_YEAR_OR_LATER { - clientYearFirstUnixTime := clientTodayFirstUnixTime - int64((now.YearDay()-1)*24*60*60) - return transactionUnixTime >= clientYearFirstUnixTime + clientYearStartTime := clientTodayStartTime.AddDate(0, 0, -(now.YearDay() - 1)) + return transactionUnixTime >= clientYearStartTime.Unix() } return false diff --git a/pkg/utils/datetimes.go b/pkg/utils/datetimes.go index 40c7e422..233ab970 100644 --- a/pkg/utils/datetimes.go +++ b/pkg/utils/datetimes.go @@ -414,6 +414,12 @@ func GetTransactionTimeRangeByYearMonth(year int32, month int32) (int64, int64, return minTransactionTime, maxTransactionTime, nil } +// GetStartOfDay returns the start time of the day for the specified time +func GetStartOfDay(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) +} + // parseFromUnixTime parses a unix time and returns a golang time struct func parseFromUnixTime(unixTime int64) time.Time { return time.Unix(unixTime, 0) diff --git a/pkg/utils/datetimes_test.go b/pkg/utils/datetimes_test.go index 262267f9..c25cd185 100644 --- a/pkg/utils/datetimes_test.go +++ b/pkg/utils/datetimes_test.go @@ -516,6 +516,16 @@ func TestGetTransactionTimeRangeByYearMonth(t *testing.T) { assert.Equal(t, expectedMaxValue, actualMaxValue) } +func TestGetStartOfDay(t *testing.T) { + expectedValue := int64(1617148800) // 2021-03-31 00:00:00 UTC + actualValue := GetStartOfDay(time.Unix(1617228083, 0).In(time.UTC)) + assert.Equal(t, expectedValue, actualValue.Unix()) + + expectedValue = int64(1617206400) // 2021-04-01 00:00:00 UTC+8 + actualValue = GetStartOfDay(time.Unix(1617228083, 0).In(time.FixedZone("Test Timezone", 28800))) + assert.Equal(t, expectedValue, actualValue.Unix()) +} + func TestParseFromUnixTime(t *testing.T) { expectedValue := int64(1617228083) actualTime := parseFromUnixTime(expectedValue)