選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

time_stamp.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package util
  5. import (
  6. "time"
  7. "code.gitea.io/gitea/modules/setting"
  8. )
  9. // TimeStamp defines a timestamp
  10. type TimeStamp int64
  11. // TimeStampNow returns now int64
  12. func TimeStampNow() TimeStamp {
  13. return TimeStamp(time.Now().Unix())
  14. }
  15. // Add adds seconds and return sum
  16. func (ts TimeStamp) Add(seconds int64) TimeStamp {
  17. return ts + TimeStamp(seconds)
  18. }
  19. // AddDuration adds time.Duration and return sum
  20. func (ts TimeStamp) AddDuration(interval time.Duration) TimeStamp {
  21. return ts + TimeStamp(interval/time.Second)
  22. }
  23. // Year returns the time's year
  24. func (ts TimeStamp) Year() int {
  25. return ts.AsTime().Year()
  26. }
  27. // AsTime convert timestamp as time.Time in Local locale
  28. func (ts TimeStamp) AsTime() (tm time.Time) {
  29. tm = time.Unix(int64(ts), 0).In(setting.UILocation)
  30. return
  31. }
  32. // AsTimePtr convert timestamp as *time.Time in Local locale
  33. func (ts TimeStamp) AsTimePtr() *time.Time {
  34. tm := time.Unix(int64(ts), 0).In(setting.UILocation)
  35. return &tm
  36. }
  37. // Format formats timestamp as
  38. func (ts TimeStamp) Format(f string) string {
  39. return ts.AsTime().Format(f)
  40. }
  41. // FormatLong formats as RFC1123Z
  42. func (ts TimeStamp) FormatLong() string {
  43. return ts.Format(time.RFC1123Z)
  44. }
  45. // FormatShort formats as short
  46. func (ts TimeStamp) FormatShort() string {
  47. return ts.Format("Jan 02, 2006")
  48. }
  49. // IsZero is zero time
  50. func (ts TimeStamp) IsZero() bool {
  51. return ts.AsTime().IsZero()
  52. }