diff --git a/dateparse/README.md b/dateparse/README.md index 2f8a1b1..8f2a982 100644 --- a/dateparse/README.md +++ b/dateparse/README.md @@ -10,36 +10,102 @@ Simple CLI to test out dateparse. # 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 | -+------------+---------------------+---------------+----------+-------------------------------+ + +Your Current time.Local zone is PDT + ++------------+---------------------+---------------------------+-------------------------------+-------------------------------+ +| method | Input | Zone Source | Parsed: %v | Parsed: t.In(time.UTC) | ++------------+---------------------+---------------------------+-------------------------------+-------------------------------+ +| ParseAny | 2017-07-19 03:21:00 | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | 2017-07-19 03:21:00 | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | 2017-07-19 03:21:00 | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | 2017-07-19 03:21:00 | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | 2017-07-19 03:21:00 | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseIn | 2017-07-19 03:21:00 | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:00 | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:00 | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:00 | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 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" -+------------+---------------------------+---------------+----------+---------------------------------+ -| 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 | -+------------+---------------------------+---------------+----------+---------------------------------+ + +Your Current time.Local zone is PDT + ++------------+---------------------------+---------------------------+---------------------------------+-------------------------------+ +| method | Input | Zone Source | Parsed: %v | Parsed: t.In(time.UTC) | ++------------+---------------------------+---------------------------+---------------------------------+-------------------------------+ +| ParseAny | 2017-07-19 03:21:51+00:00 | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseAny | 2017-07-19 03:21:51+00:00 | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | +| ParseAny | 2017-07-19 03:21:51+00:00 | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | 2017-07-19 03:21:51+00:00 | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | 2017-07-19 03:21:51+00:00 | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | 2017-07-19 03:21:51+00:00 | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:51+00:00 | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:51+00:00 | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | 2017-07-19 03:21:51+00:00 | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | ++------------+---------------------------+---------------------------+---------------------------------+-------------------------------+ + + +$ dateparse --timezone="America/Denver" "Monday, 19-Jul-17 03:21:00 MDT" + +Your Current time.Local zone is PDT + ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ +| method | Input | Zone Source | Parsed: %v | Parsed: t.In(time.UTC) | ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ +| ParseAny | Monday, 19-Jul-17 03:21:00 MDT | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MDT | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MDT | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MDT | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MDT | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MDT | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MDT | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MDT | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MDT | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ + + +# pass in a wrong timezone "MST" (should be MDT) +$ dateparse --timezone="America/Denver" "Monday, 19-Jul-17 03:21:00 MST" + +Your Current time.Local zone is PDT + ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ +| method | Input | Zone Source | Parsed: %v | Parsed: t.In(time.UTC) | ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ +| ParseAny | Monday, 19-Jul-17 03:21:00 MST | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MST | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MST | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MST | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MST | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MST | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MST | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MST | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MST | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ + + +# note, we are using America/New_York which doesn't recognize MDT so essentially ignores it +$ dateparse --timezone="America/New_York" "Monday, 19-Jul-17 03:21:00 MDT" + +Your Current time.Local zone is PDT + ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ +| method | Input | Zone Source | Parsed: %v | Parsed: t.In(time.UTC) | ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ +| ParseIn | Monday, 19-Jul-17 03:21:00 MDT | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MDT | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | Monday, 19-Jul-17 03:21:00 MDT | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MDT | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MDT | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | Monday, 19-Jul-17 03:21:00 MDT | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MDT | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MDT | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | Monday, 19-Jul-17 03:21:00 MDT | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | ++------------+--------------------------------+---------------------------+-------------------------------+-------------------------------+ + ``` \ No newline at end of file diff --git a/dateparse/main.go b/dateparse/main.go index 16da552..bdf24cf 100644 --- a/dateparse/main.go +++ b/dateparse/main.go @@ -41,9 +41,12 @@ func main() { loc = l } + zonename, _ := time.Now().In(time.Local).Zone() + fmt.Printf("\nYour Current time.Local zone is %v\n\n", zonename) + table := termtables.CreateTable() - table.AddHeaders("method", "Input", "Zone Source", "Timezone", "Parsed, and Output as %v") + table.AddHeaders("method", "Input", "Zone Source", "Parsed", "Parsed: t.In(time.UTC)") parsers := map[string]parser{ "ParseAny": parseAny, @@ -51,34 +54,38 @@ func main() { "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)) + table.AddRow(name, datestr, "time.Local = nil", parser(datestr, nil), parser(datestr, nil).In(time.UTC)) if timezone != "" { - table.AddRow(name, datestr, "timezone arg", zonename, parser(datestr, loc)) + time.Local = loc + table.AddRow(name, datestr, "time.Local = timezone arg", parser(datestr, loc), parser(datestr, loc).In(time.UTC)) } time.Local = time.UTC - table.AddRow(name, datestr, "UTC", "UTC", parser(datestr, time.UTC)) + table.AddRow(name, datestr, "time.Local = time.UTC", parser(datestr, time.UTC), parser(datestr, time.UTC).In(time.UTC)) } fmt.Println(table.Render()) } -type parser func(datestr string, loc *time.Location) string +func stuff() (string, string) { + return "more", "stuff" +} -func parseLocal(datestr string, loc *time.Location) string { +type parser func(datestr string, loc *time.Location) time.Time + +func parseLocal(datestr string, loc *time.Location) time.Time { time.Local = loc t, _ := dateparse.ParseLocal(datestr) - return t.String() + return t } -func parseIn(datestr string, loc *time.Location) string { +func parseIn(datestr string, loc *time.Location) time.Time { t, _ := dateparse.ParseIn(datestr, loc) - return t.String() + return t } -func parseAny(datestr string, loc *time.Location) string { +func parseAny(datestr string, loc *time.Location) time.Time { t, _ := dateparse.ParseAny(datestr) - return t.String() + return t }