utils.go 519 B

12345678910111213141516171819202122
  1. package timeutils
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. // GetTimestamp tries to parse given string as RFC3339 time
  7. // or Unix timestamp, if successful returns a Unix timestamp
  8. // as string otherwise returns value back.
  9. func GetTimestamp(value string) string {
  10. format := RFC3339NanoFixed
  11. loc := time.FixedZone(time.Now().Zone())
  12. if len(value) < len(format) {
  13. format = format[:len(value)]
  14. }
  15. t, err := time.ParseInLocation(format, value, loc)
  16. if err != nil {
  17. return value
  18. }
  19. return strconv.FormatInt(t.Unix(), 10)
  20. }