Skip to content

Instantly share code, notes, and snippets.

@duglin
Last active August 29, 2015 14:21
Show Gist options
  • Save duglin/3bc009861e58f381dafe to your computer and use it in GitHub Desktop.
Save duglin/3bc009861e58f381dafe to your computer and use it in GitHub Desktop.
time
func GetTimestamp(value string) string {
format := RFC3339NanoFixed
loc := time.FixedZone(time.Now().Zone())
if len(value) < len(format) {
format = format[:len(value)]
}
var shift time.Duration
if i := strings.Index(value, "Z"); i > 0 && i != len(value)-1 {
parts := strings.SplitN(value[i+1:], ":", 2)
hr, err := strconv.Atoi(parts[0])
if err != nil {
return value
}
min, err := strconv.Atoi(parts[1])
if err != nil {
return value
}
shift = time.Duration(hr)*time.Hour + time.Duration(min)*time.Minute
// now remove Z... from value
value = value[:i+1]
}
var t time.Time
var err error
if shift != 0 {
t, err = time.Parse(format, value)
t = t.Add(shift)
} else {
t, err = time.ParseInLocation(format, value, loc)
}
if err != nil {
return value
}
return strconv.FormatInt(t.Unix(), 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment