Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from paulmach/time-utc
Browse files Browse the repository at this point in the history
time marshals to iso8601 utc
  • Loading branch information
hesidoryn authored Jun 18, 2018
2 parents 4a9fc48 + 0f439da commit ac7afda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions osm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

const osmTimeFormat = "2006-01-02T15:04:05"
const osmTimeFormat = "2006-01-02T15:04:05Z"

// Tag represents osm tag
type Tag struct {
Expand All @@ -27,7 +27,7 @@ func (ts *Tags) Scan(value interface{}) error {
type TimeOSM time.Time

func (t *TimeOSM) String() string {
return time.Time(*t).Format(osmTimeFormat)
return time.Time(*t).UTC().Format(osmTimeFormat)
}

// Scan - Implement the database/sql scanner interface
Expand Down
39 changes: 39 additions & 0 deletions osm/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package osm

import (
"testing"
"time"
)

func TestTimeOSMString(t *testing.T) {
newYork, err := time.LoadLocation("America/New_York")
if err != nil {
t.Fatalf("invalid timezone: %v", err)
}

cases := []struct {
name string
time time.Time
expected string
}{
{
name: "iso 8601 format",
time: time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
expected: "2012-01-01T00:00:00Z",
},
{
name: "always UTC",
time: time.Date(2012, 1, 1, 0, 0, 0, 0, newYork),
expected: "2012-01-01T05:00:00Z",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tosm := TimeOSM(tc.time)
if v := tosm.String(); v != tc.expected {
t.Errorf("incorrect format: %v", v)
}
})
}
}

0 comments on commit ac7afda

Please sign in to comment.