Dashboard: Support configuring default timezone via config file (#27404)
Add a default timezone that the administrator can set in the settings. This setting is be used as default for the users timezone preference. Can be used when creating Grafana instances without administrator intervention, in order to give user the correct default timezone. Fixes #25654
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
type DateFormatIntervals struct {
|
||||
@@ -15,6 +22,23 @@ type DateFormatIntervals struct {
|
||||
Year string `json:"year"`
|
||||
}
|
||||
|
||||
const LocalBrowserTimezone = "browser"
|
||||
|
||||
func valueAsTimezone(section *ini.Section, keyName string, defaultValue string) (string, error) {
|
||||
timezone := section.Key(keyName).MustString(defaultValue)
|
||||
|
||||
if timezone == LocalBrowserTimezone {
|
||||
return LocalBrowserTimezone, nil
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation(timezone)
|
||||
if err != nil {
|
||||
return LocalBrowserTimezone, 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")
|
||||
@@ -25,4 +49,10 @@ func (cfg *Cfg) readDateFormats() {
|
||||
cfg.DateFormats.Interval.Month = valueAsString(dateFormats, "interval_month", "YYYY-MM")
|
||||
cfg.DateFormats.Interval.Year = "YYYY"
|
||||
cfg.DateFormats.UseBrowserLocale = dateFormats.Key("date_format_use_browser_locale").MustBool(false)
|
||||
|
||||
timezone, err := valueAsTimezone(dateFormats, "default_timezone", LocalBrowserTimezone)
|
||||
if err != nil {
|
||||
cfg.Logger.Warn("Unknown timezone as default_timezone", "err", err)
|
||||
}
|
||||
cfg.DateFormats.DefaultTimezone = timezone
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gopkg.in/ini.v1"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValueAsTimezone(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
output string
|
||||
hasErr bool
|
||||
}{
|
||||
"browser": {"browser", false},
|
||||
"UTC": {"UTC", false},
|
||||
"utc": {"browser", true},
|
||||
"Amsterdam": {"browser", true},
|
||||
"europe/amsterdam": {"browser", true},
|
||||
"Europe/Amsterdam": {"Europe/Amsterdam", false},
|
||||
}
|
||||
|
||||
sec, err := ini.Empty().NewSection("test")
|
||||
assert.NoError(t, err)
|
||||
key, err := sec.NewKey("test", "")
|
||||
assert.NoError(t, err)
|
||||
|
||||
for input, expected := range tests {
|
||||
key.SetValue(input)
|
||||
|
||||
output, err := valueAsTimezone(sec, "test", "default")
|
||||
|
||||
assert.Equal(t, expected.hasErr, err != nil, "Invalid has err for input: %s err: %v", input, err)
|
||||
assert.Equal(t, expected.output, output, "Invalid output for input: %s", input)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user