Skip to content

Commit

Permalink
Switch to dep
Browse files Browse the repository at this point in the history
bring godate internal to add more formats for ledger
  • Loading branch information
howeyc committed Dec 7, 2017
1 parent 6fbfe8f commit 2ac74c1
Show file tree
Hide file tree
Showing 135 changed files with 15,164 additions and 23 deletions.
63 changes: 63 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions Gopkg.toml
@@ -0,0 +1,58 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/BurntSushi/toml"
version = "0.3.0"

[[constraint]]
name = "github.com/go-martini/martini"
version = "1.0.0"

[[constraint]]
name = "github.com/jbrukh/bayesian"
version = "1.0.0"

[[constraint]]
branch = "master"
name = "github.com/joyt/godate"

[[constraint]]
branch = "master"
name = "github.com/juztin/numeronym"

[[constraint]]
branch = "master"
name = "github.com/lucasb-eyer/go-colorful"

[[constraint]]
branch = "master"
name = "github.com/marcmak/calc"

[[constraint]]
branch = "master"
name = "github.com/martini-contrib/gzip"

[[constraint]]
branch = "master"
name = "github.com/martini-contrib/staticbin"
File renamed without changes.
69 changes: 69 additions & 0 deletions internal/github.com/joyt/godate/README.md
@@ -0,0 +1,69 @@
# godate
Golang package for intelligently parsing date strings like javascript's Date.parse() and getting the layout of date strings.
Fully compatible with the native time package.

This package is still under development.

# Usage
### Installation
```
go get github.com/joyt/godate
```

In your program:
```go
var timestamp time.Time
var err error
timestamp, err = date.Parse("Mar 14 2003")
```

## Example
```go
import (
"github.com/joyt/godate"
"time"
"fmt"
)

func main() {
var t time.Time

t = date.MustParse("Aug 31 1999")
fmt.Println(t)
// Prints 1999-08-31 00:00:00 +0000 UTC

t = date.MustParse("Tuesday, August 31, 1999")
fmt.Println(t)
// Prints 1999-08-31 00:00:00 +0000 UTC

t = date.MustParse("Tue 31 Aug '99")
fmt.Println(t)
// Prints 1999-08-31 00:00:00 +0000 UTC

t = date.MustParse("08/31/1999")
fmt.Println(t)
// Prints 1999-08-31 00:00:00 +0000 UTC

t = date.MustParse("8/31/1999 20:05")
fmt.Println(t)
// Prints 1999-08-31 21:05:00 +0000 UTC

t = date.MustParse("31/08/1999 8:05pm")
fmt.Println(t)
// Prints 1999-08-31 21:05:00 +0000 UTC

t = date.MustParse("8/31/1999 8:05PM EST")
fmt.Println(t)
// Prints 1999-08-31 21:05:00 -0400 EDT

t = date.MustParse("Aug-1999")
fmt.Println(t)
// Prints 1999-08-01 00:00:00 +0000 UTC
}
```

# Notes

The parser is extremely lenient, and will try to interpret whatever it is given as a date as much as possible.

In cases where the meaning of the date is ambiguous (such as 6/09, which could mean Jun 9th or Jun 2009), the parser generally defaults to the higher resolution date (Jun 9th). An exception is made for dates without separators such as "200609", where the parser will always try to assume the year is first (200609 -> Sep 2006, NOT Jun 20th 2009).
Expand Up @@ -69,8 +69,8 @@ var (
daysOfWeek = []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}

standardDateFormats = []string{
"2006-01-02",
"2006/01/02",
"2006-01-02",
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
Expand Down
167 changes: 167 additions & 0 deletions internal/github.com/joyt/godate/godate_test.go
@@ -0,0 +1,167 @@
package date

import (
"testing"
"time"
)

func TestDates(t *testing.T) {
// Invalid dates.
tests := []string{
"",
"HI",
"20938258384738",
"23.44",
}
for _, date := range tests {
if d, err := Parse(date); err == nil {
t.Errorf("Input %q was parsed to a date %v", date, d)
}
}

// Full date, no ambiguity.
correct := time.Date(1989, 6, 25, 0, 0, 0, 0, time.UTC)
tests = []string{
"25 Jun 1989",
"Jun 25 1989",
"Jun 25 '89",
"June 25, 1989",
"June 25, '89",
"25 Jun 1989",
"25 Jun '89",

"6/25/89",
"06/25/89",
"6/25/1989",
"06/25/1989",
"25/6/89",
"25/6/1989",
"25/06/89",
"25/06/1989",

"6-25-89",
"06-25-89",
"6-25-1989",
"06-25-1989",
"25-6-89",
"25-6-1989",
"25-06-89",
"25-06-1989",

"19890625",
"890625",
}

for _, date := range tests {
d, err := Parse(date)
if err != nil {
t.Errorf("Error prasing %q: %v", date, err)
} else if d != correct {
t.Errorf("Dates for %q did not match:\n%v (actual)\n%v (expected)", date, d, correct)
}
}

// Just month and year
correct = time.Date(1989, 6, 1, 0, 0, 0, 0, time.UTC)
tests = []string{
"Jun 1989",
"Jun-1989",
"Jun '89",
"June 1989",
"June '89",
"6/89",
"06/89",
"6-89",
"06-89",
"198906",
"8906",
"0689",
}

for _, date := range tests {
d, err := Parse(date)
if err != nil {
t.Errorf("Error prasing %s: %v", date, err)
} else if d != correct {
t.Errorf("Dates for %q did not match:\n%v (actual)\n%v (expected)", date, d, correct)
}
}

// Just month and day
correct = time.Date(0, 6, 25, 0, 0, 0, 0, time.UTC)
tests = []string{
"Jun 25",
"Jun-25",
"June 25",
"6/25",
"06/25",
"6-25",
}

for _, date := range tests {
d, err := Parse(date)
if err != nil {
t.Errorf("Error prasing %q: %v", date, err)
} else if d != correct {
t.Errorf("Dates for %q did not match:\n%v (actual)\n%v (expected)", date, d, correct)
}
}

// Just month and year, ambiguous
correct = time.Date(2009, 6, 1, 0, 0, 0, 0, time.UTC)
tests = []string{
"Jun 2009",
"Jun-2009",
"June 2009",
"200906",
}

for _, date := range tests {
d, err := Parse(date)
if err != nil {
t.Errorf("Error prasing %q: %v", date, err)
} else if d != correct {
t.Errorf("Dates for %q did not match:\n%v (actual)\n%v (expected)", date, d, correct)
}
}

// Full date with time at minute resolution.
correct = time.Date(1989, 6, 25, 15, 45, 0, 0, time.UTC)
tests = []string{
"Jun 25 1989 15:45",
"Jun 25 1989 3:45PM",
"Jun 25 1989 03:45PM",
"6/25/1989 15:45",
"6/25/1989 03:45PM",
"6/25/1989 3:45PM",
}

for _, date := range tests {
d, err := Parse(date)
if err != nil {
t.Errorf("Error prasing %q: %v", date, err)
} else if d != correct {
t.Errorf("Dates for %q did not match:\n%v (actual)\n%v (expected)", date, d, correct)
}
}

// TODO: Make more test cases for time of day, timezones.
}

func TestConvert(t *testing.T) {
// TODO: Make test cases for time of day, timezones.
tests := map[string]string{
"Jan 02 2006": "MMM dd yyyy",
"January 02, 2006": "MMMM dd, yyyy",
"01/02/2006": "MM/dd/yyyy",
"_2/1/06": "d/M/yy",
"01-2006": "MM-yyyy",
"Jan-2006": "MMM-yyyy",
"Mon 02 Jan '06": "EEE dd MMM 'yy",
}
for f, correct := range tests {
if uf := ConvertGoLayoutToUnicode(f); uf != correct {
t.Errorf("Failed to convert %q to %q, got %q instead", f, correct, uf)
}
}
}
3 changes: 2 additions & 1 deletion parse.go
Expand Up @@ -9,7 +9,8 @@ import (
"sort"
"strings"

"github.com/joyt/godate"
"github.com/howeyc/ledger/internal/github.com/joyt/godate"

"github.com/marcmak/calc/calc"
)

Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/BurntSushi/toml/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ac74c1

Please sign in to comment.