From f4a4dbd672d6a07f43d06d9672755feac1974452 Mon Sep 17 00:00:00 2001 From: Brent Beardsley Date: Fri, 29 Mar 2019 08:12:06 -0600 Subject: [PATCH] v0.3.1, show built in formats in help, added UnixSeconds as output format --- iso8601_replacer.go | 4 +++ main.go | 73 +++++++++++++++++++++++---------------------- release.sh | 2 +- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/iso8601_replacer.go b/iso8601_replacer.go index bfd9eeb..254fad1 100644 --- a/iso8601_replacer.go +++ b/iso8601_replacer.go @@ -2,6 +2,7 @@ package main import ( "regexp" + "strconv" "strings" "time" "unicode" @@ -117,6 +118,9 @@ func (replacer Iso8601Replacer) ReplaceDates(input string, format string, locati if err != nil { panic(err.Error()) } + if format == unixSeconds { + return strconv.FormatInt(t.Unix(), 10) + } return t.In(location).Format(format) }) } diff --git a/main.go b/main.go index c38630a..8a17740 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,32 @@ import ( "flag" "fmt" "os" + "sort" + "strings" "time" ) -const version = "0.3.0" +const version = "0.3.1" +const unixSeconds = "UnixSeconds" + +var builtInFormats = map[string]string{ + "ANSIC": time.ANSIC, + "UnixDate": time.UnixDate, + "RubyDate": time.RubyDate, + "RFC822": time.RFC822, + "RFC822Z": time.RFC822Z, + "RFC850": time.RFC850, + "RFC1123": time.RFC1123, + "RFC1123Z": time.RFC1123Z, + "RFC3339": time.RFC3339, + "RFC3339Nano": time.RFC3339Nano, + "Kitchen": time.Kitchen, + "Stamp": time.Stamp, + "StampMilli": time.StampMilli, + "StampMicro": time.StampMicro, + "StampNano": time.StampNano, + unixSeconds: unixSeconds, +} func printUsage() { fmt.Fprintln(os.Stderr, "Usage") @@ -24,41 +46,20 @@ func printUsage() { fmt.Fprintln(os.Stderr, " - -> pipe input with timestamps from stdin") } +func getBuiltInFormatKeys() []string { + keys := make([]string, 0) + for key := range builtInFormats { + keys = append(keys, key) + } + return keys +} + func getFormat(formatString string) string { - switch formatString { - case "ANSIC": - return time.ANSIC - case "UnixDate": - return time.UnixDate - case "RubyDate": - return time.RubyDate - case "RFC822": - return time.RFC822 - case "RFC822Z": - return time.RFC822Z - case "RFC850": - return time.RFC850 - case "RFC1123": - return time.RFC1123 - case "RFC1123Z": - return time.RFC1123Z - case "RFC3339": - return time.RFC3339 - case "RFC3339Nano": - return time.RFC3339Nano - case "Kitchen": - return time.Kitchen - case "Stamp": - return time.Stamp - case "StampMilli": - return time.StampMilli - case "StampMicro": - return time.StampMicro - case "StampNano": - return time.StampNano - default: - return formatString + builtInFormat, ok := builtInFormats[formatString] + if ok { + return builtInFormat } + return formatString } func getLocation(locationString string) *time.Location { @@ -70,8 +71,10 @@ func getLocation(locationString string) *time.Location { } func main() { + builtinFormatKeys := getBuiltInFormatKeys() + sort.Strings(builtinFormatKeys) locationPtr := flag.String("location", "Local", "tzdata location to convert to") - formatPtr := flag.String("format", "Mon 2006 Jan 02 03:04pm MST", "format to use") + formatPtr := flag.String("format", "Mon 2006 Jan 02 03:04pm MST", "format to use (options \""+strings.Join(builtinFormatKeys, "\", \"")+"\"") versionPtr := flag.Bool("version", false, "print version number and exit") typePtr := flag.String("type", "iso8601", "what type of timestamps in the input (options \"iso8601\", \"unix\")") flag.Parse() diff --git a/release.sh b/release.sh index ebb76b0..d895061 100755 --- a/release.sh +++ b/release.sh @@ -2,7 +2,7 @@ set -e -version="0.3.0" +version="0.3.1" package="timeconverter" bin="timeconverter" repo="https://github.com/bbeardsley/timeconverter"