|
@@ -12,18 +12,85 @@ const parserTokenBufferSize = 128
|
|
const regexpTimezoneTag = "^Timezone:([+-][0-9]{4})([A-Z]{3})$"
|
|
const regexpTimezoneTag = "^Timezone:([+-][0-9]{4})([A-Z]{3})$"
|
|
|
|
|
|
type TimeElementFactoryFunc func() TimeElement
|
|
type TimeElementFactoryFunc func() TimeElement
|
|
-type NonDisplayTagFunc func(tp *TimePrinter)
|
|
|
|
|
|
+type TimeElementExFactoryFunc func() TimeElementEx
|
|
|
|
+type NonDisplayTagFunc func(tp *FormatPrinterParsingAbstract)
|
|
|
|
+
|
|
|
|
+type FormatPrinterParsingAbstract struct {
|
|
|
|
+ isEx bool
|
|
|
|
+ tp *TimePrinter
|
|
|
|
+ tpe *TimePrinterEx
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) IsEx() bool {
|
|
|
|
+ return this.isEx
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) AddPureText(txt string) {
|
|
|
|
+ if this.isEx {
|
|
|
|
+ this.tpe.AddPureText(txt)
|
|
|
|
+ } else {
|
|
|
|
+ this.tp.AddPureText(txt)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) AddExDataTag(e TimeElementEx) {
|
|
|
|
+ this.tpe.AddTimeElementEx(e)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) AddElement(e TimeElement) {
|
|
|
|
+ if this.isEx {
|
|
|
|
+ this.tpe.AddTimeElement(e)
|
|
|
|
+ } else {
|
|
|
|
+ this.tp.AddElement(e)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) UseTimezone(tz *time.Location) {
|
|
|
|
+ if this.isEx {
|
|
|
|
+ this.tpe.UseTimezone(tz)
|
|
|
|
+ } else {
|
|
|
|
+ this.tp.UseTimezone(tz)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) UseUTC() {
|
|
|
|
+ if this.isEx {
|
|
|
|
+ this.tpe.UseUTC()
|
|
|
|
+ } else {
|
|
|
|
+ this.tp.UseUTC()
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) UseLocalTimezone() {
|
|
|
|
+ if this.isEx {
|
|
|
|
+ this.tpe.UseLocalTimezone()
|
|
|
|
+ } else {
|
|
|
|
+ this.tp.UseLocalTimezone()
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) GetTP() *TimePrinter {
|
|
|
|
+ return this.tp
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatPrinterParsingAbstract) GetTPE() *TimePrinterEx {
|
|
|
|
+ return this.tpe
|
|
|
|
+}
|
|
|
|
|
|
type FormatParser struct {
|
|
type FormatParser struct {
|
|
tagList map[string]TimeElementFactoryFunc
|
|
tagList map[string]TimeElementFactoryFunc
|
|
ndTagList map[string]NonDisplayTagFunc
|
|
ndTagList map[string]NonDisplayTagFunc
|
|
|
|
+ exTagList map[string]TimeElementExFactoryFunc
|
|
tzsReg *regexp.Regexp
|
|
tzsReg *regexp.Regexp
|
|
|
|
+ isEx bool
|
|
}
|
|
}
|
|
|
|
|
|
-func NewFormatParser() *FormatParser {
|
|
|
|
|
|
+func NewFormatParser(isEx bool) *FormatParser {
|
|
f := &FormatParser{
|
|
f := &FormatParser{
|
|
tagList: make(map[string]TimeElementFactoryFunc),
|
|
tagList: make(map[string]TimeElementFactoryFunc),
|
|
ndTagList: make(map[string]NonDisplayTagFunc),
|
|
ndTagList: make(map[string]NonDisplayTagFunc),
|
|
|
|
+ exTagList: make(map[string]TimeElementExFactoryFunc),
|
|
|
|
+ isEx: isEx,
|
|
}
|
|
}
|
|
f.addDefaultElementClasses()
|
|
f.addDefaultElementClasses()
|
|
f.tzsReg, _ = regexp.Compile(regexpTimezoneTag)
|
|
f.tzsReg, _ = regexp.Compile(regexpTimezoneTag)
|
|
@@ -45,7 +112,11 @@ func (this *FormatParser) AddNonDisplayTag(tagname string, nf NonDisplayTagFunc)
|
|
this.ndTagList[tagname] = nf
|
|
this.ndTagList[tagname] = nf
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *FormatParser) ParseFormatString(tp *TimePrinter, fs string) {
|
|
|
|
|
|
+func (this *FormatParser) AddExDataTag(tagname string, ff TimeElementExFactoryFunc) {
|
|
|
|
+ this.exTagList[tagname] = ff
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *FormatParser) ParseFormatString(tp *FormatPrinterParsingAbstract, fs string) {
|
|
if len(fs) < 1 {
|
|
if len(fs) < 1 {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -56,7 +127,7 @@ func (this *FormatParser) ParseFormatString(tp *TimePrinter, fs string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *FormatParser) parseBasicFmt(tp *TimePrinter, fs string) {
|
|
|
|
|
|
+func (this *FormatParser) parseBasicFmt(tp *FormatPrinterParsingAbstract, fs string) {
|
|
s := fs
|
|
s := fs
|
|
s = strings.Replace(s, "!", "<#000>", -1)
|
|
s = strings.Replace(s, "!", "<#000>", -1)
|
|
s = strings.Replace(s, "yyyy", "<#001>", -1)
|
|
s = strings.Replace(s, "yyyy", "<#001>", -1)
|
|
@@ -86,7 +157,7 @@ func (this *FormatParser) parseBasicFmt(tp *TimePrinter, fs string) {
|
|
this.parseAdvancedFmt(tp, s)
|
|
this.parseAdvancedFmt(tp, s)
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *FormatParser) parseAdvancedFmt(tp *TimePrinter, fs string) {
|
|
|
|
|
|
+func (this *FormatParser) parseAdvancedFmt(tp *FormatPrinterParsingAbstract, fs string) {
|
|
if len(fs) > 1 {
|
|
if len(fs) > 1 {
|
|
if fs[0] == '!' {
|
|
if fs[0] == '!' {
|
|
this.parseBasicFmt(tp, fs[1:])
|
|
this.parseBasicFmt(tp, fs[1:])
|
|
@@ -117,7 +188,7 @@ func (this *FormatParser) parseAdvancedFmt(tp *TimePrinter, fs string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *FormatParser) processTimezoneTag(tp *TimePrinter, tzs string, tzname string) {
|
|
|
|
|
|
+func (this *FormatParser) processTimezoneTag(tp *FormatPrinterParsingAbstract, tzs string, tzname string) {
|
|
sg := 0
|
|
sg := 0
|
|
if tzs[0] == '+' {
|
|
if tzs[0] == '+' {
|
|
sg = 1
|
|
sg = 1
|
|
@@ -144,12 +215,19 @@ func (this *FormatParser) processTimezoneTag(tp *TimePrinter, tzs string, tzname
|
|
tp.UseTimezone(tz)
|
|
tp.UseTimezone(tz)
|
|
}
|
|
}
|
|
|
|
|
|
-func (this *FormatParser) processTag(tp *TimePrinter, tag string) {
|
|
|
|
|
|
+func (this *FormatParser) processTag(tp *FormatPrinterParsingAbstract, tag string) {
|
|
rrs := this.tzsReg.FindStringSubmatch(tag)
|
|
rrs := this.tzsReg.FindStringSubmatch(tag)
|
|
if len(rrs) == 3 {
|
|
if len(rrs) == 3 {
|
|
this.processTimezoneTag(tp, rrs[1], rrs[2])
|
|
this.processTimezoneTag(tp, rrs[1], rrs[2])
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+ if this.isEx {
|
|
|
|
+ v0, ok := this.exTagList[tag]
|
|
|
|
+ if ok {
|
|
|
|
+ tp.AddExDataTag(v0())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
v1, ok := this.ndTagList[tag]
|
|
v1, ok := this.ndTagList[tag]
|
|
if ok {
|
|
if ok {
|
|
v1(tp)
|
|
v1(tp)
|
|
@@ -164,8 +242,8 @@ func (this *FormatParser) processTag(tp *TimePrinter, tag string) {
|
|
}
|
|
}
|
|
|
|
|
|
func (this *FormatParser) addDefaultElementClasses() {
|
|
func (this *FormatParser) addDefaultElementClasses() {
|
|
- this.AddNonDisplayTag("UTC", func(tp *TimePrinter) { tp.UseUTC() })
|
|
|
|
- this.AddNonDisplayTag("Local", func(tp *TimePrinter) { tp.UseLocalTimezone() })
|
|
|
|
|
|
+ this.AddNonDisplayTag("UTC", func(tp *FormatPrinterParsingAbstract) { tp.UseUTC() })
|
|
|
|
+ this.AddNonDisplayTag("Local", func(tp *FormatPrinterParsingAbstract) { tp.UseLocalTimezone() })
|
|
this.AddElementClass("year", func() TimeElement { return tfelem.NewYearElement(false) })
|
|
this.AddElementClass("year", func() TimeElement { return tfelem.NewYearElement(false) })
|
|
this.AddElementClass("shortYear", func() TimeElement { return tfelem.NewYearElement(true) })
|
|
this.AddElementClass("shortYear", func() TimeElement { return tfelem.NewYearElement(true) })
|
|
this.AddElementClass("month", func() TimeElement { return tfelem.NewNumbericMonthElement(true) })
|
|
this.AddElementClass("month", func() TimeElement { return tfelem.NewNumbericMonthElement(true) })
|