bitbucket.org/ai69/amoy@v0.2.3/time.go (about)

     1  package amoy
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // Common durations. Inherited from libexec/src/time/time.go
     9  //revive:disable:time-naming
    10  const (
    11  	Nanosecond  time.Duration = 1
    12  	Microsecond               = 1000 * Nanosecond
    13  	Millisecond               = 1000 * Microsecond
    14  	Second                    = 1000 * Millisecond
    15  	Minute                    = 60 * Second
    16  	Hour                      = 60 * Minute
    17  	Day                       = 24 * Hour
    18  	Week                      = 7 * Day
    19  	Year                      = 365 * Day
    20  )
    21  
    22  var (
    23  	// ZeroDuration is a zero-value for time.Duration.
    24  	ZeroDuration time.Duration
    25  
    26  	// ZeroTime is a zero-value for time.Time.
    27  	ZeroTime = time.Time{}
    28  )
    29  
    30  // GetUnixTime returns a current time in unix timestamp.
    31  func GetUnixTime() int64 {
    32  	return time.Now().Unix()
    33  }
    34  
    35  // ParseUnixTime converts a unix timestamp to local time.
    36  func ParseUnixTime(u int64) time.Time {
    37  	return time.Unix(u, 0)
    38  }
    39  
    40  // BeforeNow indicates if the given time is before current time.
    41  func BeforeNow(t time.Time) bool {
    42  	return time.Since(t) > 0
    43  }
    44  
    45  // AfterNow indicates if the given time is after current time.
    46  func AfterNow(t time.Time) bool {
    47  	return time.Until(t) > 0
    48  }
    49  
    50  // NowStr returns the current local time in RFC3339Nano format.
    51  // e.g. 2021-09-01T12:52:33.250864+08:00
    52  func NowStr() string {
    53  	return time.Now().Format(time.RFC3339Nano)
    54  }
    55  
    56  // ShortNowStr returns the current local time in short format.
    57  // e.g. 2021-09-01 12:52:33
    58  func ShortNowStr() string {
    59  	return time.Now().Format("2006-01-02 15:04:05")
    60  }
    61  
    62  // DateNowStr returns the date of current local time.
    63  // e.g. 2021-09-01
    64  func DateNowStr() string {
    65  	return time.Now().Format("2006-01-02")
    66  }
    67  
    68  // Now returns the current local time. This value is simply time.Now for consistency.
    69  var Now = time.Now
    70  
    71  // UTCNow returns the current UTC time.
    72  func UTCNow() time.Time {
    73  	return time.Now().UTC()
    74  }
    75  
    76  // UTCNowStr returns the current UTC time in RFC3339Nano format.
    77  // e.g. 2021-09-01T04:52:33.251188Z
    78  func UTCNowStr() string {
    79  	return UTCNow().UTC().Format(time.RFC3339Nano)
    80  }
    81  
    82  // ShortUTCNowStr returns the current UTC time in short format.
    83  // e.g. 2021-09-01 04:52:33
    84  func ShortUTCNowStr() string {
    85  	return UTCNow().Format("2006-01-02 15:04:05")
    86  }
    87  
    88  // DateUTCNowStr returns the date of current UTC time.
    89  // e.g. 2021-09-01
    90  func DateUTCNowStr() string {
    91  	return UTCNow().Format("2006-01-02")
    92  }
    93  
    94  // Milliseconds returns a duration of given milliseconds.
    95  func Milliseconds(n float64) time.Duration {
    96  	return time.Duration(float64(Millisecond) * n)
    97  }
    98  
    99  // Seconds returns a duration of given seconds.
   100  func Seconds(n float64) time.Duration {
   101  	return time.Duration(float64(Second) * n)
   102  }
   103  
   104  // Minutes returns a duration of given minutes.
   105  func Minutes(n float64) time.Duration {
   106  	return time.Duration(float64(Minute) * n)
   107  }
   108  
   109  // Hours returns a duration of given hours.
   110  func Hours(n float64) time.Duration {
   111  	return time.Duration(float64(Hour) * n)
   112  }
   113  
   114  // Days returns a duration of given days.
   115  func Days(n float64) time.Duration {
   116  	return time.Duration(float64(Day) * n)
   117  }
   118  
   119  // Weeks returns a duration of given weeks.
   120  func Weeks(n float64) time.Duration {
   121  	return time.Duration(float64(Week) * n)
   122  }
   123  
   124  // Years returns a duration of given years.
   125  func Years(n float64) time.Duration {
   126  	return time.Duration(float64(Year) * n)
   127  }
   128  
   129  // DayBegin returns the first moment of the given time in given location.
   130  func DayBegin(t time.Time, loc *time.Location) time.Time {
   131  	year, month, day := t.Date()
   132  	return time.Date(year, month, day, 0, 0, 0, 0, loc)
   133  }
   134  
   135  // DayEnd returns the last moment of the given time in given location.
   136  func DayEnd(t time.Time, loc *time.Location) time.Time {
   137  	year, month, day := t.Date()
   138  	return time.Date(year, month, day, 0, 0, 0, 0, loc).Add(Day).Add(-Nanosecond)
   139  }
   140  
   141  // LocalDayBegin returns the first moment of the given time in local timezone.
   142  func LocalDayBegin(t time.Time) time.Time {
   143  	year, month, day := t.Date()
   144  	return time.Date(year, month, day, 0, 0, 0, 0, time.Local)
   145  }
   146  
   147  // LocalDayEnd returns the last moment of the given time in local timezone.
   148  func LocalDayEnd(t time.Time) time.Time {
   149  	year, month, day := t.Date()
   150  	return time.Date(year, month, day, 0, 0, 0, 0, time.Local).Add(Day).Add(-Nanosecond)
   151  }
   152  
   153  // UTCDayBegin returns the first moment of the given time in UTC timezone.
   154  func UTCDayBegin(t time.Time) time.Time {
   155  	year, month, day := t.Date()
   156  	return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
   157  }
   158  
   159  // UTCDayEnd returns the last moment of the given time in UTC timezone.
   160  func UTCDayEnd(t time.Time) time.Time {
   161  	year, month, day := t.Date()
   162  	return time.Date(year, month, day, 0, 0, 0, 0, time.UTC).Add(Day).Add(-Nanosecond)
   163  }
   164  
   165  // NewLocalDay returns the first moment of the given date in local timezone.
   166  func NewLocalDay(year int, month time.Month, day int) time.Time {
   167  	return time.Date(year, month, day, 0, 0, 0, 0, time.Local)
   168  }
   169  
   170  // NewUTCDay returns the first moment of the given date in UTC timezone.
   171  func NewUTCDay(year int, month time.Month, day int) time.Time {
   172  	return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
   173  }
   174  
   175  // DurationToString returns a string representation (dd:HH:MM:SS.sss) of given duration.
   176  func DurationToString(dr time.Duration) string {
   177  	const (
   178  		dMs = 24 * 60 * 60 * 1000
   179  		hMs = 60 * 60 * 1000
   180  		mMs = 60 * 1000
   181  		sMs = 1000
   182  	)
   183  	t := int(dr.Milliseconds())
   184  	d := t / dMs
   185  	t = t % dMs
   186  
   187  	h := t / hMs
   188  	t = t % hMs
   189  
   190  	m := t / mMs
   191  	t = t % mMs
   192  
   193  	s := t / sMs
   194  	t = t % sMs
   195  	if d > 0 {
   196  		return fmt.Sprintf("%02d:%02d:%02d:%02d.%03d", d, h, m, s, t)
   197  	}
   198  	return fmt.Sprintf("%02d:%02d:%02d.%03d", h, m, s, t)
   199  }
   200  
   201  // MustParseDateString returns a time.Time of given date string (format: 2006-01-02) in UTC, or panics if it fails.
   202  func MustParseDateString(s string) time.Time {
   203  	t, err := time.ParseInLocation("2006-01-02", s, time.UTC)
   204  	if err != nil {
   205  		panic(fmt.Errorf("amoy: %w", err))
   206  	}
   207  	return t
   208  }