timefmtex.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package timefmt
  2. import (
  3. "strings"
  4. "time"
  5. )
  6. type TimeFormatterEx struct {
  7. Parser *FormatParser
  8. Printer *TimePrinterEx
  9. Multiline bool
  10. }
  11. func (this *TimeFormatterEx) Format(t time.Time, exdata map[string]string) string {
  12. fs := this.Printer.PrintTimeEx(t, exdata)
  13. if this.Multiline {
  14. return fs
  15. } else {
  16. fs = strings.Replace(fs, "\r", "", -1)
  17. fs = strings.Replace(fs, "\n", "", -1)
  18. return fs
  19. }
  20. }
  21. func (this *TimeFormatterEx) ForceTimezone(tz *time.Location) {
  22. this.Printer.UseTimezone(tz)
  23. }
  24. func (this *TimeFormatterEx) ForceLocalTimezone() {
  25. this.Printer.UseLocalTimezone()
  26. }
  27. func (this *TimeFormatterEx) ForceUTC() {
  28. this.Printer.UseUTC()
  29. }
  30. func (this *TimeFormatterEx) ParseFormat(fmtstr string) {
  31. this.Parser.ParseFormatString(&FormatPrinterParsingAbstract{
  32. isEx: true,
  33. tp: nil,
  34. tpe: this.Printer,
  35. }, fmtstr)
  36. }
  37. func NewDefaultTimeFormatterEx(fmtstr string, multiline bool) *TimeFormatterEx {
  38. tf := &TimeFormatterEx{
  39. Printer: NewTimePrinterEx(),
  40. Multiline: multiline,
  41. Parser: NewFormatParser(true),
  42. }
  43. tf.ParseFormat(fmtstr)
  44. return tf
  45. }
  46. func NewTimeFormatterEx(printer *TimePrinterEx, parser *FormatParser, multiline bool) *TimeFormatterEx {
  47. tf := &TimeFormatterEx{
  48. Printer: printer,
  49. Multiline: multiline,
  50. Parser: parser,
  51. }
  52. return tf
  53. }