Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: add support for multiple timezones and set local timezone in site info

hgaol840f6bfd2b3c
diff --git a/internal/base/constant/constant.go b/internal/base/constant/constant.go
index f467f157..ae9e6430 100644
--- a/internal/base/constant/constant.go
+++ b/internal/base/constant/constant.go
@@ -29,3 +29,43 @@ var (
Revision = ""
GoVersion = ""
)
+
+var Timezones = []string{
+ // Americas
+ "America/New_York",
+ "America/Chicago",
+ "America/Los_Angeles",
+ "America/Toronto",
+ "America/Vancouver",
+ "America/Mexico_City",
+ "America/Sao_Paulo",
+ "America/Buenos_Aires",
+
+ // Europe
+ "Europe/London",
+ "Europe/Paris",
+ "Europe/Berlin",
+ "Europe/Madrid",
+ "Europe/Rome",
+ "Europe/Moscow",
+
+ // Asia
+ "Asia/Shanghai",
+ "Asia/Tokyo",
+ "Asia/Singapore",
+ "Asia/Dubai",
+ "Asia/Hong_Kong",
+ "Asia/Seoul",
+ "Asia/Bangkok",
+ "Asia/Kolkata",
+
+ // Pacific
+ "Australia/Sydney",
+ "Australia/Melbourne",
+ "Pacific/Auckland",
+
+ // Africa
+ "Africa/Cairo",
+ "Africa/Johannesburg",
+ "Africa/Lagos",
+}
diff --git a/internal/migrations/init.go b/internal/migrations/init.go
index c07907e7..b2c814e1 100644
--- a/internal/migrations/init.go
+++ b/internal/migrations/init.go
@@ -160,9 +160,28 @@ func (m *Mentor) initAdminUserRoleRel() {
}
func (m *Mentor) initSiteInfoInterface() {
+ now := time.Now()
+ zoneName, offset := now.In(time.Local).Zone()
+
+ localTimezone := "UTC"
+ for _, tz := range constant.Timezones {
+ loc, err := time.LoadLocation(tz)
+ if err != nil {
+ continue
+ }
+
+ tzNow := now.In(loc)
+ tzName, tzOffset := tzNow.Zone()
+
+ if tzName == zoneName && tzOffset == offset {
+ localTimezone = tz
+ break
+ }
+ }
+
interfaceData := map[string]string{
"language": m.userData.Language,
- "time_zone": "UTC",
+ "time_zone": localTimezone,
}
interfaceDataBytes, _ := json.Marshal(interfaceData)
_, m.err = m.engine.Context(m.ctx).Insert(&entity.SiteInfo{
@@ -452,5 +471,4 @@ func (m *Mentor) initDefaultBadges() {
return
}
}
- return
}