code.gitea.io/gitea@v1.19.3/modules/setting/time.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"time"
     8  
     9  	"code.gitea.io/gitea/modules/log"
    10  )
    11  
    12  var (
    13  	// Time settings
    14  	TimeFormat string
    15  	// UILocation is the location on the UI, so that we can display the time on UI.
    16  	DefaultUILocation = time.Local
    17  )
    18  
    19  func loadTimeFrom(rootCfg ConfigProvider) {
    20  	timeFormatKey := rootCfg.Section("time").Key("FORMAT").MustString("")
    21  	if timeFormatKey != "" {
    22  		TimeFormat = map[string]string{
    23  			"ANSIC":       time.ANSIC,
    24  			"UnixDate":    time.UnixDate,
    25  			"RubyDate":    time.RubyDate,
    26  			"RFC822":      time.RFC822,
    27  			"RFC822Z":     time.RFC822Z,
    28  			"RFC850":      time.RFC850,
    29  			"RFC1123":     time.RFC1123,
    30  			"RFC1123Z":    time.RFC1123Z,
    31  			"RFC3339":     time.RFC3339,
    32  			"RFC3339Nano": time.RFC3339Nano,
    33  			"Kitchen":     time.Kitchen,
    34  			"Stamp":       time.Stamp,
    35  			"StampMilli":  time.StampMilli,
    36  			"StampMicro":  time.StampMicro,
    37  			"StampNano":   time.StampNano,
    38  		}[timeFormatKey]
    39  		// When the TimeFormatKey does not exist in the previous map e.g.'2006-01-02 15:04:05'
    40  		if len(TimeFormat) == 0 {
    41  			TimeFormat = timeFormatKey
    42  			TestTimeFormat, _ := time.Parse(TimeFormat, TimeFormat)
    43  			if TestTimeFormat.Format(time.RFC3339) != "2006-01-02T15:04:05Z" {
    44  				log.Warn("Provided TimeFormat: %s does not create a fully specified date and time.", TimeFormat)
    45  				log.Warn("In order to display dates and times correctly please check your time format has 2006, 01, 02, 15, 04 and 05")
    46  			}
    47  			log.Trace("Custom TimeFormat: %s", TimeFormat)
    48  		}
    49  	}
    50  
    51  	zone := rootCfg.Section("time").Key("DEFAULT_UI_LOCATION").String()
    52  	if zone != "" {
    53  		var err error
    54  		DefaultUILocation, err = time.LoadLocation(zone)
    55  		if err != nil {
    56  			log.Fatal("Load time zone failed: %v", err)
    57  		} else {
    58  			log.Info("Default UI Location is %v", zone)
    59  		}
    60  	}
    61  	if DefaultUILocation == nil {
    62  		DefaultUILocation = time.Local
    63  	}
    64  }