2015-04-14 04:36:12 +00:00
|
|
|
package timeutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2015-05-13 16:27:00 +00:00
|
|
|
"strings"
|
2015-04-14 04:36:12 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTimestamp tries to parse given string as RFC3339 time
|
2015-05-13 16:27:00 +00:00
|
|
|
// or Unix timestamp (with seconds precision), if successful
|
|
|
|
//returns a Unix timestamp as string otherwise returns value back.
|
2015-04-14 04:36:12 +00:00
|
|
|
func GetTimestamp(value string) string {
|
2015-05-13 16:27:00 +00:00
|
|
|
var format string
|
|
|
|
if strings.Contains(value, ".") {
|
|
|
|
format = time.RFC3339Nano
|
|
|
|
} else {
|
|
|
|
format = time.RFC3339
|
|
|
|
}
|
|
|
|
|
2015-04-14 04:36:12 +00:00
|
|
|
loc := time.FixedZone(time.Now().Zone())
|
|
|
|
if len(value) < len(format) {
|
|
|
|
format = format[:len(value)]
|
|
|
|
}
|
|
|
|
t, err := time.ParseInLocation(format, value, loc)
|
|
|
|
if err != nil {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return strconv.FormatInt(t.Unix(), 10)
|
|
|
|
}
|