time.go 7.5 KB

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