Permalink
Browse files

Fix time parsing (#176)

* Add failing tests

* pad left side of time with zeros
  • Loading branch information...
kochman authored and garoller committed Nov 2, 2018
1 parent d61764b commit 134aa0d70d6baad875de0750d0db2a8279662102
Showing with 35 additions and 6 deletions.
  1. +8 −6 updater/updater.go
  2. +27 −0 updater/updater_test.go
@@ -311,12 +311,14 @@ func (u *Updater) GuessRouteForVehicle(vehicle *shuttletracker.Vehicle) (route *
}
func itrakTimeDate(itrakTime, itrakDate string) (time.Time, error) {
// Add one or two leading zeros to the time value if they're missing.
// time.Parse expects this.
if len(itrakTime) == 10 {
itrakTime = itrakTime[:5] + "0" + itrakTime[5:]
} else if len(itrakTime) == 9 {
itrakTime = itrakTime[:5] + "00" + itrakTime[5:]
// Add leading zeros to the time value if they're missing. time.Parse expects this.
if len(itrakTime) < 11 {
builder := itrakTime[:5]
for i := len(itrakTime); i < 11; i++ {
builder += "0"
}
builder += itrakTime[5:]
itrakTime = builder
}
combined := itrakDate + " " + itrakTime
@@ -32,4 +32,31 @@ func TestITrakTimeDate(t *testing.T) {
if !parsed.Equal(expected) {
t.Errorf("got %+v, expected %+v", parsed, expected)
}
parsed, err = itrakTimeDate("time:7", "date:10052018")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
expected = time.Date(2018, time.October, 05, 0, 0, 7, 0, time.UTC)
if !parsed.Equal(expected) {
t.Errorf("got %+v, expected %+v", parsed, expected)
}
parsed, err = itrakTimeDate("time:44", "date:10052018")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
expected = time.Date(2018, time.October, 05, 0, 0, 44, 0, time.UTC)
if !parsed.Equal(expected) {
t.Errorf("got %+v, expected %+v", parsed, expected)
}
parsed, err = itrakTimeDate("time:200", "date:10052018")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
expected = time.Date(2018, time.October, 05, 0, 2, 0, 0, time.UTC)
if !parsed.Equal(expected) {
t.Errorf("got %+v, expected %+v", parsed, expected)
}
}

0 comments on commit 134aa0d

Please sign in to comment.