Mercurial > gemma
diff pkg/common/attributes.go @ 2059:ae0021feaac8 unify_imports
Imports: Made stretch import marshable (even if it is not needed).
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 29 Jan 2019 08:28:50 +0100 |
parents | 7d627258e045 |
children | 2c67c51d57ad |
line wrap: on
line diff
--- a/pkg/common/attributes.go Mon Jan 28 20:35:14 2019 +0100 +++ b/pkg/common/attributes.go Tue Jan 29 08:28:50 2019 +0100 @@ -20,6 +20,11 @@ "time" ) +const ( + TimeFormat = "2006-01-02T15:04:05" + DateFormat = "2006-01-02" +) + type ( // Attributes is a map of optional key/value attributes @@ -103,7 +108,23 @@ return ca.Set(key, v) } -const TimeFormat = "2006-01-02T15:04:05" +func (ca Attributes) Date(key string) (time.Time, bool) { + s, found := ca.Get(key) + if !found { + return time.Time{}, false + } + d, err := time.Parse(DateFormat, s) + if err != nil { + log.Printf("error: %v\n", err) + return time.Time{}, false + } + return d, true +} + +func (ca Attributes) SetDate(key string, date time.Time) bool { + s := date.Format(DateFormat) + return ca.Set(key, s) +} // Time gives a time.Time for a given key. func (ca Attributes) Time(key string) (time.Time, bool) {