month.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package tfelem
  2. import (
  3. "git.swzry.com/zry/YAGTF/yagtf/utils"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. type MonthElement struct {
  9. UseMonthName bool
  10. Fill bool
  11. UseAbbr bool
  12. }
  13. func (this *MonthElement) ExpectedSize() int {
  14. if this.UseMonthName {
  15. if this.UseAbbr {
  16. return 3
  17. } else {
  18. return 9
  19. }
  20. } else {
  21. return 2
  22. }
  23. }
  24. func (this *MonthElement) PrintElement(t time.Time) string {
  25. m := t.Month()
  26. if this.UseMonthName {
  27. s := m.String()
  28. if this.UseAbbr {
  29. return s[:3]
  30. } else {
  31. if this.Fill {
  32. return s + strings.Repeat(" ", 9-len(s))
  33. } else {
  34. return s
  35. }
  36. }
  37. } else {
  38. mint := int(m)
  39. if this.Fill {
  40. return utils.GetFilledNumberWithTruncate(mint, 2)
  41. } else {
  42. return strconv.Itoa(mint)
  43. }
  44. }
  45. }
  46. func NewNumbericMonthElement(fill bool) *MonthElement {
  47. return &MonthElement{
  48. UseMonthName: false,
  49. Fill: fill,
  50. }
  51. }
  52. func NewEnglishMonthElement(abbrevation bool, fill bool) *MonthElement {
  53. return &MonthElement{
  54. UseMonthName: true,
  55. UseAbbr: abbrevation,
  56. Fill: fill,
  57. }
  58. }