gtime_time.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. "strconv"
  10. "time"
  11. "github.com/gogf/gf/v2/errors/gcode"
  12. "github.com/gogf/gf/v2/errors/gerror"
  13. )
  14. // Time is a wrapper for time.Time for additional features.
  15. type Time struct {
  16. wrapper
  17. }
  18. // iUnixNano is an interface definition commonly for custom time.Time wrapper.
  19. type iUnixNano 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.(iUnixNano); 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.wrapper.String()
  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. err = gerror.Wrapf(err, `time.ParseDuration failed for string "%s"`, duration)
  220. return nil, err
  221. } else {
  222. return t.Add(d), nil
  223. }
  224. }
  225. // UTC converts current time to UTC timezone.
  226. func (t *Time) UTC() *Time {
  227. newTime := t.Clone()
  228. newTime.Time = newTime.Time.UTC()
  229. return newTime
  230. }
  231. // ISO8601 formats the time as ISO8601 and returns it as string.
  232. func (t *Time) ISO8601() string {
  233. return t.Layout("2006-01-02T15:04:05-07:00")
  234. }
  235. // RFC822 formats the time as RFC822 and returns it as string.
  236. func (t *Time) RFC822() string {
  237. return t.Layout("Mon, 02 Jan 06 15:04 MST")
  238. }
  239. // AddDate adds year, month and day to the time.
  240. func (t *Time) AddDate(years int, months int, days int) *Time {
  241. newTime := t.Clone()
  242. newTime.Time = newTime.Time.AddDate(years, months, days)
  243. return newTime
  244. }
  245. // Round returns the result of rounding t to the nearest multiple of d (since the zero time).
  246. // The rounding behavior for halfway values is to round up.
  247. // If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.
  248. //
  249. // Round operates on the time as an absolute duration since the
  250. // zero time; it does not operate on the presentation form of the
  251. // time. Thus, Round(Hour) may return a time with a non-zero
  252. // minute, depending on the time's Location.
  253. func (t *Time) Round(d time.Duration) *Time {
  254. newTime := t.Clone()
  255. newTime.Time = newTime.Time.Round(d)
  256. return newTime
  257. }
  258. // Truncate returns the result of rounding t down to a multiple of d (since the zero time).
  259. // If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.
  260. //
  261. // Truncate operates on the time as an absolute duration since the
  262. // zero time; it does not operate on the presentation form of the
  263. // time. Thus, Truncate(Hour) may return a time with a non-zero
  264. // minute, depending on the time's Location.
  265. func (t *Time) Truncate(d time.Duration) *Time {
  266. newTime := t.Clone()
  267. newTime.Time = newTime.Time.Truncate(d)
  268. return newTime
  269. }
  270. // Equal reports whether t and u represent the same time instant.
  271. // Two times can be equal even if they are in different locations.
  272. // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
  273. // See the documentation on the Time type for the pitfalls of using == with
  274. // Time values; most code should use Equal instead.
  275. func (t *Time) Equal(u *Time) bool {
  276. switch {
  277. case t == nil && u != nil:
  278. return false
  279. case t == nil && u == nil:
  280. return true
  281. case t != nil && u == nil:
  282. return false
  283. default:
  284. return t.Time.Equal(u.Time)
  285. }
  286. }
  287. // Before reports whether the time instant t is before u.
  288. func (t *Time) Before(u *Time) bool {
  289. return t.Time.Before(u.Time)
  290. }
  291. // After reports whether the time instant t is after u.
  292. func (t *Time) After(u *Time) bool {
  293. switch {
  294. case t == nil:
  295. return false
  296. case t != nil && u == nil:
  297. return true
  298. default:
  299. return t.Time.After(u.Time)
  300. }
  301. }
  302. // Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
  303. // value that can be stored in a Duration, the maximum (or minimum) duration
  304. // will be returned.
  305. // To compute t-d for a duration d, use t.Add(-d).
  306. func (t *Time) Sub(u *Time) time.Duration {
  307. if t == nil || u == nil {
  308. return 0
  309. }
  310. return t.Time.Sub(u.Time)
  311. }
  312. // StartOfMinute clones and returns a new time of which the seconds is set to 0.
  313. func (t *Time) StartOfMinute() *Time {
  314. newTime := t.Clone()
  315. newTime.Time = newTime.Time.Truncate(time.Minute)
  316. return newTime
  317. }
  318. // StartOfHour clones and returns a new time of which the hour, minutes and seconds are set to 0.
  319. func (t *Time) StartOfHour() *Time {
  320. y, m, d := t.Date()
  321. newTime := t.Clone()
  322. newTime.Time = time.Date(y, m, d, newTime.Time.Hour(), 0, 0, 0, newTime.Time.Location())
  323. return newTime
  324. }
  325. // StartOfDay clones and returns a new time which is the start of day, its time is set to 00:00:00.
  326. func (t *Time) StartOfDay() *Time {
  327. y, m, d := t.Date()
  328. newTime := t.Clone()
  329. newTime.Time = time.Date(y, m, d, 0, 0, 0, 0, newTime.Time.Location())
  330. return newTime
  331. }
  332. // StartOfWeek clones and returns a new time which is the first day of week and its time is set to
  333. // 00:00:00.
  334. func (t *Time) StartOfWeek() *Time {
  335. weekday := int(t.Weekday())
  336. return t.StartOfDay().AddDate(0, 0, -weekday)
  337. }
  338. // StartOfMonth clones and returns a new time which is the first day of the month and its is set to
  339. // 00:00:00
  340. func (t *Time) StartOfMonth() *Time {
  341. y, m, _ := t.Date()
  342. newTime := t.Clone()
  343. newTime.Time = time.Date(y, m, 1, 0, 0, 0, 0, newTime.Time.Location())
  344. return newTime
  345. }
  346. // StartOfQuarter clones and returns a new time which is the first day of the quarter and its time is set
  347. // to 00:00:00.
  348. func (t *Time) StartOfQuarter() *Time {
  349. month := t.StartOfMonth()
  350. offset := (int(month.Month()) - 1) % 3
  351. return month.AddDate(0, -offset, 0)
  352. }
  353. // StartOfHalf clones and returns a new time which is the first day of the half year and its time is set
  354. // to 00:00:00.
  355. func (t *Time) StartOfHalf() *Time {
  356. month := t.StartOfMonth()
  357. offset := (int(month.Month()) - 1) % 6
  358. return month.AddDate(0, -offset, 0)
  359. }
  360. // StartOfYear clones and returns a new time which is the first day of the year and its time is set to
  361. // 00:00:00.
  362. func (t *Time) StartOfYear() *Time {
  363. y, _, _ := t.Date()
  364. newTime := t.Clone()
  365. newTime.Time = time.Date(y, time.January, 1, 0, 0, 0, 0, newTime.Time.Location())
  366. return newTime
  367. }
  368. // getPrecisionDelta returns the precision parameter for time calculation depending on `withNanoPrecision` option.
  369. func getPrecisionDelta(withNanoPrecision ...bool) time.Duration {
  370. if len(withNanoPrecision) > 0 && withNanoPrecision[0] {
  371. return time.Nanosecond
  372. }
  373. return time.Second
  374. }
  375. // EndOfMinute clones and returns a new time of which the seconds is set to 59.
  376. func (t *Time) EndOfMinute(withNanoPrecision ...bool) *Time {
  377. return t.StartOfMinute().Add(time.Minute - getPrecisionDelta(withNanoPrecision...))
  378. }
  379. // EndOfHour clones and returns a new time of which the minutes and seconds are both set to 59.
  380. func (t *Time) EndOfHour(withNanoPrecision ...bool) *Time {
  381. return t.StartOfHour().Add(time.Hour - getPrecisionDelta(withNanoPrecision...))
  382. }
  383. // EndOfDay clones and returns a new time which is the end of day the and its time is set to 23:59:59.
  384. func (t *Time) EndOfDay(withNanoPrecision ...bool) *Time {
  385. y, m, d := t.Date()
  386. newTime := t.Clone()
  387. newTime.Time = time.Date(
  388. y, m, d, 23, 59, 59, int(time.Second-getPrecisionDelta(withNanoPrecision...)), newTime.Time.Location(),
  389. )
  390. return newTime
  391. }
  392. // EndOfWeek clones and returns a new time which is the end of week and its time is set to 23:59:59.
  393. func (t *Time) EndOfWeek(withNanoPrecision ...bool) *Time {
  394. return t.StartOfWeek().AddDate(0, 0, 7).Add(-getPrecisionDelta(withNanoPrecision...))
  395. }
  396. // EndOfMonth clones and returns a new time which is the end of the month and its time is set to 23:59:59.
  397. func (t *Time) EndOfMonth(withNanoPrecision ...bool) *Time {
  398. return t.StartOfMonth().AddDate(0, 1, 0).Add(-getPrecisionDelta(withNanoPrecision...))
  399. }
  400. // EndOfQuarter clones and returns a new time which is end of the quarter and its time is set to 23:59:59.
  401. func (t *Time) EndOfQuarter(withNanoPrecision ...bool) *Time {
  402. return t.StartOfQuarter().AddDate(0, 3, 0).Add(-getPrecisionDelta(withNanoPrecision...))
  403. }
  404. // EndOfHalf clones and returns a new time which is the end of the half year and its time is set to 23:59:59.
  405. func (t *Time) EndOfHalf(withNanoPrecision ...bool) *Time {
  406. return t.StartOfHalf().AddDate(0, 6, 0).Add(-getPrecisionDelta(withNanoPrecision...))
  407. }
  408. // EndOfYear clones and returns a new time which is the end of the year and its time is set to 23:59:59.
  409. func (t *Time) EndOfYear(withNanoPrecision ...bool) *Time {
  410. return t.StartOfYear().AddDate(1, 0, 0).Add(-getPrecisionDelta(withNanoPrecision...))
  411. }
  412. // MarshalJSON implements the interface MarshalJSON for json.Marshal.
  413. // Note that, DO NOT use `(t *Time) MarshalJSON() ([]byte, error)` as it looses interface
  414. // implement of `MarshalJSON` for struct of Time.
  415. func (t Time) MarshalJSON() ([]byte, error) {
  416. return []byte(`"` + t.String() + `"`), nil
  417. }
  418. // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
  419. func (t *Time) UnmarshalJSON(b []byte) error {
  420. if len(b) == 0 {
  421. t.Time = time.Time{}
  422. return nil
  423. }
  424. newTime, err := StrToTime(string(bytes.Trim(b, `"`)))
  425. if err != nil {
  426. return err
  427. }
  428. t.Time = newTime.Time
  429. return nil
  430. }
  431. // UnmarshalText implements the encoding.TextUnmarshaler interface.
  432. // Note that it overwrites the same implementer of `time.Time`.
  433. func (t *Time) UnmarshalText(data []byte) error {
  434. vTime := New(data)
  435. if vTime != nil {
  436. *t = *vTime
  437. return nil
  438. }
  439. return gerror.NewCodef(gcode.CodeInvalidParameter, `invalid time value: %s`, data)
  440. }
  441. // NoValidation marks this struct object will not be validated by package gvalid.
  442. func (t *Time) NoValidation() {}
  443. // DeepCopy implements interface for deep copy of current type.
  444. func (t *Time) DeepCopy() interface{} {
  445. if t == nil {
  446. return nil
  447. }
  448. return New(t.Time)
  449. }