本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
713B

  1. package testfixtures
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. var timeFormats = []string{
  7. "2006-01-02",
  8. "2006-01-02 15:04",
  9. "2006-01-02 15:04:05",
  10. "20060102",
  11. "20060102 15:04",
  12. "20060102 15:04:05",
  13. "02/01/2006",
  14. "02/01/2006 15:04",
  15. "02/01/2006 15:04:05",
  16. "2006-01-02T15:04-07:00",
  17. "2006-01-02T15:04:05-07:00",
  18. }
  19. // ErrCouldNotConvertToTime is returns when a string is not a reconizable time format
  20. var ErrCouldNotConvertToTime = errors.New("Could not convert string to time")
  21. func tryStrToDate(s string) (time.Time, error) {
  22. for _, f := range timeFormats {
  23. t, err := time.ParseInLocation(f, s, time.Local)
  24. if err != nil {
  25. continue
  26. }
  27. return t, nil
  28. }
  29. return time.Time{}, ErrCouldNotConvertToTime
  30. }
上海开阖软件有限公司 沪ICP备12045867号-1