Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more simple formats #33

Merged
merged 7 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 57 additions & 12 deletions pkg/epoch/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions pkg/epoch/epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand Down