You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/ntime/template.go

44 lines
1.0 KiB
Go

package ntime
import "git.noahlan.cn/noahlan/ntool/nstr/ac"
var matches = []string{
"ddd", "dd", "d",
"HH", "hh", "h", // HH:0-23 hh:0-12
"mm", "m",
"ss", "s",
"yyyy", "yy", "y",
"SSS",
"a", "aa",
"MMMM", "MMM", "MM", "M",
"ZZ", "Z", "zz:zz", "zzzz", "z",
"EEEE", "E",
}
var replaceWith = []string{
"_2", "02", "2",
"15", "03", "3",
"04", "4",
"05", "5",
"2006", "06", "06",
"000",
"pm", "PM",
"January", "Jan", "01", "1",
"-0700", "-07", "Z07:00", "Z0700", "MST",
"Monday", "Mon",
}
// ToLayout converts java date format,
// https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#rfc822timezone into go date layout
func ToLayout(dateFormat string) string {
acBuilder := ac.NewAhoCorasickBuilder(ac.Opts{
AsciiCaseInsensitive: false, // 大小写敏感
MatchOnlyWholeWords: false,
MatchKind: ac.LeftMostLongestMatch, // 最左最长匹配
DFA: true,
})
aho := acBuilder.Build(matches)
replacer := ac.NewReplacer(aho)
return replacer.ReplaceAll(dateFormat, replaceWith)
}