Local_date.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2015 qianqiusoft.com
  2. // Licensed to You under the GNU Affero GPL v3
  3. // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE
  4. // http://www.gnu.org/licenses/why-affero-gpl.en.html
  5. package models
  6. import (
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/astaxie/beego/orm"
  11. )
  12. // datetime values like 2006-01-02 15:04:05
  13. type LocalDate time.Time
  14. const formatDate = "2006-01-02"
  15. const nilDateStr = "0001-01-01"
  16. func CurrentDate() LocalDate {
  17. now := time.Now()
  18. nowStr := now.Format(formatDateTime)
  19. nowStr = strings.TrimSpace(nowStr)
  20. if len(nowStr) > 10 {
  21. nowStr = nowStr[0:10]
  22. }
  23. now, _ = timeParse(nowStr, formatDate) //time.ParseInLocation(formatDate, nowStr, time.Local)
  24. return LocalDate(now)
  25. }
  26. func nilDate() LocalDate {
  27. nilDate, _ := timeParse(nilDateStr, formatDate) //time.ParseInLocation(formatDate, nilDateStr, time.Local)
  28. return LocalDate(nilDate)
  29. }
  30. func isNilDate(tval time.Time) bool {
  31. return tval.Format(formatDate) == nilDateStr
  32. }
  33. // Value return the time.Time
  34. func (e LocalDate) IsNilDate() bool {
  35. return isNilDate(e.Value())
  36. }
  37. // Value return the time.Time
  38. func (e LocalDate) Value() time.Time {
  39. return time.Time(e)
  40. }
  41. // Set set the LocalDate's value
  42. func (e *LocalDate) Set(d time.Time) {
  43. *e = LocalDate(d)
  44. }
  45. // String convert datatime to string
  46. func (e *LocalDate) String() string {
  47. return e.Value().String()
  48. }
  49. // FieldType return enum type Date
  50. func (e *LocalDate) FieldType() int {
  51. return orm.TypeDateField
  52. }
  53. // SetRaw convert the interface to time.Time. Allow string and time.Time
  54. func (e *LocalDate) SetRaw(value interface{}) error {
  55. if value == nil {
  56. *e = nilDate()
  57. return nil
  58. }
  59. switch d := value.(type) {
  60. case time.Time:
  61. if isNilDate(d) {
  62. *e = nilDate()
  63. } else {
  64. e.Set(d)
  65. }
  66. case string:
  67. v, err := timeParse(d, formatDate)
  68. if err == nil {
  69. e.Set(v)
  70. }
  71. return err
  72. default:
  73. return fmt.Errorf("<LocalDate.SetRaw> unknown value `%s`", value)
  74. }
  75. return nil
  76. }
  77. // RawValue return Date value
  78. func (e *LocalDate) RawValue() interface{} {
  79. return e.Value()
  80. }
  81. func (t *LocalDate) UnmarshalJSON(data []byte) (err error) {
  82. if data[0] == '"' && data[len(data)-1] == '"' {
  83. data = data[1 : len(data)-1]
  84. }
  85. dataStr := strings.TrimSpace(string(data))
  86. if dataStr == "" {
  87. *t = nilDate()
  88. return nil
  89. }
  90. if len(dataStr) > 10 {
  91. if dataStr == "0000-00-00 00:00:00" {
  92. dataStr = nilDateStr
  93. } else {
  94. dataStr = dataStr[0:10]
  95. }
  96. }
  97. now, err := timeParse(dataStr, formatDate) // time.ParseInLocation(formatDate, dataStr, time.Local)
  98. *t = LocalDate(now)
  99. return
  100. }
  101. func (t LocalDate) MarshalJSON() ([]byte, error) {
  102. if isNilDate(t.Value()) {
  103. return []byte{'"', '"'}, nil
  104. }
  105. b := make([]byte, 0, len(formatDate)+2)
  106. b = append(b, '"')
  107. b = append(b, []byte(time.Time(t).Format(formatDate))...)
  108. b = append(b, '"')
  109. return b, nil
  110. }
  111. var _ orm.Fielder = new(LocalDate)