mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Display ui time with customize time location (#7792)
* display ui time with customize time location * fix lint * rename UILocation to DefaultUILocation * move time related functions to modules/timeutil * fix tests * fix tests * fix build * fix swagger
This commit is contained in:
		@@ -278,6 +278,8 @@ var (
 | 
			
		||||
 | 
			
		||||
	// Time settings
 | 
			
		||||
	TimeFormat string
 | 
			
		||||
	// UILocation is the location on the UI, so that we can display the time on UI.
 | 
			
		||||
	DefaultUILocation = time.Local
 | 
			
		||||
 | 
			
		||||
	CSRFCookieName     = "_csrf"
 | 
			
		||||
	CSRFCookieHTTPOnly = true
 | 
			
		||||
@@ -356,10 +358,6 @@ var (
 | 
			
		||||
	HasRobotsTxt      bool
 | 
			
		||||
	InternalToken     string // internal access token
 | 
			
		||||
	IterateBufferSize int
 | 
			
		||||
 | 
			
		||||
	// UILocation is the location on the UI, so that we can display the time on UI.
 | 
			
		||||
	// Currently only show the default time.Local, it could be added to app.ini after UI is ready
 | 
			
		||||
	UILocation = time.Local
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// DateLang transforms standard language locale name to corresponding value in datetime plugin.
 | 
			
		||||
@@ -792,32 +790,47 @@ func NewContext() {
 | 
			
		||||
	AttachmentMaxFiles = sec.Key("MAX_FILES").MustInt(5)
 | 
			
		||||
	AttachmentEnabled = sec.Key("ENABLED").MustBool(true)
 | 
			
		||||
 | 
			
		||||
	TimeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("RFC1123")
 | 
			
		||||
	TimeFormat = map[string]string{
 | 
			
		||||
		"ANSIC":       time.ANSIC,
 | 
			
		||||
		"UnixDate":    time.UnixDate,
 | 
			
		||||
		"RubyDate":    time.RubyDate,
 | 
			
		||||
		"RFC822":      time.RFC822,
 | 
			
		||||
		"RFC822Z":     time.RFC822Z,
 | 
			
		||||
		"RFC850":      time.RFC850,
 | 
			
		||||
		"RFC1123":     time.RFC1123,
 | 
			
		||||
		"RFC1123Z":    time.RFC1123Z,
 | 
			
		||||
		"RFC3339":     time.RFC3339,
 | 
			
		||||
		"RFC3339Nano": time.RFC3339Nano,
 | 
			
		||||
		"Kitchen":     time.Kitchen,
 | 
			
		||||
		"Stamp":       time.Stamp,
 | 
			
		||||
		"StampMilli":  time.StampMilli,
 | 
			
		||||
		"StampMicro":  time.StampMicro,
 | 
			
		||||
		"StampNano":   time.StampNano,
 | 
			
		||||
	}[TimeFormatKey]
 | 
			
		||||
	// When the TimeFormatKey does not exist in the previous map e.g.'2006-01-02 15:04:05'
 | 
			
		||||
	if len(TimeFormat) == 0 {
 | 
			
		||||
		TimeFormat = TimeFormatKey
 | 
			
		||||
		TestTimeFormat, _ := time.Parse(TimeFormat, TimeFormat)
 | 
			
		||||
		if TestTimeFormat.Format(time.RFC3339) != "2006-01-02T15:04:05Z" {
 | 
			
		||||
			log.Fatal("Can't create time properly, please check your time format has 2006, 01, 02, 15, 04 and 05")
 | 
			
		||||
	timeFormatKey := Cfg.Section("time").Key("FORMAT").MustString("")
 | 
			
		||||
	if timeFormatKey != "" {
 | 
			
		||||
		TimeFormat = map[string]string{
 | 
			
		||||
			"ANSIC":       time.ANSIC,
 | 
			
		||||
			"UnixDate":    time.UnixDate,
 | 
			
		||||
			"RubyDate":    time.RubyDate,
 | 
			
		||||
			"RFC822":      time.RFC822,
 | 
			
		||||
			"RFC822Z":     time.RFC822Z,
 | 
			
		||||
			"RFC850":      time.RFC850,
 | 
			
		||||
			"RFC1123":     time.RFC1123,
 | 
			
		||||
			"RFC1123Z":    time.RFC1123Z,
 | 
			
		||||
			"RFC3339":     time.RFC3339,
 | 
			
		||||
			"RFC3339Nano": time.RFC3339Nano,
 | 
			
		||||
			"Kitchen":     time.Kitchen,
 | 
			
		||||
			"Stamp":       time.Stamp,
 | 
			
		||||
			"StampMilli":  time.StampMilli,
 | 
			
		||||
			"StampMicro":  time.StampMicro,
 | 
			
		||||
			"StampNano":   time.StampNano,
 | 
			
		||||
		}[timeFormatKey]
 | 
			
		||||
		// When the TimeFormatKey does not exist in the previous map e.g.'2006-01-02 15:04:05'
 | 
			
		||||
		if len(TimeFormat) == 0 {
 | 
			
		||||
			TimeFormat = timeFormatKey
 | 
			
		||||
			TestTimeFormat, _ := time.Parse(TimeFormat, TimeFormat)
 | 
			
		||||
			if TestTimeFormat.Format(time.RFC3339) != "2006-01-02T15:04:05Z" {
 | 
			
		||||
				log.Fatal("Can't create time properly, please check your time format has 2006, 01, 02, 15, 04 and 05")
 | 
			
		||||
			}
 | 
			
		||||
			log.Trace("Custom TimeFormat: %s", TimeFormat)
 | 
			
		||||
		}
 | 
			
		||||
		log.Trace("Custom TimeFormat: %s", TimeFormat)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	zone := Cfg.Section("time").Key("DEFAULT_UI_LOCATION").String()
 | 
			
		||||
	if zone != "" {
 | 
			
		||||
		DefaultUILocation, err = time.LoadLocation(zone)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal("Load time zone failed: %v", err)
 | 
			
		||||
		} else {
 | 
			
		||||
			log.Info("Default UI Location is %v", zone)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if DefaultUILocation == nil {
 | 
			
		||||
		DefaultUILocation = time.Local
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user