/
metadata.go
59 lines (45 loc) · 1.26 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package template
import (
"fmt"
"time"
"github.com/docker/go-units"
)
// Metadata contains the information for a template.
type Metadata struct {
Tag string
Repository string
Created JSONTime
}
// String returns the string slice form of Metadata.
func (m Metadata) String() []string {
tDelta := time.Now().Sub(time.Time(m.Created))
return []string{m.Tag, m.Repository, units.HumanDuration(tDelta) + " ago"}
}
// JSONTime is time.Time with JSON marshaling and unmarshaling implementations.
type JSONTime time.Time
const (
// "Mon, 02 Jan 2006 15:04:05 -0700"
timeFormat = time.RFC1123Z
)
// NewTime returns a new JSONTime containing the current time.
func NewTime() JSONTime {
return JSONTime(time.Now())
}
// MarshalJSON marshals JSONTime to JSON.
func (t *JSONTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%s"`, time.Time(*t).Format(timeFormat))
return []byte(stamp), nil
}
// UnmarshalJSON unmarshals JSON to JSONTime.
func (t *JSONTime) UnmarshalJSON(b []byte) error {
time, err := time.Parse(timeFormat, string(b)[1:len(b)-1])
if err != nil {
return err
}
*t = JSONTime(time)
return nil
}
// String returns the string form of JSONTime.
func (t JSONTime) String() string {
return fmt.Sprintf("%s", time.Time(t).Format(timeFormat))
}