fix: time zone issues in case database and server timezones vary

This commit is contained in:
Ferdinand Mütsch
2025-02-21 10:39:53 +01:00
parent c48bdfca7e
commit 2b3f1d9ef4
8 changed files with 1255 additions and 1236 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -3,6 +3,7 @@ package helpers
import (
"fmt"
"github.com/muety/wakapi/config"
"strings"
"time"
)
@@ -36,6 +37,14 @@ func FormatDateTimeHuman(date time.Time) string {
return date.Format(config.Get().App.DateTimeFormat)
}
func FormatDateTimeHumanTZ(date time.Time) string {
format := config.Get().App.DateTimeFormat
if !strings.HasSuffix(format, "MST") {
format += " MST"
}
return date.Format(format)
}
func FormatDateHuman(date time.Time) string {
return date.Format(config.Get().App.DateFormat)
}

View File

@@ -14,8 +14,8 @@ type LeaderboardItem struct {
Interval string `json:"interval" gorm:"not null; size:32; index:idx_leaderboard_combined"`
By *uint8 `json:"aggregated_by" gorm:"index:idx_leaderboard_combined"` // pointer because nullable
Total time.Duration `json:"total" gorm:"not null" swaggertype:"primitive,integer"`
Key *string `json:"key" gorm:"size:255"` // pointer because nullable
CreatedAt CustomTime `gorm:"default:CURRENT_TIMESTAMP" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
Key *string `json:"key" gorm:"size:255"` // pointer because nullable
CreatedAt CustomTime `swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"` // filled by gorm, see https://gorm.io/docs/conventions.html#CreatedAt
}
// https://github.com/go-gorm/gorm/issues/5789

View File

@@ -33,8 +33,8 @@ type Summary struct {
ID uint `json:"-" gorm:"primary_key; size:32"`
User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"`
FromTime CustomTime `json:"from" gorm:"not null; default:CURRENT_TIMESTAMP; index:idx_time_summary_user" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
ToTime CustomTime `json:"to" gorm:"not null; default:CURRENT_TIMESTAMP; index:idx_time_summary_user" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
FromTime CustomTime `json:"from" gorm:"not null; index:idx_time_summary_user" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"` // filled by gorm if not set, see https://gorm.io/docs/conventions.html#CreatedAt
ToTime CustomTime `json:"to" gorm:"not null; index:idx_time_summary_user" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"` // filled by gorm if not set, see https://gorm.io/docs/conventions.html#CreatedAt
// Previously, all the following properties created a cascade foreign key constraint on the summary_items table
// back to this summary table resulting in 5 identical foreign key constraints on the summary_items table.

View File

@@ -27,8 +27,8 @@ type User struct {
Email string `json:"email" gorm:"index:idx_user_email; size:255"`
Location string `json:"location"`
Password string `json:"-"`
CreatedAt CustomTime `gorm:"default:CURRENT_TIMESTAMP" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
LastLoggedInAt CustomTime `gorm:"default:CURRENT_TIMESTAMP" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
CreatedAt CustomTime `swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"` // filled by gorm, see https://gorm.io/docs/conventions.html#CreatedAt
LastLoggedInAt CustomTime `swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"` // filled by gorm, see https://gorm.io/docs/conventions.html#CreatedAt
ShareDataMaxDays int `json:"-"`
ShareEditors bool `json:"-" gorm:"default:false; type:bool"`
ShareLanguages bool `json:"-" gorm:"default:false; type:bool"`

View File

@@ -48,5 +48,9 @@ func (s *LeaderboardViewModel) LangIcon(lang string) string {
}
func (s *LeaderboardViewModel) LastUpdate() time.Time {
return models.Leaderboard(s.Items).LastUpdate()
tz := time.Local
if s.User != nil {
tz = s.User.TZ()
}
return models.Leaderboard(s.Items).LastUpdate().In(tz)
}

View File

@@ -24,6 +24,7 @@ func DefaultTemplateFuncs() template.FuncMap {
"json": utils.Json,
"date": helpers.FormatDateHuman,
"datetime": helpers.FormatDateTimeHuman,
"datetimetz": helpers.FormatDateTimeHumanTZ,
"simpledate": helpers.FormatDate,
"simpledatetime": helpers.FormatDateTime,
"duration": helpers.FmtWakatimeDuration,

View File

@@ -87,7 +87,7 @@
</li>
{{ end }}
</ol>
<p class="text-sm pt-8">Last Updated: {{ .LastUpdate | datetime }}</p>
<p class="text-sm pt-8">Last Updated: {{ .LastUpdate | datetimetz }}</p>
{{ else }}
<p>
<span class="iconify inline text-white text-base" data-icon="twemoji:frowning-face"></span>&nbsp;