strftime_parser.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package moment
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. type StrftimeParser struct{}
  7. var (
  8. replacements_pattern = regexp.MustCompile("%[mbhBedjwuaAVgyGYpPkHlIMSZzsTrRTDFXx]")
  9. )
  10. // Not implemented
  11. // U
  12. // C
  13. var strftime_replacements = map[string]string{
  14. "%m": "01", // stdZeroMonth 01 02 ... 11 12
  15. "%b": "Jan", // stdMonth Jan Feb ... Nov Dec
  16. "%h": "Jan",
  17. "%B": "January", // stdLongMonth January February ... November December
  18. "%e": "2", // stdDay 1 2 ... 30 30
  19. "%d": "02", // stdZeroDay 01 02 ... 30 31
  20. "%j": "<stdDayOfYear>", // Day of the year ***001 002 ... 364 365 @todo****
  21. "%w": "<stdDayOfWeek>", // Numeric representation of day of the week 0 1 ... 5 6
  22. "%u": "<stdDayOfWeekISO>", // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 2 ... 6 7 @todo
  23. "%a": "Mon", // Sun Mon ... Fri Sat
  24. "%A": "Monday", // stdLongWeekDay Sunday Monday ... Friday Saturday
  25. "%V": "<stdWeekOfYear>", // ***01 02 ... 52 53 @todo begin with zeros
  26. "%g": "06", // stdYear 70 71 ... 29 30
  27. "%y": "06",
  28. "%G": "2006", // stdLongYear 1970 1971 ... 2029 2030
  29. "%Y": "2006",
  30. "%p": "PM", // stdPM AM PM
  31. "%P": "pm", // stdpm am pm
  32. "%k": "15", // stdHour 0 1 ... 22 23
  33. "%H": "15", // 00 01 ... 22 23
  34. "%l": "3", // stdHour12 1 2 ... 11 12
  35. "%I": "03", // stdZeroHour12 01 02 ... 11 12
  36. "%M": "04", // stdZeroMinute 00 01 ... 58 59
  37. "%S": "05", // stdZeroSecond ***00 01 ... 58 59
  38. "%Z": "MST", //EST CST ... MST PST
  39. "%z": "-0700", // stdNumTZ -0700 -0600 ... +0600 +0700
  40. "%s": "<stdUnix>", // Seconds since unix epoch 1360013296
  41. "%r": "03:04:05 PM",
  42. "%R": "15:04",
  43. "%T": "15:04:05",
  44. "%D": "01/02/06",
  45. "%F": "2006-01-02",
  46. "%X": "15:04:05",
  47. "%x": "01/02/06",
  48. }
  49. func (p *StrftimeParser) Convert(layout string) string {
  50. var match [][]string
  51. if match = replacements_pattern.FindAllStringSubmatch(layout, -1); match == nil {
  52. return layout
  53. }
  54. for i := range match {
  55. if replace, ok := strftime_replacements[match[i][0]]; ok {
  56. layout = strings.Replace(layout, match[i][0], replace, 1)
  57. }
  58. }
  59. return layout
  60. }