本站源代码
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

154 linhas
3.4KB

  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package strfmt
  15. import (
  16. "database/sql/driver"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "time"
  21. "go.mongodb.org/mongo-driver/bson"
  22. )
  23. func init() {
  24. d := Date{}
  25. // register this format in the default registry
  26. Default.Add("date", &d, IsDate)
  27. }
  28. // IsDate returns true when the string is a valid date
  29. func IsDate(str string) bool {
  30. _, err := time.Parse(RFC3339FullDate, str)
  31. return err == nil
  32. }
  33. const (
  34. // RFC3339FullDate represents a full-date as specified by RFC3339
  35. // See: http://goo.gl/xXOvVd
  36. RFC3339FullDate = "2006-01-02"
  37. )
  38. // Date represents a date from the API
  39. //
  40. // swagger:strfmt date
  41. type Date time.Time
  42. // String converts this date into a string
  43. func (d Date) String() string {
  44. return time.Time(d).Format(RFC3339FullDate)
  45. }
  46. // UnmarshalText parses a text representation into a date type
  47. func (d *Date) UnmarshalText(text []byte) error {
  48. if len(text) == 0 {
  49. return nil
  50. }
  51. dd, err := time.Parse(RFC3339FullDate, string(text))
  52. if err != nil {
  53. return err
  54. }
  55. *d = Date(dd)
  56. return nil
  57. }
  58. // MarshalText serializes this date type to string
  59. func (d Date) MarshalText() ([]byte, error) {
  60. return []byte(d.String()), nil
  61. }
  62. // Scan scans a Date value from database driver type.
  63. func (d *Date) Scan(raw interface{}) error {
  64. switch v := raw.(type) {
  65. case []byte:
  66. return d.UnmarshalText(v)
  67. case string:
  68. return d.UnmarshalText([]byte(v))
  69. case time.Time:
  70. *d = Date(v)
  71. return nil
  72. case nil:
  73. *d = Date{}
  74. return nil
  75. default:
  76. return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)
  77. }
  78. }
  79. // Value converts Date to a primitive value ready to written to a database.
  80. func (d Date) Value() (driver.Value, error) {
  81. return driver.Value(d.String()), nil
  82. }
  83. // MarshalJSON returns the Date as JSON
  84. func (d Date) MarshalJSON() ([]byte, error) {
  85. return json.Marshal(time.Time(d).Format(RFC3339FullDate))
  86. }
  87. // UnmarshalJSON sets the Date from JSON
  88. func (d *Date) UnmarshalJSON(data []byte) error {
  89. if string(data) == jsonNull {
  90. return nil
  91. }
  92. var strdate string
  93. if err := json.Unmarshal(data, &strdate); err != nil {
  94. return err
  95. }
  96. tt, err := time.Parse(RFC3339FullDate, strdate)
  97. if err != nil {
  98. return err
  99. }
  100. *d = Date(tt)
  101. return nil
  102. }
  103. func (d Date) MarshalBSON() ([]byte, error) {
  104. return bson.Marshal(bson.M{"data": d.String()})
  105. }
  106. func (d *Date) UnmarshalBSON(data []byte) error {
  107. var m bson.M
  108. if err := bson.Unmarshal(data, &m); err != nil {
  109. return err
  110. }
  111. if data, ok := m["data"].(string); ok {
  112. rd, err := time.Parse(RFC3339FullDate, data)
  113. if err != nil {
  114. return err
  115. }
  116. *d = Date(rd)
  117. return nil
  118. }
  119. return errors.New("couldn't unmarshal bson bytes value as Date")
  120. }
  121. // DeepCopyInto copies the receiver and writes its value into out.
  122. func (d *Date) DeepCopyInto(out *Date) {
  123. *out = *d
  124. }
  125. // DeepCopy copies the receiver into a new Date.
  126. func (d *Date) DeepCopy() *Date {
  127. if d == nil {
  128. return nil
  129. }
  130. out := new(Date)
  131. d.DeepCopyInto(out)
  132. return out
  133. }
上海开阖软件有限公司 沪ICP备12045867号-1