gtime_time.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gtime
  7. import (
  8. "bytes"
  9. "github.com/gogf/gf/errors/gcode"
  10. "github.com/gogf/gf/errors/gerror"
  11. "strconv"
  12. "time"
  13. )
  14. // Time is a wrapper for time.Time for additional features.
  15. type Time struct {
  16. wrapper
  17. }
  18. // apiUnixNano is an interface definition commonly for custom time.Time wrapper.
  19. type apiUnixNano interface {
  20. UnixNano() int64
  21. }
  22. // New creates and returns a Time object with given parameter.
  23. // The optional parameter can be type of: time.Time/*time.Time, string or integer.
  24. func New(param ...interface{}) *Time {
  25. if len(param) > 0 {
  26. switch r := param[0].(type) {
  27. case time.Time:
  28. return NewFromTime(r)
  29. case *time.Time:
  30. return NewFromTime(*r)
  31. case Time:
  32. return &r
  33. case *Time:
  34. return r
  35. case string:
  36. if len(param) > 1 {
  37. switch t := param[1].(type) {
  38. case string:
  39. return NewFromStrFormat(r, t)
  40. case []byte:
  41. return NewFromStrFormat(r, string(t))
  42. }
  43. }
  44. return NewFromStr(r)
  45. case []byte:
  46. if len(param) > 1 {
  47. switch t := param[1].(type) {
  48. case string:
  49. return NewFromStrFormat(string(r), t)
  50. case []byte:
  51. return NewFromStrFormat(string(r), string(t))
  52. }
  53. }
  54. return NewFromStr(string(r))
  55. case int:
  56. return NewFromTimeStamp(int64(r))
  57. case int64:
  58. return NewFromTimeStamp(r)
  59. default:
  60. if v, ok := r.(apiUnixNano); ok {
  61. return NewFromTimeStamp(v.UnixNano())
  62. }
  63. }
  64. }
  65. return &Time{
  66. wrapper{time.Time{}},
  67. }
  68. }
  69. // Now creates and returns a time object of now.
  70. func Now() *Time {
  71. return &Time{
  72. wrapper{time.Now()},
  73. }
  74. }
  75. // NewFromTime creates and returns a Time object with given time.Time object.
  76. func NewFromTime(t time.Time) *Time {
  77. return &Time{
  78. wrapper{t},
  79. }
  80. }
  81. // NewFromStr creates and returns a Time object with given string.
  82. // Note that it returns nil if there's error occurs.
  83. func NewFromStr(str string) *Time {
  84. if t, err := StrToTime(str); err == nil {
  85. return t
  86. }
  87. return nil
  88. }
  89. // NewFromStrFormat creates and returns a Time object with given string and
  90. // custom format like: Y-m-d H:i:s.
  91. // Note that it returns nil if there's error occurs.
  92. func NewFromStrFormat(str string, format string) *Time {
  93. if t, err := StrToTimeFormat(str, format); err == nil {
  94. return t
  95. }
  96. return nil
  97. }
  98. // NewFromStrLayout creates and returns a Time object with given string and
  99. // stdlib layout like: 2006-01-02 15:04:05.
  100. // Note that it returns nil if there's error occurs.
  101. func NewFromStrLayout(str string, layout string) *Time {
  102. if t, err := StrToTimeLayout(str, layout); err == nil {
  103. return t
  104. }
  105. return nil
  106. }
  107. // NewFromTimeStamp creates and returns a Time object with given timestamp,
  108. // which can be in seconds to nanoseconds.
  109. // Eg: 1600443866 and 1600443866199266000 are both considered as valid timestamp number.
  110. func NewFromTimeStamp(timestamp int64) *Time {
  111. if timestamp == 0 {
  112. return &Time{}
  113. }
  114. var sec, nano int64
  115. if timestamp > 1e9 {
  116. for timestamp < 1e18 {
  117. timestamp *= 10
  118. }
  119. sec = timestamp / 1e9
  120. nano = timestamp % 1e9
  121. } else {
  122. sec = timestamp
  123. }
  124. return &Time{
  125. wrapper{time.Unix(sec, nano)},
  126. }
  127. }
  128. // Timestamp returns the timestamp in seconds.
  129. func (t *Time) Timestamp() int64 {
  130. return t.UnixNano() / 1e9
  131. }
  132. // TimestampMilli returns the timestamp in milliseconds.
  133. func (t *Time) TimestampMilli() int64 {
  134. return t.UnixNano() / 1e6
  135. }
  136. // TimestampMicro returns the timestamp in microseconds.
  137. func (t *Time) TimestampMicro() int64 {
  138. return t.UnixNano() / 1e3
  139. }
  140. // TimestampNano returns the timestamp in nanoseconds.
  141. func (t *Time) TimestampNano() int64 {
  142. return t.UnixNano()
  143. }
  144. // TimestampStr is a convenience method which retrieves and returns
  145. // the timestamp in seconds as string.
  146. func (t *Time) TimestampStr() string {
  147. return strconv.FormatInt(t.Timestamp(), 10)
  148. }
  149. // TimestampMilliStr is a convenience method which retrieves and returns
  150. // the timestamp in milliseconds as string.
  151. func (t *Time) TimestampMilliStr() string {
  152. return strconv.FormatInt(t.TimestampMilli(), 10)
  153. }
  154. // TimestampMicroStr is a convenience method which retrieves and returns
  155. // the timestamp in microseconds as string.
  156. func (t *Time) TimestampMicroStr() string {
  157. return strconv.FormatInt(t.TimestampMicro(), 10)
  158. }
  159. // TimestampNanoStr is a convenience method which retrieves and returns
  160. // the timestamp in nanoseconds as string.
  161. func (t *Time) TimestampNanoStr() string {
  162. return strconv.FormatInt(t.TimestampNano(), 10)
  163. }
  164. // Month returns the month of the year specified by t.
  165. func (t *Time) Month() int {
  166. return int(t.Time.Month())
  167. }
  168. // Second returns the second offset within the minute specified by t,
  169. // in the range [0, 59].
  170. func (t *Time) Second() int {
  171. return t.Time.Second()
  172. }
  173. // Millisecond returns the millisecond offset within the second specified by t,
  174. // in the range [0, 999].
  175. func (t *Time) Millisecond() int {
  176. return t.Time.Nanosecond() / 1e6
  177. }
  178. // Microsecond returns the microsecond offset within the second specified by t,
  179. // in the range [0, 999999].
  180. func (t *Time) Microsecond() int {
  181. return t.Time.Nanosecond() / 1e3
  182. }
  183. // Nanosecond returns the nanosecond offset within the second specified by t,
  184. // in the range [0, 999999999].
  185. func (t *Time) Nanosecond() int {
  186. return t.Time.Nanosecond()
  187. }
  188. // String returns current time object as string.
  189. func (t *Time) String() string {
  190. if t == nil {
  191. return ""
  192. }
  193. if t.IsZero() {
  194. return ""
  195. }
  196. return t.Format("Y-m-d H:i:s")
  197. }
  198. // IsZero reports whether t represents the zero time instant,
  199. // January 1, year 1, 00:00:00 UTC.
  200. func (t *Time) IsZero() bool {
  201. if t == nil {
  202. return true
  203. }
  204. return t.Time.IsZero()
  205. }
  206. // Clone returns a new Time object which is a clone of current time object.
  207. func (t *Time) Clone() *Time {
  208. return New(t.Time)
  209. }
  210. // Add adds the duration to current time.
  211. func (t *Time) Add(d time.Duration) *Time {
  212. newTime := t.Clone()
  213. newTime.Time = newTime.Time.Add(d)
  214. return newTime
  215. }
  216. // AddStr parses the given duration as string and adds it to current time.
  217. func (t *Time) AddStr(duration string) (*Time, error) {
  218. if d, err := time.ParseDuration(duration); err != nil {
  219. return nil, err
  220. } else {
  221. return t.Add(d), nil
  222. }
  223. }
  224. // UTC converts current time to UTC timezone.
  225. func (t *Time) UTC() *Time {
  226. newTime := t.Clone()
  227. newTime.Time = newTime.Time.UTC()
  228. return newTime
  229. }
  230. // ISO8601 formats the time as ISO8601 and returns it as string.
  231. func (t *Time) ISO8601() string {
  232. return t.Layout("2006-01-02T15:04:05-07:00")
  233. }
  234. // RFC822 formats the time as RFC822 and returns it as string.
  235. func (t *Time) RFC822() string {
  236. return t.Layout("Mon, 02 Jan 06 15:04 MST")
  237. }
  238. // AddDate adds year, month and day to the time.
  239. func (t *Time) AddDate(years int, months int, days int) *Time {
  240. newTime := t.Clone()
  241. newTime.Time = newTime.Time.AddDate(years, months, days)
  242. return newTime
  243. }
  244. // Round returns the result of rounding t to the nearest multiple of d (since the zero time).
  245. // The rounding behavior for halfway values is to round up.
  246. // If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.
  247. //
  248. // Round operates on the time as an absolute duration since the
  249. // zero time; it does not operate on the presentation form of the
  250. // time. Thus, Round(Hour) may return a time with a non-zero
  251. // minute, depending on the time's Location.
  252. func (t *Time) Round(d time.Duration) *Time {
  253. newTime := t.Clone()
  254. newTime.Time = newTime.Time.Round(d)
  255. return newTime
  256. }
  257. // Truncate returns the result of rounding t down to a multiple of d (since the zero time).
  258. // If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.
  259. //
  260. // Truncate operates on the time as an absolute duration since the
  261. // zero time; it does not operate on the presentation form of the
  262. // time. Thus, Truncate(Hour) may return a time with a non-zero
  263. // minute, depending on the time's Location.
  264. func (t *Time) Truncate(d time.Duration) *Time {
  265. newTime := t.Clone()
  266. newTime.Time = newTime.Time.Truncate(d)
  267. return newTime
  268. }
  269. // Equal reports whether t and u represent the same time instant.
  270. // Two times can be equal even if they are in different locations.
  271. // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
  272. // See the documentation on the Time type for the pitfalls of using == with
  273. // Time values; most code should use Equal instead.
  274. func (t *Time) Equal(u *Time) bool {
  275. return t.Time.Equal(u.Time)
  276. }
  277. // Before reports whether the time instant t is before u.
  278. func (t *Time) Before(u *Time) bool {
  279. return t.Time.Before(u.Time)
  280. }
  281. // After reports whether the time instant t is after u.
  282. func (t *Time) After(u *Time) bool {
  283. return t.Time.After(u.Time)
  284. }
  285. // Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
  286. // value that can be stored in a Duration, the maximum (or minimum) duration
  287. // will be returned.
  288. // To compute t-d for a duration d, use t.Add(-d).
  289. func (t *Time) Sub(u *Time) time.Duration {
  290. return t.Time.Sub(u.Time)
  291. }
  292. // StartOfMinute clones and returns a new time of which the seconds is set to 0.
  293. func (t *Time) StartOfMinute() *Time {
  294. newTime := t.Clone()
  295. newTime.Time = newTime.Time.Truncate(time.Minute)
  296. return newTime
  297. }
  298. // StartOfHour clones and returns a new time of which the hour, minutes and seconds are set to 0.
  299. func (t *Time) StartOfHour() *Time {
  300. y, m, d := t.Date()
  301. newTime := t.Clone()
  302. newTime.Time = time.Date(y, m, d, newTime.Time.Hour(), 0, 0, 0, newTime.Time.Location())
  303. return newTime
  304. }
  305. // StartOfDay clones and returns a new time which is the start of day, its time is set to 00:00:00.
  306. func (t *Time) StartOfDay() *Time {
  307. y, m, d := t.Date()
  308. newTime := t.Clone()
  309. newTime.Time = time.Date(y, m, d, 0, 0, 0, 0, newTime.Time.Location())
  310. return newTime
  311. }
  312. // StartOfWeek clones and returns a new time which is the first day of week and its time is set to
  313. // 00:00:00.
  314. func (t *Time) StartOfWeek() *Time {
  315. weekday := int(t.Weekday())
  316. return t.StartOfDay().AddDate(0, 0, -weekday)
  317. }
  318. // StartOfMonth clones and returns a new time which is the first day of the month and its is set to
  319. // 00:00:00
  320. func (t *Time) StartOfMonth() *Time {
  321. y, m, _ := t.Date()
  322. newTime := t.Clone()
  323. newTime.Time = time.Date(y, m, 1, 0, 0, 0, 0, newTime.Time.Location())
  324. return newTime
  325. }
  326. // StartOfQuarter clones and returns a new time which is the first day of the quarter and its time is set
  327. // to 00:00:00.
  328. func (t *Time) StartOfQuarter() *Time {
  329. month := t.StartOfMonth()
  330. offset := (int(month.Month()) - 1) % 3
  331. return month.AddDate(0, -offset, 0)
  332. }
  333. // StartOfHalf clones and returns a new time which is the first day of the half year and its time is set
  334. // to 00:00:00.
  335. func (t *Time) StartOfHalf() *Time {
  336. month := t.StartOfMonth()
  337. offset := (int(month.Month()) - 1) % 6
  338. return month.AddDate(0, -offset, 0)
  339. }
  340. // StartOfYear clones and returns a new time which is the first day of the year and its time is set to
  341. // 00:00:00.
  342. func (t *Time) StartOfYear() *Time {
  343. y, _, _ := t.Date()
  344. newTime := t.Clone()
  345. newTime.Time = time.Date(y, time.January, 1, 0, 0, 0, 0, newTime.Time.Location())
  346. return newTime
  347. }
  348. // EndOfMinute clones and returns a new time of which the seconds is set to 59.
  349. func (t *Time) EndOfMinute() *Time {
  350. return t.StartOfMinute().Add(time.Minute - time.Nanosecond)
  351. }
  352. // EndOfHour clones and returns a new time of which the minutes and seconds are both set to 59.
  353. func (t *Time) EndOfHour() *Time {
  354. return t.StartOfHour().Add(time.Hour - time.Nanosecond)
  355. }
  356. // EndOfDay clones and returns a new time which is the end of day the and its time is set to 23:59:59.
  357. func (t *Time) EndOfDay() *Time {
  358. y, m, d := t.Date()
  359. newTime := t.Clone()
  360. newTime.Time = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), newTime.Time.Location())
  361. return newTime
  362. }
  363. // EndOfWeek clones and returns a new time which is the end of week and its time is set to 23:59:59.
  364. func (t *Time) EndOfWeek() *Time {
  365. return t.StartOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
  366. }
  367. // EndOfMonth clones and returns a new time which is the end of the month and its time is set to 23:59:59.
  368. func (t *Time) EndOfMonth() *Time {
  369. return t.StartOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
  370. }
  371. // EndOfQuarter clones and returns a new time which is end of the quarter and its time is set to 23:59:59.
  372. func (t *Time) EndOfQuarter() *Time {
  373. return t.StartOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
  374. }
  375. // EndOfHalf clones and returns a new time which is the end of the half year and its time is set to 23:59:59.
  376. func (t *Time) EndOfHalf() *Time {
  377. return t.StartOfHalf().AddDate(0, 6, 0).Add(-time.Nanosecond)
  378. }
  379. // EndOfYear clones and returns a new time which is the end of the year and its time is set to 23:59:59.
  380. func (t *Time) EndOfYear() *Time {
  381. return t.StartOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
  382. }
  383. // MarshalJSON implements the interface MarshalJSON for json.Marshal.
  384. func (t *Time) MarshalJSON() ([]byte, error) {
  385. return []byte(`"` + t.String() + `"`), nil
  386. }
  387. // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
  388. func (t *Time) UnmarshalJSON(b []byte) error {
  389. if len(b) == 0 {
  390. t.Time = time.Time{}
  391. return nil
  392. }
  393. newTime, err := StrToTime(string(bytes.Trim(b, `"`)))
  394. if err != nil {
  395. return err
  396. }
  397. t.Time = newTime.Time
  398. return nil
  399. }
  400. // UnmarshalText implements the encoding.TextUnmarshaler interface.
  401. // Note that it overwrites the same implementer of `time.Time`.
  402. func (t *Time) UnmarshalText(data []byte) error {
  403. vTime := New(data)
  404. if vTime != nil {
  405. *t = *vTime
  406. return nil
  407. }
  408. return gerror.NewCodef(gcode.CodeInvalidParameter, `invalid time value: %s`, data)
  409. }
  410. // NoValidation marks this struct object will not be validated by package gvalid.
  411. func (t *Time) NoValidation() {
  412. }