Skip to content

Commit c94add0

Browse files
ref(pkg/time): move pkg/time to sdk to avoid depending on the heavy deis/pkg repo (#8)
1 parent 04f745f commit c94add0

9 files changed

Lines changed: 107 additions & 15 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DEV_ENV_PREFIX_CGO_ENABLED := docker run --rm -e GO15VENDOREXPERIMENT=1 -e CGO_E
1010
DEV_ENV_CMD := ${DEV_ENV_PREFIX} ${DEV_ENV_IMAGE}
1111

1212
GO_FILES = $(wildcard *.go)
13-
GO_PACKAGES = api apps auth builds certs config domains keys perms ps releases users
13+
GO_PACKAGES = api apps auth builds certs config domains keys perms ps releases users pkg/time
1414
GO_PACKAGES_REPO_PATH = $(addprefix $(repo_path)/,$(GO_PACKAGES))
1515
GOFMT = gofmt -e -l -s
1616
GOTEST = go test --cover --race -v

api/certs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package api
22

3-
import "github.com/deis/pkg/time"
3+
import "github.com/deis/controller-sdk-go/pkg/time"
44

55
// Cert is the definition of the cert object.
66
// Some fields are omtempty because they are only

api/ps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package api
22

3-
import "github.com/deis/pkg/time"
3+
import "github.com/deis/controller-sdk-go/pkg/time"
44

55
// Pods defines the structure of a process.
66
type Pods struct {

certs/certs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
client "github.com/deis/controller-sdk-go"
1212
"github.com/deis/controller-sdk-go/api"
13-
"github.com/deis/pkg/time"
13+
"github.com/deis/controller-sdk-go/pkg/time"
1414
)
1515

1616
const certsFixture string = `

glide.lock

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
package: github.com/deis/controller-sdk-go
22
import:
3-
- package: github.com/deis/pkg
4-
subpackages:
5-
- time
63
- package: github.com/goware/urlx

pkg/time/time.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package time
2+
3+
import "time"
4+
5+
// DeisDatetimeFormat is the standard date/time representation used in Deis.
6+
const DeisDatetimeFormat = "2006-01-02T15:04:05MST"
7+
8+
// Different format to deal with the pyopenssl formatting
9+
// http://www.pyopenssl.org/en/stable/api/crypto.html#OpenSSL.crypto.X509.get_notAfter
10+
const PyOpenSSLTimeDateTimeFormat = "2006-01-02T15:04:05"
11+
12+
// Time represents the standard datetime format used across the Deis Platform.
13+
type Time struct {
14+
*time.Time
15+
}
16+
17+
func (t *Time) format() string {
18+
return t.Format(DeisDatetimeFormat)
19+
}
20+
21+
// MarshalJSON implements the json.Marshaler interface.
22+
// The time is a quoted string in Deis' datetime format.
23+
func (t *Time) MarshalJSON() ([]byte, error) {
24+
return []byte(t.Format(`"` + DeisDatetimeFormat + `"`)), nil
25+
}
26+
27+
// UnmarshalText implements the encoding.TextUnmarshaler interface.
28+
// The time is expected to be in Deis' datetime format.
29+
func (t *Time) UnmarshalText(data []byte) error {
30+
tt, err := time.Parse(time.RFC3339, string(data))
31+
if _, ok := err.(*time.ParseError); ok {
32+
tt, err = time.Parse(DeisDatetimeFormat, string(data))
33+
if _, ok := err.(*time.ParseError); ok {
34+
tt, err = time.Parse(PyOpenSSLTimeDateTimeFormat, string(data))
35+
}
36+
}
37+
*t = Time{&tt}
38+
return err
39+
}
40+
41+
// UnmarshalJSON implements the json.Unmarshaler interface.
42+
// The time is expected to be a quoted string in Deis' datetime format.
43+
func (t *Time) UnmarshalJSON(data []byte) error {
44+
// Fractional seconds are handled implicitly by Parse.
45+
tt, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
46+
if _, ok := err.(*time.ParseError); ok {
47+
tt, err = time.Parse(`"`+DeisDatetimeFormat+`"`, string(data))
48+
if _, ok := err.(*time.ParseError); ok {
49+
tt, err = time.Parse(`"`+PyOpenSSLTimeDateTimeFormat+`"`, string(data))
50+
}
51+
}
52+
*t = Time{&tt}
53+
return err
54+
}

pkg/time/time_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package time
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestUnMarshalText(t *testing.T) {
9+
dummyTime := Time{}
10+
11+
standardTimeFormats := []string{
12+
"2006-01-02T15:04:05MST",
13+
"2006-01-02T15:04:05UTC",
14+
"2006-01-02T15:04:05PST",
15+
"2006-01-02T15:04:05Z",
16+
}
17+
for _, goodTime := range standardTimeFormats {
18+
if dummyTime.UnmarshalText([]byte(goodTime)) != nil {
19+
t.Error("expected " + goodTime + " to be marshal-able.")
20+
}
21+
if dummyTime.Year() != 2006 {
22+
t.Error(fmt.Sprintf("expected year to be 2006; got %d.", dummyTime.Year()))
23+
}
24+
}
25+
26+
alternateTimeFormats := []string{
27+
"2007-01-02T15:04:05",
28+
"2007-01-02T15:04:05",
29+
"2007-01-02T15:04:05",
30+
}
31+
32+
for _, goodTime := range alternateTimeFormats {
33+
if dummyTime.UnmarshalText([]byte(goodTime)) != nil {
34+
t.Error("expected " + goodTime + " to be marshal-able.")
35+
}
36+
if dummyTime.Year() != 2007 {
37+
t.Error(fmt.Sprintf("expected year to be 2007; got %d.", dummyTime.Year()))
38+
}
39+
}
40+
41+
badTime := "this is a bad time, isn't it?"
42+
if dummyTime.UnmarshalText([]byte(badTime)) == nil {
43+
t.Error("expected " + badTime + "to be unmarshal-able.")
44+
}
45+
}

ps/ps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
client "github.com/deis/controller-sdk-go"
1212
"github.com/deis/controller-sdk-go/api"
13-
"github.com/deis/pkg/time"
13+
"github.com/deis/controller-sdk-go/pkg/time"
1414
)
1515

1616
const processesFixture string = `

0 commit comments

Comments
 (0)