Improve CLI tool for example usage

This commit is contained in:
Aaron Raddon 2017-07-26 18:15:47 -07:00
parent 882d5c810b
commit 82e967717c
3 changed files with 89 additions and 21 deletions

View File

@ -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

View File

@ -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 |
+------------+---------------------------+---------------+----------+---------------------------------+
```

View File

@ -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()
}