date.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package date
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. "strings"
  6. "time"
  7. )
  8. type Date time.Time
  9. const NormalDateFormat = "2006-01-02"
  10. func (t Date) MarshalJSON() ([]byte, error) {
  11. if y := t.Year(); y < 0 || y >= 10000 {
  12. return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
  13. }
  14. if t.IsZero() {
  15. b := []byte(`""`)
  16. return b, nil
  17. }
  18. b := make([]byte, 0, len(NormalDateFormat)+2)
  19. b = append(b, '"')
  20. b = t.AppendFormat(b, NormalDateFormat)
  21. b = append(b, '"')
  22. return b, nil
  23. }
  24. func (t *Date) UnmarshalJSON(value []byte) error {
  25. var v = strings.TrimSpace(strings.Trim(string(value), "\""))
  26. if v == "" {
  27. return nil
  28. }
  29. tm, err := time.ParseInLocation(NormalDateFormat, v, time.Local)
  30. if err != nil {
  31. return err
  32. }
  33. *t = Date(Date(tm).OfTime(0, 0, 0).OfNanosecond(0))
  34. return nil
  35. }
  36. func (t Date) MarshalText() ([]byte, error) {
  37. if y := t.Year(); y < 0 || y >= 10000 {
  38. return nil, errors.New("Time.MarshalText: year outside of range [0,9999]")
  39. }
  40. b := make([]byte, 0, len(NormalDateFormat))
  41. return t.AppendFormat(b, NormalDateFormat), nil
  42. }
  43. func (t *Date) UnmarshalText(data []byte) error {
  44. *t = t.FromString(string(data))
  45. return nil
  46. }
  47. func (t Date) FromString(str string) Date {
  48. return ParseDate(str)
  49. }
  50. func ParseDate(str string) Date {
  51. str = dateStrFormat(str)
  52. tm, err := time.Parse(NormalDateFormat, str)
  53. if nil != err {
  54. return Unix(0, 0).ToDate()
  55. }
  56. return Date(Date(tm).OfTime(0, 0, 0).OfNanosecond(0))
  57. }
  58. func (t Date) String() string {
  59. return t.Format(NormalDateFormat)
  60. }
  61. func (t Date) Value() (driver.Value, error) {
  62. if t.IsZero() {
  63. return nil, nil
  64. }
  65. return t.Format(NormalDateFormat), nil
  66. }
  67. func (t *Date) Scan(value interface{}) error {
  68. if value == nil {
  69. return nil
  70. }
  71. *t = Date(value.(time.Time))
  72. return nil
  73. }
  74. func (t Date) Format(layout string) string {
  75. layout = strings.TrimSpace(layout)
  76. return t.T().Format(layout)
  77. }
  78. func (t Date) AppendFormat(b []byte, layout string) []byte {
  79. return t.T().AppendFormat(b, layout)
  80. }
  81. // 当前时间
  82. func Today() Date {
  83. return Date(Date(time.Now()).OfTime(0, 0, 0).OfNanosecond(0))
  84. }
  85. // 构造date.Time
  86. func NewDate(year int, month time.Month, day int, loc *time.Location) Date {
  87. return Date(time.Date(year, month, day, 0, 0, 0, 0, loc))
  88. }
  89. // 转date.Datetime
  90. func (t Date) ToDatetime() Datetime {
  91. return Datetime(t)
  92. }
  93. // 转date.Date
  94. func AsDate(tm time.Time) Date {
  95. return Date(Date(tm).OfTime(0, 0, 0).OfNanosecond(0))
  96. }
  97. // 转date.Time
  98. func (t Date) ToTime() Time {
  99. return Time(t)
  100. }
  101. // 转time.Time
  102. func (t Date) T() time.Time {
  103. return time.Time(t)
  104. }
  105. // 是否晚于
  106. func (t Date) After(u Date) bool {
  107. return t.T().After(u.T())
  108. }
  109. // 是否早于
  110. func (t Date) Before(u Date) bool {
  111. return t.T().Before(u.T())
  112. }
  113. // 是否等于
  114. func (t Date) Equal(u Date) bool {
  115. return t.T().Equal(u.T())
  116. }
  117. // 是否零值
  118. func (t Date) IsZero() bool {
  119. return t.T().IsZero()
  120. }
  121. // 获取年、月、日
  122. func (t Date) Date() (year int, month time.Month, day int) {
  123. return t.T().Date()
  124. }
  125. // 获取年
  126. func (t Date) Year() int {
  127. return t.T().Year()
  128. }
  129. // 获取月
  130. func (t Date) Month() time.Month {
  131. return t.T().Month()
  132. }
  133. // 获取日
  134. func (t Date) Day() int {
  135. return t.T().Day()
  136. }
  137. // 获取星期几
  138. func (t Date) Weekday() time.Weekday {
  139. return t.T().Weekday()
  140. }
  141. // 获取年、第几周
  142. func (t Date) ISOWeek() (year, week int) {
  143. return t.T().ISOWeek()
  144. }
  145. // 获取时、分、秒
  146. func (t Date) Clock() (hour, min, sec int) {
  147. return t.T().Clock()
  148. }
  149. // 获取小时
  150. func (t Date) Hour() int {
  151. return t.T().Hour()
  152. }
  153. // 获取分钟
  154. func (t Date) Minute() int {
  155. return t.T().Minute()
  156. }
  157. // 获取秒
  158. func (t Date) Second() int {
  159. return t.T().Second()
  160. }
  161. // 获取毫秒
  162. func (t Date) Millisecond() int {
  163. return int(time.Duration(t.T().Nanosecond()) / time.Millisecond)
  164. }
  165. // 获取微秒
  166. func (t Date) Microsecond() int {
  167. return int(time.Duration(t.T().Nanosecond()) / time.Microsecond)
  168. }
  169. // 获取纳秒
  170. func (t Date) Nanosecond() int {
  171. return t.T().Nanosecond()
  172. }
  173. // 获取是一年中第几天
  174. func (t Date) YearDay() int {
  175. return t.T().YearDay()
  176. }
  177. // 获取该时间 - 参数时间 的时间差
  178. func (t Date) Sub(u Date) time.Duration {
  179. return t.T().Sub(u.T())
  180. }
  181. // 加一个时间差
  182. func (t Date) Add(d time.Duration) Datetime {
  183. return Datetime(t.T().Add(d))
  184. }
  185. // 加年、月、日
  186. func (t Date) AddDate(years int, months int, days int) Date {
  187. return Date(t.T().AddDate(years, months, days))
  188. }
  189. // 加时、分、秒
  190. func (t Date) AddTime(hours int, minutes int, seconds int) Datetime {
  191. d := time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute + time.Duration(seconds)*time.Second
  192. return t.Add(d)
  193. }
  194. // 加年
  195. func (t Date) AddYears(years int) Date {
  196. return t.AddDate(years, 0, 0)
  197. }
  198. // 加月
  199. func (t Date) AddMonths(months int) Date {
  200. return t.AddDate(0, months, 0)
  201. }
  202. // 加日
  203. func (t Date) AddDays(days int) Date {
  204. return t.AddDate(0, 0, days)
  205. }
  206. // 加小时
  207. func (t Date) AddHours(hours int) Datetime {
  208. d := time.Duration(hours) * time.Hour
  209. return t.Add(d)
  210. }
  211. // 加分钟
  212. func (t Date) AddMinutes(minutes int) Datetime {
  213. d := time.Duration(minutes) * time.Minute
  214. return t.Add(d)
  215. }
  216. // 加秒
  217. func (t Date) AddSeconds(seconds int) Datetime {
  218. d := time.Duration(seconds) * time.Second
  219. return t.Add(d)
  220. }
  221. // 加纳秒
  222. func (t Date) AddNanoseconds(nanoseconds int) Datetime {
  223. d := time.Duration(nanoseconds) * time.Nanosecond
  224. return t.Add(d)
  225. }
  226. // 指定时间
  227. func (t Date) Of(year int, month time.Month, day int, hour int, minute int, second int, nanosecond int) Datetime {
  228. return Datetime(time.Date(year, month, day, hour, minute, second, nanosecond, t.Location()))
  229. }
  230. // 指定年、月、日
  231. func (t Date) OfDate(year int, month time.Month, day int) Date {
  232. hour, minute, second := t.Clock()
  233. return Date(t.Of(year, month, day, hour, minute, second, t.Nanosecond()))
  234. }
  235. // 指定时、分、秒
  236. func (t Date) OfTime(hour int, minute int, second int) Datetime {
  237. year, month, day := t.Date()
  238. return t.Of(year, month, day, hour, minute, second, t.Nanosecond())
  239. }
  240. // 指定年
  241. func (t Date) OfYear(year int) Date {
  242. return t.OfDate(year, t.Month(), t.Day())
  243. }
  244. // 指定月
  245. func (t Date) OfMonth(month time.Month) Date {
  246. return t.OfDate(t.Year(), month, t.Day())
  247. }
  248. // 指定日
  249. func (t Date) OfDay(day int) Date {
  250. return t.OfDate(t.Year(), t.Month(), day)
  251. }
  252. // 指定小时
  253. func (t Date) OfHour(hour int) Datetime {
  254. return t.OfTime(hour, t.Minute(), t.Second())
  255. }
  256. // 指定分钟
  257. func (t Date) OfMinute(minute int) Datetime {
  258. return t.OfTime(t.Hour(), minute, t.Second())
  259. }
  260. // 指定秒
  261. func (t Date) OfSecond(second int) Datetime {
  262. return t.OfTime(t.Hour(), t.Minute(), second)
  263. }
  264. // 指定纳秒
  265. func (t Date) OfNanosecond(nanosecond int) Datetime {
  266. year, month, day := t.Date()
  267. hour, minute, second := t.Clock()
  268. return t.Of(year, month, day, hour, minute, second, nanosecond)
  269. }
  270. // 转UTC时间
  271. func (t Date) UTC() Datetime {
  272. return Datetime(t.T().UTC())
  273. }
  274. // 转本地时间
  275. func (t Date) Local() Datetime {
  276. return Datetime(t.T().Local())
  277. }
  278. // 转指定时区时间
  279. func (t Date) In(loc *time.Location) Datetime {
  280. return Datetime(t.T().In(loc))
  281. }
  282. // 获取时区
  283. func (t Date) Location() *time.Location {
  284. return t.T().Location()
  285. }
  286. // 获取时区
  287. func (t Date) Zone() (name string, offset int) {
  288. return t.T().Zone()
  289. }
  290. // 获取UTC时间戳
  291. func (t Date) Unix() int64 {
  292. return t.T().Unix()
  293. }
  294. // 获取UTC纳秒数
  295. func (t Date) UnixNano() int64 {
  296. return t.T().Unix()
  297. }
  298. // 从目标日期开始到现在的时间差
  299. func SinceDate(t Date) time.Duration {
  300. return Today().Sub(t)
  301. }
  302. // 现在到目标日期的时间差
  303. func UntilDate(t Date) time.Duration {
  304. return t.Sub(Today())
  305. }