From a56c1075be03b225ef52246624e6b095d9c133b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20J=C3=BCrgensmeyer?= <6493966+sj14@users.noreply.github.com> Date: Sun, 23 Apr 2023 11:44:45 +0200 Subject: [PATCH] Add more simple formats (#33) --- pkg/epoch/epoch.go | 69 ++++++++++++++++++++++++++++++++++------- pkg/epoch/epoch_test.go | 8 ++--- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/pkg/epoch/epoch.go b/pkg/epoch/epoch.go index f6ddc1f..a51806f 100644 --- a/pkg/epoch/epoch.go +++ b/pkg/epoch/epoch.go @@ -341,18 +341,63 @@ func FormatName(format string) (string, error) { // FormatSimple converts a simple format to Go's native formatting. func FormatSimple(format string) string { - format = strings.ReplaceAll(format, "YYYY", "2006") // Long year - format = strings.ReplaceAll(format, "YY", "06") // Short year - format = strings.ReplaceAll(format, "MM", "01") // Month (2-digit) - format = strings.ReplaceAll(format, "M", "1") // Month (1-digit) - format = strings.ReplaceAll(format, "DD", "02") // Day (2-digit) - format = strings.ReplaceAll(format, "D", "2") // Day (1-digit) - - format = strings.ReplaceAll(format, "hh", "15") // Hour (2-digit) - format = strings.ReplaceAll(format, "mm", "04") // Minute (2-digit) - format = strings.ReplaceAll(format, "m", "4") // Minute (1-digit) - format = strings.ReplaceAll(format, "ss", "05") // Second (2-digit) - format = strings.ReplaceAll(format, "s", "5") // Second (1-digit) + format = strings.ReplaceAll(format, "{YYYY}", "2006") // Long year + format = strings.ReplaceAll(format, "{YY}", "06") // Short year + + format = strings.ReplaceAll(format, "{MMMM}", "January") + format = strings.ReplaceAll(format, "{MMM}", "Jan") + format = strings.ReplaceAll(format, "{MM}", "01") // Month (2-digit) + format = strings.ReplaceAll(format, "{M}", "1") // Month (1-digit) + + format = strings.ReplaceAll(format, "{DDDD}", "002") // Day of year + format = strings.ReplaceAll(format, "{DDDD}", "__2") // Day of year + + format = strings.ReplaceAll(format, "{DD}", "02") // Day of month (2-digit) + format = strings.ReplaceAll(format, "{D}", "2") // Day of month (1-digit) + + format = strings.ReplaceAll(format, "{dddd}", "Monday") // Day of week + format = strings.ReplaceAll(format, "{ddd}", "Mon") // Day of week + + format = strings.ReplaceAll(format, "{HH}", "15") // Hour 24 (2-digit) + format = strings.ReplaceAll(format, "{hh}", "03") // Hour 12 (2-digit) + format = strings.ReplaceAll(format, "{h}", "3") // Hour 12 (1-digit) + + format = strings.ReplaceAll(format, "{A}", "PM") + format = strings.ReplaceAll(format, "{a}", "pm") + + format = strings.ReplaceAll(format, "{mm}", "04") // Minute (2-digit) + format = strings.ReplaceAll(format, "{m}", "4") // Minute (1-digit) + + format = strings.ReplaceAll(format, "{ss}", "05") // Second (2-digit) + format = strings.ReplaceAll(format, "{s}", "5") // Second (1-digit) + + // Arbitrary precision of fractional seconds. + // The dot won't be removed from the output :-/ + format = strings.ReplaceAll(format, "{F}", ".0") + format = strings.ReplaceAll(format, "{FF}", ".00") + format = strings.ReplaceAll(format, "{FFF}", ".000") + format = strings.ReplaceAll(format, "{FFFF}", ".0000") + format = strings.ReplaceAll(format, "{FFFFF}", ".00000") + format = strings.ReplaceAll(format, "{FFFFFF}", ".000000") + format = strings.ReplaceAll(format, "{FFFFFFF}", ".0000000") + format = strings.ReplaceAll(format, "{FFFFFFFF}", ".00000000") + format = strings.ReplaceAll(format, "{FFFFFFFFF}", ".000000000") + + format = strings.ReplaceAll(format, "{f}", ".9") + format = strings.ReplaceAll(format, "{ff}", ".99") + format = strings.ReplaceAll(format, "{fff}", ".999") + format = strings.ReplaceAll(format, "{ffff}", ".9999") + format = strings.ReplaceAll(format, "{fffff}", ".99999") + format = strings.ReplaceAll(format, "{ffffff}", ".999999") + format = strings.ReplaceAll(format, "{fffffff}", ".9999999") + format = strings.ReplaceAll(format, "{ffffffff}", ".99999999") + format = strings.ReplaceAll(format, "{fffffffff}", ".999999999") + + // Timezone / Offset + format = strings.ReplaceAll(format, "{ZZZ}", "-07:00") + format = strings.ReplaceAll(format, "{ZZ}", "-0700") + format = strings.ReplaceAll(format, "{Z}", "-07") + format = strings.ReplaceAll(format, "{z}", "MST") return format } diff --git a/pkg/epoch/epoch_test.go b/pkg/epoch/epoch_test.go index ab75fef..c10b119 100644 --- a/pkg/epoch/epoch_test.go +++ b/pkg/epoch/epoch_test.go @@ -290,22 +290,22 @@ func TestFormatSimple(t *testing.T) { }{ { name: "year-month-day long", - format: "YYYY-MM-DD", + format: "{YYYY}-{MM}-{DD}", want: "2022-09-08", }, { name: "month/day/year short", - format: "M/D/YY", + format: "{M}/{D}/{YY}", want: "9/8/22", }, { name: "hour:minute:second long", - format: "hh:mm:ss", + format: "{hh}:{mm}:{ss}", want: "07:06:05", }, { name: "second-minute short", - format: "s-m", + format: "{s}-{m}", want: "5-6", }, }