From 82e967717c804e7dae48cd75357b99a3d5c6c2c1 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Wed, 26 Jul 2017 18:15:47 -0700 Subject: [PATCH] Improve CLI tool for example usage --- README.md | 6 +++++ dateparse/README.md | 42 +++++++++++++++++++++++++----- dateparse/main.go | 62 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 89 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1902a4f..3ed20d4 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,12 @@ t, err := dateparse.ParseLocal("3/1/2014") ``` +cli tool for testing dateformats +---------------------------------- + +[Date Parse CLI](https://github.com/araddon/dateparse/blob/master/dateparse) + + Extended example https://github.com/araddon/dateparse/blob/master/example/main.go ```go package main diff --git a/dateparse/README.md b/dateparse/README.md index c138900..2f8a1b1 100644 --- a/dateparse/README.md +++ b/dateparse/README.md @@ -1,17 +1,45 @@ +DateParse CLI +---------------------- + Simple CLI to test out dateparse. ```sh +# Since this date string has no timezone/offset so is more effected by +# which method you use to parse + +$ dateparse --timezone="America/Denver" "2017-07-19 03:21:00" ++------------+---------------------+---------------+----------+-------------------------------+ +| method | Input | Zone Source | Timezone | Parsed, and Output as %v | ++------------+---------------------+---------------+----------+-------------------------------+ +| ParseAny | 2017-07-19 03:21:00 | Local Default | PDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | 2017-07-19 03:21:00 | timezone arg | PDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | 2017-07-19 03:21:00 | UTC | UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | 2017-07-19 03:21:00 | Local Default | PDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | 2017-07-19 03:21:00 | timezone arg | PDT | 2017-07-19 03:21:00 -0600 MDT | +| ParseIn | 2017-07-19 03:21:00 | UTC | UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:00 | Local Default | PDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:00 | timezone arg | PDT | 2017-07-19 03:21:00 -0600 MDT | +| ParseLocal | 2017-07-19 03:21:00 | UTC | UTC | 2017-07-19 03:21:00 +0000 UTC | ++------------+---------------------+---------------+----------+-------------------------------+ + +# Note on this one that the outputed zone is always UTC/0 offset as opposed to above $ dateparse --timezone="America/Denver" "2017-07-19 03:21:51+00:00" -+---------------------------+---------------+----------------+---------------------------------+ -| Input | Zone Source | Timezone | Parsed, and Output as %v | -+---------------------------+---------------+----------------+---------------------------------+ -| 2017-07-19 03:21:51+00:00 | Local Default | PDT | 2017-07-19 03:21:51 +0000 +0000 | -| 2017-07-19 03:21:51+00:00 | timezone arg | America/Denver | 2017-07-19 03:21:51 +0000 +0000 | -| 2017-07-19 03:21:51+00:00 | UTC | UTC | 2017-07-19 03:21:51 +0000 UTC | -+---------------------------+---------------+----------------+---------------------------------+ ++------------+---------------------------+---------------+----------+---------------------------------+ +| method | Input | Zone Source | Timezone | Parsed, and Output as %v | ++------------+---------------------------+---------------+----------+---------------------------------+ +| ParseAny | 2017-07-19 03:21:51+00:00 | Local Default | PDT | 2017-07-19 03:21:51 +0000 UTC | +| ParseAny | 2017-07-19 03:21:51+00:00 | timezone arg | PDT | 2017-07-19 03:21:51 +0000 UTC | +| ParseAny | 2017-07-19 03:21:51+00:00 | UTC | UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | 2017-07-19 03:21:51+00:00 | Local Default | PDT | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | 2017-07-19 03:21:51+00:00 | timezone arg | PDT | 2017-07-19 03:21:51 +0000 +0000 | +| ParseIn | 2017-07-19 03:21:51+00:00 | UTC | UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:51+00:00 | Local Default | PDT | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:51+00:00 | timezone arg | PDT | 2017-07-19 03:21:51 +0000 +0000 | +| ParseLocal | 2017-07-19 03:21:51+00:00 | UTC | UTC | 2017-07-19 03:21:51 +0000 UTC | ++------------+---------------------------+---------------+----------+---------------------------------+ ``` \ No newline at end of file diff --git a/dateparse/main.go b/dateparse/main.go index ac704f4..16da552 100644 --- a/dateparse/main.go +++ b/dateparse/main.go @@ -19,32 +19,66 @@ func main() { flag.Parse() if len(flag.Args()) == 0 { - fmt.Println(`Must pass ./dateparse "2009-08-12T22:15:09.99Z"`) + fmt.Println(`Must pass a time, and optional location: + + ./dateparse "2009-08-12T22:15:09.99Z" + + ./dateparse --timezone="America/Denver" "2017-07-19 03:21:51+00:00" + `) return } + datestr = flag.Args()[0] - table := termtables.CreateTable() - - table.AddHeaders("Input", "Zone Source", "Timezone", "Parsed, and Output as %v") - - zonename, _ := time.Now().In(time.Local).Zone() - - table.AddRow(datestr, "Local Default", fmt.Sprintf("%v", zonename), fmt.Sprintf("%v", dateparse.MustParse(datestr))) - + var loc *time.Location if timezone != "" { // NOTE: This is very, very important to understand // time-parsing in go - loc, err := time.LoadLocation(timezone) + l, err := time.LoadLocation(timezone) if err != nil { panic(err.Error()) } - time.Local = loc - table.AddRow(datestr, "timezone arg", fmt.Sprintf("%v", timezone), fmt.Sprintf("%v", dateparse.MustParse(datestr))) + loc = l } - time.Local = time.UTC - table.AddRow(datestr, "UTC", "UTC", fmt.Sprintf("%v", dateparse.MustParse(datestr))) + table := termtables.CreateTable() + + table.AddHeaders("method", "Input", "Zone Source", "Timezone", "Parsed, and Output as %v") + + parsers := map[string]parser{ + "ParseAny": parseAny, + "ParseIn": parseIn, + "ParseLocal": parseLocal, + } + + zonename, _ := time.Now().In(time.Local).Zone() + for name, parser := range parsers { + time.Local = nil + table.AddRow(name, datestr, "Local Default", zonename, parser(datestr, nil)) + if timezone != "" { + table.AddRow(name, datestr, "timezone arg", zonename, parser(datestr, loc)) + } + time.Local = time.UTC + table.AddRow(name, datestr, "UTC", "UTC", parser(datestr, time.UTC)) + } fmt.Println(table.Render()) } + +type parser func(datestr string, loc *time.Location) string + +func parseLocal(datestr string, loc *time.Location) string { + time.Local = loc + t, _ := dateparse.ParseLocal(datestr) + return t.String() +} + +func parseIn(datestr string, loc *time.Location) string { + t, _ := dateparse.ParseIn(datestr, loc) + return t.String() +} + +func parseAny(datestr string, loc *time.Location) string { + t, _ := dateparse.ParseAny(datestr) + return t.String() +}