forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate_formats.go
61 lines (51 loc) · 2.15 KB
/
date_formats.go
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
57
58
59
60
61
package setting
import (
"time"
"gopkg.in/ini.v1"
)
type DateFormats struct {
FullDate string `json:"fullDate"`
UseBrowserLocale bool `json:"useBrowserLocale"`
Interval DateFormatIntervals `json:"interval"`
DefaultTimezone string `json:"defaultTimezone"`
DefaultWeekStart string `json:"defaultWeekStart"`
}
type DateFormatIntervals struct {
Millisecond string `json:"millisecond"`
Second string `json:"second"`
Minute string `json:"minute"`
Hour string `json:"hour"`
Day string `json:"day"`
Month string `json:"month"`
Year string `json:"year"`
}
const localBrowser = "browser"
func valueAsTimezone(section *ini.Section, keyName string) (string, error) {
timezone := section.Key(keyName).MustString(localBrowser)
if timezone == localBrowser {
return localBrowser, nil
}
location, err := time.LoadLocation(timezone)
if err != nil {
return localBrowser, err
}
return location.String(), nil
}
func (cfg *Cfg) readDateFormats() {
dateFormats := cfg.Raw.Section("date_formats")
cfg.DateFormats.FullDate = valueAsString(dateFormats, "full_date", "YYYY-MM-DD HH:mm:ss")
cfg.DateFormats.Interval.Millisecond = valueAsString(dateFormats, "interval_millisecond", "HH:mm:ss.SSS")
cfg.DateFormats.Interval.Second = valueAsString(dateFormats, "interval_second", "HH:mm:ss")
cfg.DateFormats.Interval.Minute = valueAsString(dateFormats, "interval_minute", "HH:mm")
cfg.DateFormats.Interval.Hour = valueAsString(dateFormats, "interval_hour", "MM-DD HH:mm")
cfg.DateFormats.Interval.Day = valueAsString(dateFormats, "interval_day", "YYYY-MM-DD")
cfg.DateFormats.Interval.Month = valueAsString(dateFormats, "interval_month", "YYYY-MM")
cfg.DateFormats.Interval.Year = "YYYY"
cfg.DateFormats.UseBrowserLocale = dateFormats.Key("use_browser_locale").MustBool(false)
timezone, err := valueAsTimezone(dateFormats, "default_timezone")
if err != nil {
cfg.Logger.Warn("Unknown timezone as default_timezone", "err", err)
}
cfg.DateFormats.DefaultTimezone = timezone
cfg.DateFormats.DefaultWeekStart = valueAsString(dateFormats, "default_week_start", "browser")
}