strftime.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // COPIED FROM https://github.com/jehiah/go-strftime
  2. package main
  3. import (
  4. "time"
  5. )
  6. // taken from time/format.go
  7. var conversion = map[string]string{
  8. /*stdLongMonth */ "B": "January",
  9. /*stdMonth */ "b": "Jan",
  10. // stdNumMonth */ "m": "1",
  11. /*stdZeroMonth */ "m": "01",
  12. /*stdLongWeekDay */ "A": "Monday",
  13. /*stdWeekDay */ "a": "Mon",
  14. // stdDay */ "d": "2",
  15. // stdUnderDay */ "d": "_2",
  16. /*stdZeroDay */ "d": "02",
  17. /*stdHour */ "H": "15",
  18. // stdHour12 */ "I": "3",
  19. /*stdZeroHour12 */ "I": "03",
  20. // stdMinute */ "M": "4",
  21. /*stdZeroMinute */ "M": "04",
  22. // stdSecond */ "S": "5",
  23. /*stdZeroSecond */ "S": "05",
  24. /*stdLongYear */ "Y": "2006",
  25. /*stdYear */ "y": "06",
  26. /*stdPM */ "p": "PM",
  27. // stdpm */ "p": "pm",
  28. /*stdTZ */ "Z": "MST",
  29. // stdISO8601TZ */ "z": "Z0700", // prints Z for UTC
  30. // stdISO8601ColonTZ */ "z": "Z07:00", // prints Z for UTC
  31. /*stdNumTZ */ "z": "-0700", // always numeric
  32. // stdNumShortTZ */ "b": "-07", // always numeric
  33. // stdNumColonTZ */ "b": "-07:00", // always numeric
  34. "%": "%",
  35. }
  36. // This is an alternative to time.Format because no one knows
  37. // what date 040305 is supposed to create when used as a 'layout' string
  38. // this takes standard strftime format options. For a complete list
  39. // of format options see http://strftime.org/
  40. func strftime(format string, t time.Time) string {
  41. layout := ""
  42. length := len(format)
  43. for i := 0; i < length; i++ {
  44. if format[i] == '%' && i <= length-2 {
  45. if layoutCmd, ok := conversion[format[i+1:i+2]]; ok {
  46. layout = layout + layoutCmd
  47. i++
  48. continue
  49. }
  50. }
  51. layout = layout + format[i:i+1]
  52. }
  53. return t.Format(layout)
  54. }