2019-09-03 17:34:21 +03:00
|
|
|
// For more tutorials: https://blog.learngoprogramming.com
|
|
|
|
//
|
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-09-27 19:09:07 +03:00
|
|
|
// timestamp stores, formats and automatically prints a timestamp: it's a stringer.
|
|
|
|
type timestamp struct {
|
|
|
|
time.Time
|
|
|
|
}
|
2019-09-03 17:34:21 +03:00
|
|
|
|
2019-09-27 19:09:07 +03:00
|
|
|
// String method makes the timestamp an fmt.stringer.
|
2019-09-03 17:34:21 +03:00
|
|
|
func (ts timestamp) String() string {
|
2019-09-27 19:09:07 +03:00
|
|
|
if ts.IsZero() {
|
2019-09-03 17:34:21 +03:00
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2019-09-27 19:09:07 +03:00
|
|
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
|
|
|
const layout = "2006/01"
|
|
|
|
return ts.Format(layout)
|
2019-09-03 17:34:21 +03:00
|
|
|
}
|
|
|
|
|
2019-10-17 14:11:51 +03:00
|
|
|
func toTimestamp(v interface{}) (ts timestamp) {
|
2019-09-03 17:34:21 +03:00
|
|
|
var t int
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case int:
|
|
|
|
t = v
|
|
|
|
case string:
|
|
|
|
t, _ = strconv.Atoi(v)
|
|
|
|
}
|
|
|
|
|
2019-10-17 14:11:51 +03:00
|
|
|
ts.Time = time.Unix(int64(t), 0)
|
|
|
|
return ts
|
2019-09-03 17:34:21 +03:00
|
|
|
}
|