-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
segment_time.go
54 lines (47 loc) · 1.19 KB
/
segment_time.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
package main
import "time"
type tempus struct {
props *properties
env environmentInfo
templateText string
CurrentDate time.Time
}
const (
// TimeFormat uses the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format the current time
TimeFormat Property = "time_format"
)
func (t *tempus) enabled() bool {
// if no date set, use now(unit testing)
if t.CurrentDate.IsZero() {
t.CurrentDate = time.Now()
}
segmentTemplate := t.props.getString(SegmentTemplate, "")
if segmentTemplate != "" {
template := &textTemplate{
Template: segmentTemplate,
Context: t,
}
var err error
t.templateText, err = template.render()
if err != nil {
t.templateText = err.Error()
}
return len(t.templateText) > 0
}
return true
}
func (t *tempus) string() string {
return t.getFormattedText()
}
func (t *tempus) init(props *properties, env environmentInfo) {
t.props = props
t.env = env
}
func (t *tempus) getFormattedText() string {
if len(t.templateText) > 0 {
return t.templateText
}
// keep old behaviour if no template
timeFormatProperty := t.props.getString(TimeFormat, "15:04:05")
return t.CurrentDate.Format(timeFormatProperty)
}