summaryrefslogtreecommitdiff
path: root/internal/config/timezone.go
blob: 04f218e1cfd0f290be9ab51132a16bbe93c6112e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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())
}