diff options
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/timezone.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/internal/config/timezone.go b/internal/config/timezone.go new file mode 100644 index 0000000..04f218e --- /dev/null +++ b/internal/config/timezone.go @@ -0,0 +1,56 @@ +package config + +import ( + "sync" + "time" +) + +var ( + displayTimezone *time.Location + tzOnce sync.Once +) + +// SetDisplayTimezone sets the global display timezone (call once at startup) +func SetDisplayTimezone(tz string) { + tzOnce.Do(func() { + loc, err := time.LoadLocation(tz) + if err != nil { + loc = time.UTC + } + displayTimezone = loc + }) +} + +// GetDisplayTimezone returns the configured display timezone +func GetDisplayTimezone() *time.Location { + if displayTimezone == nil { + return time.UTC + } + return displayTimezone +} + +// Now returns the current time in the configured display timezone +func Now() time.Time { + return time.Now().In(GetDisplayTimezone()) +} + +// Today returns today's date at midnight in the configured timezone +func Today() time.Time { + now := Now() + return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, GetDisplayTimezone()) +} + +// ParseDateInDisplayTZ parses a date string (2006-01-02) in the display timezone +func ParseDateInDisplayTZ(dateStr string) (time.Time, error) { + return time.ParseInLocation("2006-01-02", dateStr, GetDisplayTimezone()) +} + +// ParseDateTimeInDisplayTZ parses a datetime string in the display timezone +func ParseDateTimeInDisplayTZ(layout, value string) (time.Time, error) { + return time.ParseInLocation(layout, value, GetDisplayTimezone()) +} + +// ToDisplayTZ converts a time to the display timezone +func ToDisplayTZ(t time.Time) time.Time { + return t.In(GetDisplayTimezone()) +} |
