1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package timefmt
- import (
- "bytes"
- "git.swzry.com/zry/YAGTF/yagtf/tfelem"
- "time"
- )
- type TimeElementEx interface {
- PrintElementEx(t time.Time, exdata map[string]string) string
- ExpectedSize() int
- }
- type TimePrinterEx struct {
- elementList []TimeElementEx
- expectedSize int
- forceTz bool
- forceUTC bool
- forceLocal bool
- forceTzVal *time.Location
- }
- func (this *TimePrinterEx) UseArgumentSpecifiedTimezone() {
- this.forceTz = false
- }
- func (this *TimePrinterEx) UseTimezone(tz *time.Location) {
- this.forceTz = true
- this.forceLocal = false
- this.forceUTC = false
- this.forceTzVal = tz
- }
- func (this *TimePrinterEx) UseLocalTimezone() {
- this.forceTz = true
- this.forceLocal = true
- this.forceUTC = false
- }
- func (this *TimePrinterEx) UseUTC() {
- this.forceTz = true
- this.forceLocal = false
- this.forceUTC = true
- }
- func (this *TimePrinterEx) AddPureText(text string) {
- e := &tfelem.PureTextElement{
- PureText: text,
- }
- this.AddTimeElement(e)
- }
- func (this *TimePrinterEx) AddTimeElement(e TimeElement) {
- this.AddTimeElementEx(NewBasicTimeElementWrap(e))
- }
- func (this *TimePrinterEx) AddTimeElementEx(e TimeElementEx) {
- this.elementList = append(this.elementList, e)
- this.expectedSize += e.ExpectedSize()
- }
- func (this *TimePrinterEx) PrintTimeEx(t time.Time, exdata map[string]string) 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.PrintElementEx(ttz, exdata))
- }
- return buf.String()
- }
- func NewTimePrinterEx() *TimePrinterEx {
- tp := &TimePrinterEx{}
- tp.elementList = make([]TimeElementEx, 0)
- tp.expectedSize = 0
- return tp
- }
|