github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/timeutil/timestamp.go (about)

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