Skip to content

Commit

Permalink
Add more simple formats (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
sj14 authored Apr 23, 2023
1 parent aa19e84 commit a56c107
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
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

0 comments on commit a56c107

Please sign in to comment.