Yet Another Golang Time Formatter

zry 7e4b966165 Fix Documentation 2019-09-19 11:37 4 years ago
.idea 78ebdfc565 First Distribution. 4 years ago
tests 03bb9f4e7b Add 'TimeFormatEx' and its dependency. 4 years ago
yagtf 03bb9f4e7b Add 'TimeFormatEx' and its dependency. 4 years ago
.gitignore 471eb28375 initial commit 4 years ago
LICENSE 471eb28375 initial commit 4 years ago
README.md 7e4b966165 Fix Documentation 2019-09-19 11:37 4 years ago

README.md

YAGTF

Yet Another Golang Time Formatter

English Documentation

Tradition Mode

Directly using the format string like 'yyyy-MM-dd HH:mm:ss'. It will be translated into advanced mode format string by simply string replacing.

Just for compatibility, not recommend for complex format.

Symbols:

Symbol Function Description Will Be Replaced To Output Example
yyyy long year <year> 2019
yy short year <shortYear> 96
MM month with zero fill <month> 02
M3 month in English abbr. <monthAbbr> Jan
M09 month in English with zero fill <monthName> January
M9 month in English without zero fill <monthNameNoFill> January
M month without zero fill <monthNoFill> 2
dd day with zero fill <day> 05
d day without zero fill <dayNoFill> 5
HH 24h hour with zero fill <hour24> 13
hh 12h hour with zero fill <hour12> 01
H 24h hour without zero fill <hour24NoFill> 13
h 12h hour without zero fill <hour12NoFill> 1
mm minute with zero fill <minute> 01
ss second with zero fill <second> 05
E09 weekday in English with zero fill <weekdayName> Monday
E9 weekday in English without zero fill <weekdayNameNoFill> Monday
E3 weekday in English abbr. <weekdayAbbr> Mon
A AM/PM <ampm> AM
S1 millisecond <ms> 026
S2 microsecond <us> 114514
S3 nanosecond <ns> 019810019
Z timezone abbr. <timezoneAbbr> CST
z timezone UTC offset <timezone> +0800

Examples:

yyyy-MM-dd HH:mm:ss -> 2000-02-15 19:27:35

yy M3 dd E09 hh:mm:ss.S2 A -> 96 Nov 14 Thursday 01:05:27.114514 PM

Advanced Mode

Start your format string with !. If you want to use tradition mode, but your formatting string start with !, please start with !! to instead of !.

(The ! in middle of string will be auto-escaped)

Pure Text

Directly type pure text. For escaping !, use <!>

Examples:

hello, gensokyo<!> -> hello, gensokyo!

Tag

Use < and > to wrap the name of tag. For escaping < and '>', using <lt> and <gt>

Examples:

<year>-<month>-<day> -> 2019-01-02

<lt><hour>:<minute>:<second><gt><!> -> <16:05:27>!

Non-display Tags

Some of tags will not be displayed, they just be used for set some options.

Examples:

<UTC><hour>:<minute> -> 08:05

Display Tag List

Tag Function Description Output Example
<year> long year 2019
<shortYear> short year 96
<month> month with zero fill 02
<monthNoFill> month without zero fill 2
<monthAbbr> month in English abbr. Jan
<monthName> month in English with zero fill January
<monthNameNoFill> month in English without zero fill January
<day> day with zero fill 05
<dayNoFill> day without zero fill 5
<hour24> 24h hour with zero fill 13
<hour12> 12h hour with zero fill 01
<hour24NoFill> 24h hour without zero fill 13
<hour12NoFill> 12h hour without zero fill 1
<minute> minute with zero fill 01
<second> second with zero fill 05
<weekday> weekday in numberic 1
<weekdayName> weekday in English with zero fill Monday
<weekdayNameNoFill> weekday in English without zero fill Monday
<weekdayAbbr> weekday in English abbr. Mon
<ampm> AM/PM AM
<ms> millisecond 026
<us> microsecond 114514
<ns> nanosecond 019810019
<yearweek> the week in this year with zero fill 06
<yearweekNoFill> the week in this year without zero fill 6
<yearday> the day in this year with zero fill 034
<yeardayNoFill> the day in this year without zero fill 34
<timezone> timezone UTC offset +0800
<timezoneAbbr> timezone abbr. CST
<timezoneSec> timezone offset in seconds with zero fill 03600
<timezoneSecNoFill> timezone offset in seconds without zero fill 3600
<iso8601date> ISO8601 date 2019-01-01
<iso8601time> ISO8601 time 19:28:31
<iso8601full> ISO8601 full format 2019-01-01T19:28:31+0800
<common> common format 2019-01-01 19:28:31
<nginx> nginx default time format 01/Jan/2019:19:28:31 +0800
<!> exclamation mark !
<lt> less-than mark <
<gt> great-than mark >
<q> quote mark "
<sq> single quote mark '
<sp> space
<tab> tab
<br> linebreak '\n' (valid in multi-line mode only)
<cr> linebreak '\r' (valid in multi-line mode only)

Alias

Alias For Tag
<y> <year>
<sy> <shortYear>
<mon> <month>
<monNF> <monthNoFill>
<monA> <monthAbbr>
<monN> <monthName>
<monNNF> <monthNameNoFill>
<d> <day>
<dNF> <dayNoFill>
<h24> <hour24>
<h12> <hour12>
<h24NF> <hour24NoFill>
<h12NF> <hour12NoFill>
<min> <minute>
<s> <second>
<wd> <weekday>
<wdN> <weekdayName>
<wdNNF> <weekdayNameNoFill>
<wdA> <weekdayAbbr>
<yw> <yearweek>
<ywNF> <yearweekNoFill>
<yd> <yearday>
<ydNF> <yeardayNoFill>
<tz> <timezone>
<tzA> <timezoneAbbr>
<tzS> <timezoneSec>
<tzSNF> <timezoneSecNoFill>
<i8d> <iso8601date>
<i8t> <iso8601time>
<i8f> <iso8601full>
<lf> <br>

Non-display Tag List

Tag Function Description
<UTC> use UTC timezone for output
<Local> use local timezone for output
<Timezone:> *use specified timezone for output
  • Instuction For <Timezone:> Tag:

Specified timezone to use follow the colon. Using UTC offset format with name followed.

Example:

<Timezone:+0800CST> will specified the timezone to CST(+0800) for output.

External Tags

By Using TimeFormatterEx, you can append your custom tags.

Using This Package In Go

Installation

go get git.swzry.com/zry/YAGTF

Coding

With Basic Formatter

package main

import (
	"fmt"
	"git.swzry.com/zry/YAGTF/yagtf/timefmt"
	"time"
)

func main() {
	tf := timefmt.NewDefaultTimeFormatter("!<y>-<mon>-<d> <h24>:<min>:<s> <tz> <tzA>", false)
	fmt.Println(tf.Format(time.Now()))
	tf = timefmt.NewDefaultTimeFormatter("!<UTC><y>-<mon>-<d> <h24>:<min>:<s> <tz> <tzA>", false)
	fmt.Println(tf.Format(time.Now()))
	tf = timefmt.NewDefaultTimeFormatter("!<Timezone:+0900JST><y>-<mon>-<d> <h24>:<min>:<s><br><tz> <tzA>", true)
	fmt.Println(tf.Format(time.Now()))
	tf = timefmt.NewDefaultTimeFormatter("!<Timezone:+0900JST><y>-<mon>-<d> <h24>:<min>:<s><br><tz> <tzA>", false)
	fmt.Println(tf.Format(time.Now()))
	tf = timefmt.NewDefaultTimeFormatter("yyyy-MM-dd HH:mm:ss z Z", false)
	fmt.Println(tf.Format(time.Now()))
	tf = timefmt.NewDefaultTimeFormatter("!yyyy-MM-dd HH:mm:ss z Z", false)
	fmt.Println(tf.Format(time.Now()))
	tf = timefmt.NewDefaultTimeFormatter("!!yyyy-MM-dd HH:mm:ss z Z", false)
	fmt.Println(tf.Format(time.Now()))
}

With TimeFormatterEx:

package main

import (
	"fmt"
	"git.swzry.com/zry/YAGTF/yagtf/tfelem"
	"git.swzry.com/zry/YAGTF/yagtf/timefmt"
	"time"
)

func main() {
	printer := timefmt.NewTimePrinterEx()
	parser := timefmt.NewFormatParser(true)
	parser.AddExDataTag("level", func() timefmt.TimeElementEx {
		return tfelem.NewExDataElement(4, "level")
	})
	parser.AddExDataTag("app", func() timefmt.TimeElementEx {
		return tfelem.NewExDataElement(64, "app")
	})
	parser.AddExDataTag("msg", func() timefmt.TimeElementEx {
		return tfelem.NewExDataElement(2048, "msg")
	})
	tf := timefmt.NewTimeFormatterEx(printer, parser, false)
	tf.ParseFormat("![<y>-<mon>-<d> <h24>:<min>:<s> <tz> <tzA>] <lt><level><gt> <app> : <msg>")
	logtest(tf, "INFO", "test1.app", "hello, world")
	logtest(tf, "WARN", "test2.app", "hello, gensokyo")
	logtest(tf, "INFO", "test3.app", "the quick brown fox jumps over a lazy dog.")
	logtest(tf, "INFO", "test4.app", "Unicode Test With Emoji 1:☁⚡🪁🗝️👨, 👨💡, 📍✔️. ⛪❌📍. ☁⚡, ☁⚡⛪, ⛪🔥, 🏢️📍✔️.")
	logtest(tf, "INFO", "test5.app", "Unicode Test With Emoji 2:🇨🇳, 1980 < 📅 < 2015, 👶✖️1 = ✔️; 📅 > 2015, 👶✖️2 = ✔️.")
	logtest(tf, "INFO", "test6.app", "Unicode Test With Emoji 3:👦✂🔌, ⚡👦, 🚑🏥, 😷💉, 😷😱, 👦☠️. 👵🏽😭, 💴⚰️, 👮⚰️❌, 🔥💀->⚱️✔️.")
	logtest(tf, "INFO", "test7.app", "Unicode Test With Emoji 4:👦🚬🚄, 👮🚭, 💴, 👦👿, 👦💣💥🚔, 👮😱, 👮🔫👦, 👦☠️.")
	logtest(tf, "INFO", "test8.app", "Unicode Test With Chinese and Emoji: 魔理沙偷走了重要的📚, 帕秋莉👿.")
}

func logtest(tf *timefmt.TimeFormatterEx, level string, app string, msg string) {
	exdata := make(map[string]string)
	exdata["level"] = level
	exdata["app"] = app
	exdata["msg"] = msg
	fmt.Println(tf.Format(time.Now(), exdata))
}

中文文档

待完善...