/vendor/github.com/go-openapi/strfmt/date.go

https://github.com/alibaba/pouch · Go · 152 lines · 105 code · 22 blank · 25 comment · 18 complexity · 4391bed4cc989800f6ed96b74c60f0ff MD5 · raw file

  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. "errors"
  18. "fmt"
  19. "regexp"
  20. "time"
  21. "gopkg.in/mgo.v2/bson"
  22. "github.com/mailru/easyjson/jlexer"
  23. "github.com/mailru/easyjson/jwriter"
  24. )
  25. func init() {
  26. d := Date{}
  27. Default.Add("date", &d, IsDate)
  28. }
  29. // IsDate returns true when the string is a valid date
  30. func IsDate(str string) bool {
  31. matches := rxDate.FindAllStringSubmatch(str, -1)
  32. if len(matches) == 0 || len(matches[0]) == 0 {
  33. return false
  34. }
  35. m := matches[0]
  36. return !(m[2] < "01" || m[2] > "12" || m[3] < "01" || m[3] > "31")
  37. }
  38. const (
  39. // RFC3339FullDate represents a full-date as specified by RFC3339
  40. // See: http://goo.gl/xXOvVd
  41. RFC3339FullDate = "2006-01-02"
  42. // DatePattern pattern to match for the date format from http://tools.ietf.org/html/rfc3339#section-5.6
  43. DatePattern = `^([0-9]{4})-([0-9]{2})-([0-9]{2})`
  44. )
  45. var (
  46. rxDate = regexp.MustCompile(DatePattern)
  47. )
  48. // Date represents a date from the API
  49. //
  50. // swagger:strfmt date
  51. type Date time.Time
  52. // String converts this date into a string
  53. func (d Date) String() string {
  54. return time.Time(d).Format(RFC3339FullDate)
  55. }
  56. // UnmarshalText parses a text representation into a date type
  57. func (d *Date) UnmarshalText(text []byte) error {
  58. if len(text) == 0 {
  59. return nil
  60. }
  61. dd, err := time.Parse(RFC3339FullDate, string(text))
  62. if err != nil {
  63. return err
  64. }
  65. *d = Date(dd)
  66. return nil
  67. }
  68. // MarshalText serializes this date type to string
  69. func (d Date) MarshalText() ([]byte, error) {
  70. return []byte(d.String()), nil
  71. }
  72. // Scan scans a Date value from database driver type.
  73. func (d *Date) Scan(raw interface{}) error {
  74. switch v := raw.(type) {
  75. case []byte:
  76. return d.UnmarshalText(v)
  77. case string:
  78. return d.UnmarshalText([]byte(v))
  79. case time.Time:
  80. *d = Date(v)
  81. return nil
  82. case nil:
  83. *d = Date{}
  84. return nil
  85. default:
  86. return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)
  87. }
  88. }
  89. // Value converts Date to a primitive value ready to written to a database.
  90. func (d Date) Value() (driver.Value, error) {
  91. return driver.Value(d.String()), nil
  92. }
  93. func (t Date) MarshalJSON() ([]byte, error) {
  94. var w jwriter.Writer
  95. t.MarshalEasyJSON(&w)
  96. return w.BuildBytes()
  97. }
  98. func (t Date) MarshalEasyJSON(w *jwriter.Writer) {
  99. w.String(time.Time(t).Format(RFC3339FullDate))
  100. }
  101. func (t *Date) UnmarshalJSON(data []byte) error {
  102. l := jlexer.Lexer{Data: data}
  103. t.UnmarshalEasyJSON(&l)
  104. return l.Error()
  105. }
  106. func (t *Date) UnmarshalEasyJSON(in *jlexer.Lexer) {
  107. if data := in.String(); in.Ok() {
  108. tt, err := time.Parse(RFC3339FullDate, data)
  109. if err != nil {
  110. in.AddError(err)
  111. return
  112. }
  113. *t = Date(tt)
  114. }
  115. }
  116. func (t *Date) GetBSON() (interface{}, error) {
  117. return bson.M{"data": t.String()}, nil
  118. }
  119. func (t *Date) SetBSON(raw bson.Raw) error {
  120. var m bson.M
  121. if err := raw.Unmarshal(&m); err != nil {
  122. return err
  123. }
  124. if data, ok := m["data"].(string); ok {
  125. rd, err := time.Parse(RFC3339FullDate, data)
  126. *t = Date(rd)
  127. return err
  128. }
  129. return errors.New("couldn't unmarshal bson raw value as Duration")
  130. }