This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new 840f6bfd feat: add support for multiple timezones and set local 
timezone in site info
840f6bfd is described below

commit 840f6bfd2b3c2bece9027e22df6f70f5a1124653
Author: hgaol <[email protected]>
AuthorDate: Wed Apr 2 21:36:39 2025 +0800

    feat: add support for multiple timezones and set local timezone in site info
---
 internal/base/constant/constant.go | 40 ++++++++++++++++++++++++++++++++++++++
 internal/migrations/init.go        | 22 +++++++++++++++++++--
 2 files changed, 60 insertions(+), 2 deletions(-)

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
 }

Reply via email to