package timefmt import ( "strings" "time" ) type TimeFormatter struct { Parser *FormatParser Printer *TimePrinter Multiline bool } func (this *TimeFormatter) Format(t time.Time) string { fs := this.Printer.PrintTime(t) if this.Multiline { return fs } else { fs = strings.Replace(fs, "\r", "", -1) fs = strings.Replace(fs, "\n", "", -1) return fs } } func (this *TimeFormatter) ForceTimezone(tz *time.Location) { this.Printer.UseTimezone(tz) } func (this *TimeFormatter) ForceLocalTimezone() { this.Printer.UseLocalTimezone() } func (this *TimeFormatter) ForceUTC() { this.Printer.UseUTC() } func (this *TimeFormatter) ParseFormat(fmtstr string) { this.Parser.ParseFormatString(&FormatPrinterParsingAbstract{ isEx: false, tp: this.Printer, tpe: nil, }, fmtstr) } func NewDefaultTimeFormatter(fmtstr string, multiline bool) *TimeFormatter { tf := &TimeFormatter{ Printer: NewTimePrinter(), Multiline: multiline, Parser: NewFormatParser(false), } tf.ParseFormat(fmtstr) return tf } func NewTimeFormatter(printer *TimePrinter, parser *FormatParser, multiline bool) *TimeFormatter { tf := &TimeFormatter{ Printer: printer, Multiline: multiline, Parser: parser, } return tf }