github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/services/timezones/timezones.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package timezones
     5  
     6  import (
     7  	"encoding/json"
     8  	"io/ioutil"
     9  	"sync/atomic"
    10  
    11  	"github.com/mattermost/mattermost-server/utils/fileutils"
    12  )
    13  
    14  type Timezones struct {
    15  	supportedZones atomic.Value
    16  }
    17  
    18  func New(timezonesConfigFile string) *Timezones {
    19  	timezones := Timezones{}
    20  
    21  	if len(timezonesConfigFile) == 0 {
    22  		timezonesConfigFile = "timezones.json"
    23  	}
    24  
    25  	var supportedTimezones []string
    26  
    27  	// Attempt to get timezones from config. Failure results in defaults.
    28  	if timezoneFile := fileutils.FindConfigFile(timezonesConfigFile); timezoneFile == "" {
    29  		supportedTimezones = DefaultSupportedTimezones
    30  	} else if raw, err := ioutil.ReadFile(timezoneFile); err != nil {
    31  		supportedTimezones = DefaultSupportedTimezones
    32  	} else if err := json.Unmarshal(raw, &supportedTimezones); err != nil {
    33  		supportedTimezones = DefaultSupportedTimezones
    34  	}
    35  
    36  	timezones.supportedZones.Store(supportedTimezones)
    37  
    38  	return &timezones
    39  }
    40  
    41  func (t *Timezones) GetSupported() []string {
    42  	if supportedZonesValue := t.supportedZones.Load(); supportedZonesValue != nil {
    43  		return supportedZonesValue.([]string)
    44  	}
    45  	return []string{}
    46  }
    47  
    48  func DefaultUserTimezone() map[string]string {
    49  	defaultTimezone := make(map[string]string)
    50  	defaultTimezone["useAutomaticTimezone"] = "true"
    51  	defaultTimezone["automaticTimezone"] = ""
    52  	defaultTimezone["manualTimezone"] = ""
    53  
    54  	return defaultTimezone
    55  }