FILE / ScuroNeko/SNekLog

time_format.go

Исходный файл и его история в репозитории.
FILE v2.0.0
Files
SNekLog/time_format.go
T
ScuroNeko d534f92d48 (refactor): prepare breaking v2 API
- rename package to sneklog and move module to /v2
- replace text output flags with writer formatters
- remove external color dependencies
- update migration notes and coverage
2026-04-24 15:43:14 +03:00

162 lines
3.6 KiB
Go

package sneklog
import "strings"
const (
Layout = "%m/%d %I:%M:%S%p '%y %z"
ANSIC = "%a %b %e %H:%M:%S %Y"
UnixDate = "%a %b %e %H:%M:%S %Z %Y"
RubyDate = "%a %b %d %H:%M:%S %z %Y"
RFC822 = "%d %b %y %H:%M %Z"
RFC822Z = "%d %b %y %H:%M %z"
RFC850 = "%A, %d-%b-%y %H:%M:%S %Z"
RFC1123 = "%a, %d %b %Y %H:%M:%S %Z"
RFC1123Z = "%a, %d %b %Y %H:%M:%S %z"
RFC3339 = "%Y-%m-%dT%H:%M:%S%:z"
RFC3339Nano = "%Y-%m-%dT%H:%M:%S%#N%:z"
Kitchen = "%-I:%M%p"
Stamp = "%b %e %H:%M:%S"
StampMilli = "%b %e %H:%M:%S.%L"
StampMicro = "%b %e %H:%M:%S.%f"
StampNano = "%b %e %H:%M:%S.%N"
DateTime = "%Y-%m-%d %H:%M:%S"
DateOnly = "%Y-%m-%d"
TimeOnly = "%H:%M:%S"
)
type rule struct {
strf string
goLayout string
}
var strftimeToGoRules = []rule{
// Long/special tokens first.
{"%::z", "Z07:00:00"},
{"%:::z", "Z07"},
{"%:z", "Z07:00"},
{"%#N", ".999999999"},
{"%#f", ".999999"},
{"%#L", ".999"},
{"%-m", "1"},
{"%#m", "1"},
{"%-d", "2"},
{"%#d", "2"},
{"%-H", "15"},
{"%#H", "15"},
{"%-I", "3"},
{"%#I", "3"},
{"%-M", "4"},
{"%#M", "4"},
{"%-S", "5"},
{"%#S", "5"},
{"%%", "%"},
{"%Y", "2006"},
{"%y", "06"},
{"%B", "January"},
{"%b", "Jan"},
{"%h", "Jan"},
{"%m", "01"},
{"%d", "02"},
{"%e", "_2"},
{"%A", "Monday"},
{"%a", "Mon"},
{"%H", "15"},
{"%I", "03"},
{"%M", "04"},
{"%S", "05"},
{"%p", "PM"},
{"%z", "-0700"},
{"%Z", "MST"},
{"%N", "000000000"},
{"%f", "000000"},
{"%L", "000"},
{"%F", "2006-01-02"},
{"%T", "15:04:05"},
{"%R", "15:04"},
{"%D", "01/02/06"},
{"%r", "03:04:05 PM"},
}
func StrftimeToGo(format string) string {
var out strings.Builder
for i := 0; i < len(format); {
if format[i] != '%' {
out.WriteByte(format[i])
i++
continue
}
matched := false
for _, r := range strftimeToGoRules {
if strings.HasPrefix(format[i:], r.strf) {
out.WriteString(r.goLayout)
i += len(r.strf)
matched = true
break
}
}
if !matched {
// Можно заменить на ошибку, если хочешь strict mode.
out.WriteByte(format[i])
i++
}
}
return out.String()
}
func GoToStrftime(format string) string {
format = strings.ReplaceAll(format, "2006", "%Y")
format = strings.ReplaceAll(format, "06", "%y")
format = strings.ReplaceAll(format, "15", "%H")
format = strings.ReplaceAll(format, "03", "%I")
format = strings.ReplaceAll(format, "3", "%-I")
format = strings.ReplaceAll(format, "3", "%#I")
format = strings.ReplaceAll(format, "January", "%B")
format = strings.ReplaceAll(format, "Jan", "%b")
format = strings.ReplaceAll(format, "01", "%m")
format = strings.ReplaceAll(format, "1", "%-m")
format = strings.ReplaceAll(format, "02", "%d")
format = strings.ReplaceAll(format, "_2", "%e")
format = strings.ReplaceAll(format, "2", "%-d")
format = strings.ReplaceAll(format, "Monday", "%A")
format = strings.ReplaceAll(format, "Mon", "%a")
format = strings.ReplaceAll(format, "04", "%M")
format = strings.ReplaceAll(format, "05", "%S")
format = strings.ReplaceAll(format, ".999999999", "%#N")
format = strings.ReplaceAll(format, "000000000", "%N")
format = strings.ReplaceAll(format, ".999999", "%#f")
format = strings.ReplaceAll(format, "000000", "%f")
format = strings.ReplaceAll(format, ".999", "%#L")
format = strings.ReplaceAll(format, "000", "%L")
format = strings.ReplaceAll(format, "PM", "%p")
format = strings.ReplaceAll(format, "-0700", "%z")
format = strings.ReplaceAll(format, "MST", "%Z")
format = strings.ReplaceAll(format, "Z07:00", "%:z")
format = strings.ReplaceAll(format, "Z07:00:00", "%::z")
format = strings.ReplaceAll(format, "Z07", "%:::z")
return format
}