123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package timefmt
- import (
- "bytes"
- "git.swzry.com/zry/YAGTF/yagtf/tfelem"
- "time"
- )
- type TimeElement interface {
- PrintElement(t time.Time) string
- ExpectedSize() int
- }
- const bufSizeRedunt = 64
- type TimePrinter struct {
- elementList []TimeElement
- expectedSize int
- forceTz bool
- forceUTC bool
- forceLocal bool
- forceTzVal *time.Location
- }
- func (this *TimePrinter) UseArgumentSpecifiedTimezone() {
- this.forceTz = false
- }
- func (this *TimePrinter) UseTimezone(tz *time.Location) {
- this.forceTz = true
- this.forceLocal = false
- this.forceUTC = false
- this.forceTzVal = tz
- }
- func (this *TimePrinter) UseLocalTimezone() {
- this.forceTz = true
- this.forceLocal = true
- this.forceUTC = false
- }
- func (this *TimePrinter) UseUTC() {
- this.forceTz = true
- this.forceLocal = false
- this.forceUTC = true
- }
- func (this *TimePrinter) AddPureText(text string) {
- e := &tfelem.PureTextElement{
- PureText: text,
- }
- this.AddElement(e)
- }
- func (this *TimePrinter) AddElement(e TimeElement) {
- this.elementList = append(this.elementList, e)
- this.expectedSize += e.ExpectedSize()
- }
- func (this *TimePrinter) PrintTime(t time.Time) string {
- bsl := make([]byte, 0, this.expectedSize+bufSizeRedunt)
- buf := bytes.NewBuffer(bsl)
- ttz := t
- if this.forceTz {
- if this.forceLocal {
- ttz = t.Local()
- } else if this.forceUTC {
- ttz = t.UTC()
- } else {
- ttz = t.In(this.forceTzVal)
- }
- }
- for _, v := range this.elementList {
- buf.WriteString(v.PrintElement(ttz))
- }
- return buf.String()
- }
- func NewTimePrinter() *TimePrinter {
- tp := &TimePrinter{}
- tp.elementList = make([]TimeElement, 0)
- tp.expectedSize = 0
- return tp
- }
|