Files
olons_server/internal/store/time.go
T
richjohnrambo f56b84bb10
Deploy olons-server production / deploy (push) Successful in 34s
fix: display finance records and ledgers in Beijing time
2026-10-23 00:13:07 +08:00

29 lines
1.5 KiB
Go

package store
// databaseWallTimeUTC normalizes DATETIME values written by CURRENT_TIMESTAMP.
// The database session can use UTC or Beijing time independently of the Go process.
// Do not use it for fields explicitly written as UTC by application code.
func databaseWallTimeUTC(column string) string {
return `DATE_FORMAT(TIMESTAMPADD(SECOND,TIMESTAMPDIFF(SECOND,NOW(),UTC_TIMESTAMP()),` + column + `),'%Y-%m-%dT%H:%i:%sZ')`
}
// beijingWallTimeUTC handles DATETIME fields explicitly written by the admin
// service, whose business clock is Asia/Shanghai, independently of the DB session.
func beijingWallTimeUTC(column string) string {
return `DATE_FORMAT(TIMESTAMPADD(HOUR,-8,` + column + `),'%Y-%m-%dT%H:%i:%sZ')`
}
// walletLedgerTimeUTC preserves the UTC clock used by insertTaskLedger and
// insertWithdrawalLedger; all other ledger writers use the admin's Beijing clock.
// Legacy forced-payment entries share the task:...:match key with client entries,
// so their writer-provided remarks must also distinguish those two sources.
func walletLedgerTimeUTC() string {
return `CASE WHEN idempotency_key LIKE 'withdrawal:%'
OR idempotency_key REGEXP '^task:[0-9]+:(settle|expire):'
OR (idempotency_key REGEXP '^task:[0-9]+:match:(available|frozen)$' AND remark IN (
'自动派单扣除可用余额','自动派单冻结订单本金',
'会员确认手动订单扣除可用余额','会员确认手动订单冻结本金'))
THEN DATE_FORMAT(occurred_at,'%Y-%m-%dT%H:%i:%sZ')
ELSE ` + beijingWallTimeUTC("occurred_at") + ` END`
}