Local_date.go 2.8 KB

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