From 1234d4802b1181c4e1ec7229a72a21e4f44ab30e Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 25 Mar 2021 22:42:56 +0800 Subject: [PATCH] transaction month list supports browser time zone --- pkg/api/transactions.go | 2 +- pkg/services/transactions.go | 8 ++++---- pkg/utils/datetimes.go | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/api/transactions.go b/pkg/api/transactions.go index e1b5a979..ea52a0f2 100644 --- a/pkg/api/transactions.go +++ b/pkg/api/transactions.go @@ -243,7 +243,7 @@ func (a *TransactionsApi) TransactionMonthListHandler(c *core.Context) (interfac } } - transactions, err := a.transactions.GetTransactionsInMonthByPage(uid, transactionListReq.Year, transactionListReq.Month, transactionListReq.Type, allCategoryIds, transactionListReq.AccountId, transactionListReq.Keyword, transactionListReq.Page, transactionListReq.Count) + transactions, err := a.transactions.GetTransactionsInMonthByPage(uid, transactionListReq.Year, transactionListReq.Month, transactionListReq.Type, allCategoryIds, transactionListReq.AccountId, transactionListReq.Keyword, transactionListReq.Page, transactionListReq.Count, utcOffset) if err != nil { log.ErrorfWithRequestId(c, "[transactions.TransactionMonthListHandler] failed to get transactions in month \"%d-%d\" for user \"uid:%d\", because %s", transactionListReq.Year, transactionListReq.Month, uid, err.Error()) diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 406bf6de..285d0408 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -143,7 +143,7 @@ func (s *TransactionService) GetTransactionsByMaxTime(uid int64, maxTime int64, } // GetTransactionsInMonthByPage returns transactions in given year and month -func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, transactionType models.TransactionDbType, categoryIds []int64, accountId int64, keyword string, page int, count int) ([]*models.Transaction, error) { +func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, month int, transactionType models.TransactionDbType, categoryIds []int64, accountId int64, keyword string, page int, count int, utcOffset int16) ([]*models.Transaction, error) { if uid <= 0 { return nil, errs.ErrUserIdInvalid } @@ -156,7 +156,7 @@ func (s *TransactionService) GetTransactionsInMonthByPage(uid int64, year int, m return nil, errs.ErrPageCountInvalid } - startTime, err := utils.ParseFromLongDateTime(fmt.Sprintf("%d-%d-01 00:00:00", year, month)) + startTime, err := utils.ParseFromLongDateTime(fmt.Sprintf("%d-%d-01 00:00:00", year, month), utcOffset) if err != nil { return nil, errs.ErrSystemError @@ -260,12 +260,12 @@ func (s *TransactionService) GetAllTransactionCount(uid int64) (int64, error) { } // GetMonthTransactionCount returns total count of transactions in given year and month -func (s *TransactionService) GetMonthTransactionCount(uid int64, year int64, month int64) (int64, error) { +func (s *TransactionService) GetMonthTransactionCount(uid int64, year int64, month int64, utcOffset int16) (int64, error) { if uid <= 0 { return 0, errs.ErrUserIdInvalid } - startTime, err := utils.ParseFromLongDateTime(fmt.Sprintf("%d-%d-01 00:00:00", year, month)) + startTime, err := utils.ParseFromLongDateTime(fmt.Sprintf("%d-%d-01 00:00:00", year, month), utcOffset) if err != nil { return 0, errs.ErrSystemError diff --git a/pkg/utils/datetimes.go b/pkg/utils/datetimes.go index 855274f8..cd60e96f 100644 --- a/pkg/utils/datetimes.go +++ b/pkg/utils/datetimes.go @@ -23,8 +23,9 @@ func ParseFromUnixTime(unixTime int64) time.Time { } // ParseFromLongDateTime parses a formatted string in long date time format -func ParseFromLongDateTime(t string) (time.Time, error) { - return time.Parse(longDateTimeFormat, t) +func ParseFromLongDateTime(t string, utcOffset int16) (time.Time, error) { + timezone := time.FixedZone("Timezone", int(utcOffset)*60) + return time.ParseInLocation(longDateTimeFormat, t, timezone) } // GetMinTransactionTimeFromUnixTime returns the minimum transaction time from unix time