code.gitea.io/gitea@v1.19.3/modules/timeutil/timestamp.go (about)

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