yagtf.go 871 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package timefmt
  2. import (
  3. "strings"
  4. "time"
  5. )
  6. type TimeFormatter struct {
  7. printer *TimePrinter
  8. multiline bool
  9. }
  10. func (this *TimeFormatter) Format(t time.Time) string {
  11. fs := this.printer.PrintTime(t)
  12. if this.multiline {
  13. return fs
  14. } else {
  15. fs = strings.Replace(fs, "\r", "", -1)
  16. fs = strings.Replace(fs, "\n", "", -1)
  17. return fs
  18. }
  19. return this.printer.PrintTime(t)
  20. }
  21. func (this *TimeFormatter) ForceTimezone(tz *time.Location) {
  22. this.printer.UseTimezone(tz)
  23. }
  24. func (this *TimeFormatter) ForceLocalTimezone() {
  25. this.printer.UseLocalTimezone()
  26. }
  27. func (this *TimeFormatter) ForceUTC() {
  28. this.printer.UseUTC()
  29. }
  30. func NewTimeFormatter(fmtstr string, multiline bool) *TimeFormatter {
  31. tf := &TimeFormatter{
  32. printer: NewTimePrinter(),
  33. multiline: multiline,
  34. }
  35. parser := NewFormatParser()
  36. parser.ParseFormatString(tf.printer, fmtstr)
  37. return tf
  38. }