2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
2021-03-16 03:36:05 -07:00
|
|
|
import "time"
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
type tempus struct {
|
2022-01-01 11:08:08 -08:00
|
|
|
props Properties
|
2022-01-01 11:09:52 -08:00
|
|
|
env Environment
|
2021-03-16 03:36:05 -07:00
|
|
|
templateText string
|
|
|
|
CurrentDate time.Time
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// TimeFormat uses the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format the current time
|
2019-03-13 04:14:30 -07:00
|
|
|
TimeFormat Property = "time_format"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *tempus) enabled() bool {
|
2021-03-16 03:36:05 -07:00
|
|
|
// 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,
|
2021-05-26 12:12:58 -07:00
|
|
|
Env: t.env,
|
2021-03-16 03:36:05 -07:00
|
|
|
}
|
2021-04-11 06:24:03 -07:00
|
|
|
var err error
|
|
|
|
t.templateText, err = template.render()
|
|
|
|
if err != nil {
|
|
|
|
t.templateText = err.Error()
|
|
|
|
}
|
2021-03-16 03:36:05 -07:00
|
|
|
return len(t.templateText) > 0
|
|
|
|
}
|
2019-03-13 04:14:30 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tempus) string() string {
|
2021-03-16 03:36:05 -07:00
|
|
|
return t.getFormattedText()
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2022-01-01 11:09:52 -08:00
|
|
|
func (t *tempus) init(props Properties, env Environment) {
|
2019-03-13 04:14:30 -07:00
|
|
|
t.props = props
|
|
|
|
t.env = env
|
|
|
|
}
|
2021-03-16 03:36:05 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|