From a812d5e66314815db26d0fc951ace9eece4c66c6 Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Thu, 18 Aug 2016 09:49:48 -0400 Subject: Add FmtDateTime helper functions. --- xog/xog.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'xog') diff --git a/xog/xog.go b/xog/xog.go index 643ea46e..b491ed73 100644 --- a/xog/xog.go +++ b/xog/xog.go @@ -71,7 +71,7 @@ func New() locales.Translator { erasAbbreviated: [][]uint8{{0x41, 0x5a}, {0x41, 0x46}}, erasNarrow: [][]uint8{[]uint8(nil), []uint8(nil)}, erasWide: [][]uint8{{0x4b, 0x75, 0x6c, 0x69, 0x73, 0x74, 0x6f, 0x20, 0x6e, 0x67, 0x61, 0x20, 0x61, 0x7a, 0x69, 0x6c, 0x61, 0x77, 0x6f}, {0x4b, 0x75, 0x6c, 0x69, 0x73, 0x74, 0x6f, 0x20, 0x6e, 0x67, 0x61, 0x20, 0x61, 0x66, 0x66, 0x69, 0x6c, 0x65}}, - timezones: map[string][]uint8{"AEST": {0x41, 0x45, 0x53, 0x54}, "MESZ": {0x4d, 0x45, 0x53, 0x5a}, "SGT": {0x53, 0x47, 0x54}, "AKDT": {0x41, 0x4b, 0x44, 0x54}, "CAT": {0x43, 0x41, 0x54}, "IST": {0x49, 0x53, 0x54}, "WART": {0x57, 0x41, 0x52, 0x54}, "HAT": {0x48, 0x41, 0x54}, "GYT": {0x47, 0x59, 0x54}, "JST": {0x4a, 0x53, 0x54}, "MST": {0x4d, 0x53, 0x54}, "UYST": {0x55, 0x59, 0x53, 0x54}, "CLT": {0x43, 0x4c, 0x54}, "ADT": {0x41, 0x44, 0x54}, "ChST": {0x43, 0x68, 0x53, 0x54}, "WAST": {0x57, 0x41, 0x53, 0x54}, "MEZ": {0x4d, 0x45, 0x5a}, "CDT": {0x43, 0x44, 0x54}, "LHDT": {0x4c, 0x48, 0x44, 0x54}, "HKT": {0x48, 0x4b, 0x54}, "WIT": {0x57, 0x49, 0x54}, "UYT": {0x55, 0x59, 0x54}, "ARST": {0x41, 0x52, 0x53, 0x54}, "SRT": {0x53, 0x52, 0x54}, "AWDT": {0x41, 0x57, 0x44, 0x54}, "AKST": {0x41, 0x4b, 0x53, 0x54}, "MDT": {0x4d, 0x44, 0x54}, "WITA": {0x57, 0x49, 0x54, 0x41}, "LHST": {0x4c, 0x48, 0x53, 0x54}, "GFT": {0x47, 0x46, 0x54}, "NZST": {0x4e, 0x5a, 0x53, 0x54}, "ART": {0x41, 0x52, 0x54}, "OEZ": {0x4f, 0x45, 0x5a}, "JDT": {0x4a, 0x44, 0x54}, "NZDT": {0x4e, 0x5a, 0x44, 0x54}, "CST": {0x43, 0x53, 0x54}, "MYT": {0x4d, 0x59, 0x54}, "CHAST": {0x43, 0x48, 0x41, 0x53, 0x54}, "HNT": {0x48, 0x4e, 0x54}, "WESZ": {0x57, 0x45, 0x53, 0x5a}, "HADT": {0x48, 0x41, 0x44, 0x54}, "PDT": {0x50, 0x44, 0x54}, "TMST": {0x54, 0x4d, 0x53, 0x54}, "SAST": {0x53, 0x41, 0x53, 0x54}, "ACWDT": {0x41, 0x43, 0x57, 0x44, 0x54}, "HKST": {0x48, 0x4b, 0x53, 0x54}, "TMT": {0x54, 0x4d, 0x54}, "GMT": {0x47, 0x4d, 0x54}, "COST": {0x43, 0x4f, 0x53, 0x54}, "EDT": {0x45, 0x44, 0x54}, "ACST": {0x41, 0x43, 0x53, 0x54}, "CHADT": {0x43, 0x48, 0x41, 0x44, 0x54}, "WARST": {0x57, 0x41, 0x52, 0x53, 0x54}, "EST": {0x45, 0x53, 0x54}, "AWST": {0x41, 0x57, 0x53, 0x54}, "AST": {0x41, 0x53, 0x54}, "WIB": {0x57, 0x49, 0x42}, "BT": {0x42, 0x54}, "∅∅∅": {0xe2, 0x88, 0x85, 0xe2, 0x88, 0x85, 0xe2, 0x88, 0x85}, "PST": {0x50, 0x53, 0x54}, "COT": {0x43, 0x4f, 0x54}, "CLST": {0x43, 0x4c, 0x53, 0x54}, "WAT": {0x57, 0x41, 0x54}, "ACDT": {0x41, 0x43, 0x44, 0x54}, "ACWST": {0x41, 0x43, 0x57, 0x53, 0x54}, "ECT": {0x45, 0x43, 0x54}, "AEDT": {0x41, 0x45, 0x44, 0x54}, "VET": {0x56, 0x45, 0x54}, "EAT": {0x45, 0x41, 0x54}, "BOT": {0x42, 0x4f, 0x54}, "WEZ": {0x57, 0x45, 0x5a}, "HAST": {0x48, 0x41, 0x53, 0x54}, "OESZ": {0x4f, 0x45, 0x53, 0x5a}}, + timezones: map[string][]uint8{"MDT": {0x4d, 0x44, 0x54}, "HKST": {0x48, 0x4b, 0x53, 0x54}, "AWST": {0x41, 0x57, 0x53, 0x54}, "HAT": {0x48, 0x41, 0x54}, "ChST": {0x43, 0x68, 0x53, 0x54}, "ECT": {0x45, 0x43, 0x54}, "WARST": {0x57, 0x41, 0x52, 0x53, 0x54}, "MYT": {0x4d, 0x59, 0x54}, "UYST": {0x55, 0x59, 0x53, 0x54}, "LHST": {0x4c, 0x48, 0x53, 0x54}, "HKT": {0x48, 0x4b, 0x54}, "CAT": {0x43, 0x41, 0x54}, "AKDT": {0x41, 0x4b, 0x44, 0x54}, "TMT": {0x54, 0x4d, 0x54}, "MST": {0x4d, 0x53, 0x54}, "CLST": {0x43, 0x4c, 0x53, 0x54}, "SGT": {0x53, 0x47, 0x54}, "WITA": {0x57, 0x49, 0x54, 0x41}, "NZST": {0x4e, 0x5a, 0x53, 0x54}, "IST": {0x49, 0x53, 0x54}, "ACST": {0x41, 0x43, 0x53, 0x54}, "∅∅∅": {0xe2, 0x88, 0x85, 0xe2, 0x88, 0x85, 0xe2, 0x88, 0x85}, "PDT": {0x50, 0x44, 0x54}, "ADT": {0x41, 0x44, 0x54}, "MEZ": {0x4d, 0x45, 0x5a}, "OESZ": {0x4f, 0x45, 0x53, 0x5a}, "CHAST": {0x43, 0x48, 0x41, 0x53, 0x54}, "WAST": {0x57, 0x41, 0x53, 0x54}, "OEZ": {0x4f, 0x45, 0x5a}, "HAST": {0x48, 0x41, 0x53, 0x54}, "NZDT": {0x4e, 0x5a, 0x44, 0x54}, "GFT": {0x47, 0x46, 0x54}, "PST": {0x50, 0x53, 0x54}, "TMST": {0x54, 0x4d, 0x53, 0x54}, "VET": {0x56, 0x45, 0x54}, "COT": {0x43, 0x4f, 0x54}, "AEST": {0x41, 0x45, 0x53, 0x54}, "CHADT": {0x43, 0x48, 0x41, 0x44, 0x54}, "SRT": {0x53, 0x52, 0x54}, "JST": {0x4a, 0x53, 0x54}, "HNT": {0x48, 0x4e, 0x54}, "BT": {0x42, 0x54}, "CLT": {0x43, 0x4c, 0x54}, "GYT": {0x47, 0x59, 0x54}, "ACWST": {0x41, 0x43, 0x57, 0x53, 0x54}, "COST": {0x43, 0x4f, 0x53, 0x54}, "ACDT": {0x41, 0x43, 0x44, 0x54}, "AKST": {0x41, 0x4b, 0x53, 0x54}, "WEZ": {0x57, 0x45, 0x5a}, "JDT": {0x4a, 0x44, 0x54}, "ART": {0x41, 0x52, 0x54}, "ACWDT": {0x41, 0x43, 0x57, 0x44, 0x54}, "WART": {0x57, 0x41, 0x52, 0x54}, "CDT": {0x43, 0x44, 0x54}, "WAT": {0x57, 0x41, 0x54}, "AWDT": {0x41, 0x57, 0x44, 0x54}, "LHDT": {0x4c, 0x48, 0x44, 0x54}, "BOT": {0x42, 0x4f, 0x54}, "MESZ": {0x4d, 0x45, 0x53, 0x5a}, "GMT": {0x47, 0x4d, 0x54}, "EST": {0x45, 0x53, 0x54}, "AEDT": {0x41, 0x45, 0x44, 0x54}, "WIB": {0x57, 0x49, 0x42}, "WESZ": {0x57, 0x45, 0x53, 0x5a}, "EDT": {0x45, 0x44, 0x54}, "WIT": {0x57, 0x49, 0x54}, "EAT": {0x45, 0x41, 0x54}, "AST": {0x41, 0x53, 0x54}, "CST": {0x43, 0x53, 0x54}, "HADT": {0x48, 0x41, 0x44, 0x54}, "UYT": {0x55, 0x59, 0x54}, "ARST": {0x41, 0x52, 0x53, 0x54}, "SAST": {0x53, 0x41, 0x53, 0x54}}, } } @@ -361,6 +361,7 @@ func (xog *xog) FmtTimeShort(t time.Time) []byte { b = strconv.AppendInt(b, int64(t.Hour()), 10) b = append(b, xog.timeSeparator...) + if t.Minute() < 10 { b = append(b, '0') } @@ -383,12 +384,14 @@ func (xog *xog) FmtTimeMedium(t time.Time) []byte { b = strconv.AppendInt(b, int64(t.Hour()), 10) b = append(b, xog.timeSeparator...) + if t.Minute() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Minute()), 10) b = append(b, xog.timeSeparator...) + if t.Second() < 10 { b = append(b, '0') } @@ -411,12 +414,14 @@ func (xog *xog) FmtTimeLong(t time.Time) []byte { b = strconv.AppendInt(b, int64(t.Hour()), 10) b = append(b, xog.timeSeparator...) + if t.Minute() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Minute()), 10) b = append(b, xog.timeSeparator...) + if t.Second() < 10 { b = append(b, '0') } @@ -443,12 +448,14 @@ func (xog *xog) FmtTimeFull(t time.Time) []byte { b = strconv.AppendInt(b, int64(t.Hour()), 10) b = append(b, xog.timeSeparator...) + if t.Minute() < 10 { b = append(b, '0') } b = strconv.AppendInt(b, int64(t.Minute()), 10) b = append(b, xog.timeSeparator...) + if t.Second() < 10 { b = append(b, '0') } @@ -457,6 +464,7 @@ func (xog *xog) FmtTimeFull(t time.Time) []byte { b = append(b, []byte{0x20}...) tz, _ := t.Zone() + if btz, ok := xog.timezones[tz]; ok { b = append(b, btz...) } else { @@ -465,3 +473,51 @@ func (xog *xog) FmtTimeFull(t time.Time) []byte { return b } + +// FmtDateTimeShort returns the short date & time representation of 't' for 'xog' +// returned as a []byte just in case the caller wishes to add more and can help +// avoid allocations; otherwise just cast as string. +func (xog *xog) FmtDateTimeShort(t time.Time) []byte { + + b := xog.FmtDateShort(t) + b = append(b, ' ') + b = append(b, xog.FmtTimeShort(t)...) + + return b +} + +// FmtDateTimeMedium returns the medium date & time representation of 't' for 'xog' +// returned as a []byte just in case the caller wishes to add more and can help +// avoid allocations; otherwise just cast as string. +func (xog *xog) FmtDateTimeMedium(t time.Time) []byte { + + b := xog.FmtDateMedium(t) + b = append(b, ' ') + b = append(b, xog.FmtTimeMedium(t)...) + + return b +} + +// FmtDateTimeLong returns the long date & time representation of 't' for 'xog' +// returned as a []byte just in case the caller wishes to add more and can help +// avoid allocations; otherwise just cast as string. +func (xog *xog) FmtDateTimeLong(t time.Time) []byte { + + b := xog.FmtDateLong(t) + b = append(b, ' ') + b = append(b, xog.FmtTimeLong(t)...) + + return b +} + +// FmtDateTimeFull returns the full date & time representation of 't' for 'xog' +// returned as a []byte just in case the caller wishes to add more and can help +// avoid allocations; otherwise just cast as string. +func (xog *xog) FmtDateTimeFull(t time.Time) []byte { + + b := xog.FmtDateFull(t) + b = append(b, ' ') + b = append(b, xog.FmtTimeFull(t)...) + + return b +} -- cgit v1.2.3