You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timestamp.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package timeutil
  4. import (
  5. "time"
  6. "code.gitea.io/gitea/modules/setting"
  7. )
  8. // TimeStamp defines a timestamp
  9. type TimeStamp int64
  10. var (
  11. // mock is NOT concurrency-safe!!
  12. mock time.Time
  13. // Used for IsZero, to check if timestamp is the zero time instant.
  14. timeZeroUnix = time.Time{}.Unix()
  15. )
  16. // Set sets the time to a mocked time.Time
  17. func Set(now time.Time) {
  18. mock = now
  19. }
  20. // Unset will unset the mocked time.Time
  21. func Unset() {
  22. mock = time.Time{}
  23. }
  24. // TimeStampNow returns now int64
  25. func TimeStampNow() TimeStamp {
  26. if !mock.IsZero() {
  27. return TimeStamp(mock.Unix())
  28. }
  29. return TimeStamp(time.Now().Unix())
  30. }
  31. // Add adds seconds and return sum
  32. func (ts TimeStamp) Add(seconds int64) TimeStamp {
  33. return ts + TimeStamp(seconds)
  34. }
  35. // AddDuration adds time.Duration and return sum
  36. func (ts TimeStamp) AddDuration(interval time.Duration) TimeStamp {
  37. return ts + TimeStamp(interval/time.Second)
  38. }
  39. // Year returns the time's year
  40. func (ts TimeStamp) Year() int {
  41. return ts.AsTime().Year()
  42. }
  43. // AsTime convert timestamp as time.Time in Local locale
  44. func (ts TimeStamp) AsTime() (tm time.Time) {
  45. return ts.AsTimeInLocation(setting.DefaultUILocation)
  46. }
  47. // AsLocalTime convert timestamp as time.Time in local location
  48. func (ts TimeStamp) AsLocalTime() time.Time {
  49. return time.Unix(int64(ts), 0)
  50. }
  51. // AsTimeInLocation convert timestamp as time.Time in Local locale
  52. func (ts TimeStamp) AsTimeInLocation(loc *time.Location) time.Time {
  53. return time.Unix(int64(ts), 0).In(loc)
  54. }
  55. // AsTimePtr convert timestamp as *time.Time in Local locale
  56. func (ts TimeStamp) AsTimePtr() *time.Time {
  57. return ts.AsTimePtrInLocation(setting.DefaultUILocation)
  58. }
  59. // AsTimePtrInLocation convert timestamp as *time.Time in customize location
  60. func (ts TimeStamp) AsTimePtrInLocation(loc *time.Location) *time.Time {
  61. tm := time.Unix(int64(ts), 0).In(loc)
  62. return &tm
  63. }
  64. // Format formats timestamp as given format
  65. func (ts TimeStamp) Format(f string) string {
  66. return ts.FormatInLocation(f, setting.DefaultUILocation)
  67. }
  68. // FormatInLocation formats timestamp as given format with spiecific location
  69. func (ts TimeStamp) FormatInLocation(f string, loc *time.Location) string {
  70. return ts.AsTimeInLocation(loc).Format(f)
  71. }
  72. // FormatLong formats as RFC1123Z
  73. func (ts TimeStamp) FormatLong() string {
  74. return ts.Format(time.RFC1123Z)
  75. }
  76. // FormatShort formats as short
  77. func (ts TimeStamp) FormatShort() string {
  78. return ts.Format("Jan 02, 2006")
  79. }
  80. // FormatDate formats a date in YYYY-MM-DD server time zone
  81. func (ts TimeStamp) FormatDate() string {
  82. return time.Unix(int64(ts), 0).String()[:10]
  83. }
  84. // IsZero is zero time
  85. func (ts TimeStamp) IsZero() bool {
  86. return int64(ts) == 0 || int64(ts) == timeZeroUnix
  87. }