github.com/blend/go-sdk@v1.20220411.3/cron/util.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import "time"
    11  
    12  // this is used by `Now()`
    13  // it can be overrided in tests etc.
    14  var _nowProvider = time.Now
    15  
    16  // Now returns a new timestamp.
    17  func Now() time.Time {
    18  	return _nowProvider().UTC()
    19  }
    20  
    21  // Since returns the duration since another timestamp.
    22  func Since(t time.Time) time.Duration {
    23  	return Now().Sub(t)
    24  }
    25  
    26  // Min returns the minimum of two times.
    27  func Min(t1, t2 time.Time) time.Time {
    28  	if t1.IsZero() && t2.IsZero() {
    29  		return time.Time{}
    30  	}
    31  	if t1.IsZero() {
    32  		return t2
    33  	}
    34  	if t2.IsZero() {
    35  		return t1
    36  	}
    37  	if t1.Before(t2) {
    38  		return t1
    39  	}
    40  	return t2
    41  }
    42  
    43  // Max returns the maximum of two times.
    44  func Max(t1, t2 time.Time) time.Time {
    45  	if t1.Before(t2) {
    46  		return t2
    47  	}
    48  	return t1
    49  }
    50  
    51  // FormatTime returns a string for a time.
    52  func FormatTime(t time.Time) string {
    53  	return t.Format(time.RFC3339)
    54  }
    55  
    56  // NOTE: time.Zero()? what's that?
    57  var (
    58  	// DaysOfWeek are all the time.Weekday in an array for utility purposes.
    59  	DaysOfWeek = []time.Weekday{
    60  		time.Sunday,
    61  		time.Monday,
    62  		time.Tuesday,
    63  		time.Wednesday,
    64  		time.Thursday,
    65  		time.Friday,
    66  		time.Saturday,
    67  	}
    68  
    69  	// WeekDays are the business time.Weekday in an array.
    70  	WeekDays = []time.Weekday{
    71  		time.Monday,
    72  		time.Tuesday,
    73  		time.Wednesday,
    74  		time.Thursday,
    75  		time.Friday,
    76  	}
    77  
    78  	// WeekWeekEndDaysDays are the weekend time.Weekday in an array.
    79  	WeekendDays = []time.Weekday{
    80  		time.Sunday,
    81  		time.Saturday,
    82  	}
    83  
    84  	// Epoch is unix epoch saved for utility purposes.
    85  	Epoch = time.Unix(0, 0)
    86  	// Zero is different than epoch in that it is the "unset" value for a time
    87  	// where Epoch is a valid date. Nominally it is `time.Time{}`.
    88  	Zero = time.Time{}
    89  )
    90  
    91  // NOTE: we have to use shifts here because in their infinite wisdom google didn't make these values powers of two for masking
    92  const (
    93  	// AllDaysMask is a bitmask of all the days of the week.
    94  	AllDaysMask = 1<<uint(time.Sunday) | 1<<uint(time.Monday) | 1<<uint(time.Tuesday) | 1<<uint(time.Wednesday) | 1<<uint(time.Thursday) | 1<<uint(time.Friday) | 1<<uint(time.Saturday)
    95  	// WeekDaysMask is a bitmask of all the weekdays of the week.
    96  	WeekDaysMask = 1<<uint(time.Monday) | 1<<uint(time.Tuesday) | 1<<uint(time.Wednesday) | 1<<uint(time.Thursday) | 1<<uint(time.Friday)
    97  	//WeekendDaysMask is a bitmask of the weekend days of the week.
    98  	WeekendDaysMask = 1<<uint(time.Sunday) | 1<<uint(time.Saturday)
    99  )
   100  
   101  // IsWeekDay returns if the day is a monday->friday.
   102  func IsWeekDay(day time.Weekday) bool {
   103  	return !IsWeekendDay(day)
   104  }
   105  
   106  // IsWeekendDay returns if the day is a monday->friday.
   107  func IsWeekendDay(day time.Weekday) bool {
   108  	return day == time.Saturday || day == time.Sunday
   109  }
   110  
   111  // ConstBool returns a value provider for a constant.
   112  func ConstBool(value bool) func() bool {
   113  	return func() bool {
   114  		return value
   115  	}
   116  }
   117  
   118  // ConstInt returns a value provider for a constant.
   119  func ConstInt(value int) func() int {
   120  	return func() int {
   121  		return value
   122  	}
   123  }
   124  
   125  // ConstDuration returns a value provider for a constant.
   126  func ConstDuration(value time.Duration) func() time.Duration {
   127  	return func() time.Duration {
   128  		return value
   129  	}
   130  }
   131  
   132  // ConstLabels returns a value provider for a constant.
   133  func ConstLabels(labels map[string]string) func() map[string]string {
   134  	return func() map[string]string {
   135  		return labels
   136  	}
   137  }