github.com/cilium/cilium@v1.16.2/pkg/time/time.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 // package time is a wrapper for the stdlib time library that aliases most 5 // underlying types, but allows overrides for testing purposes. 6 // 7 // Synced to go-1.20.7. 8 package time 9 10 import ( 11 "time" 12 ) 13 14 const ( 15 Layout = time.Layout 16 ANSIC = time.ANSIC 17 UnixDate = time.UnixDate 18 RubyDate = time.RubyDate 19 RFC822 = time.RFC822 20 RFC822Z = time.RFC822Z 21 RFC850 = time.RFC850 22 RFC1123 = time.RFC1123 23 RFC1123Z = time.RFC1123Z 24 RFC3339 = time.RFC3339 25 RFC3339Nano = time.RFC3339Nano 26 Kitchen = time.Kitchen 27 Stamp = time.Stamp 28 StampMilli = time.StampMilli 29 StampMicro = time.StampMicro 30 StampNano = time.StampNano 31 DateTime = time.DateTime 32 DateOnly = time.DateOnly 33 TimeOnly = time.TimeOnly 34 35 Nanosecond = time.Nanosecond 36 Microsecond = time.Microsecond 37 Millisecond = time.Millisecond 38 Second = time.Second 39 Minute = time.Minute 40 Hour = time.Hour 41 ) 42 43 var ( 44 ParseDuration = time.ParseDuration 45 Since = time.Since 46 Until = time.Until 47 FixedZone = time.FixedZone 48 LoadLocation = time.LoadLocation 49 LoadLocationFromTZData = time.LoadLocationFromTZData 50 Date = time.Date 51 Now = time.Now 52 Parse = time.Parse 53 ParseInLocation = time.ParseInLocation 54 Unix = time.Unix 55 UnixMicro = time.UnixMicro 56 UnixMilli = time.UnixMilli 57 ) 58 59 type ( 60 Duration = time.Duration 61 Location = time.Location 62 Month = time.Month 63 ParseError = time.ParseError 64 Ticker = time.Ticker 65 Time = time.Time 66 Timer = time.Timer 67 Weekday = time.Weekday 68 ) 69 70 var ( 71 MaxInternalTimerDelay time.Duration 72 ) 73 74 // After overrides the stdlib time.After to enforce maximum sleepiness via 75 // option.MaxInternalTimerDelay. 76 func After(d Duration) <-chan Time { 77 if MaxInternalTimerDelay > 0 && d > MaxInternalTimerDelay { 78 d = MaxInternalTimerDelay 79 } 80 return time.After(d) 81 } 82 83 // Sleep overrides the stdlib time.Sleep to enforce maximum sleepiness via 84 // option.MaxInternalTimerDelay. 85 func Sleep(d time.Duration) { 86 if MaxInternalTimerDelay > 0 && d > MaxInternalTimerDelay { 87 d = MaxInternalTimerDelay 88 } 89 time.Sleep(d) 90 } 91 92 // Tick overrides the stdlib time.Tick to enforce maximum sleepiness via 93 // option.MaxInternalTimerDelay. 94 func Tick(d Duration) <-chan time.Time { 95 return NewTicker(d).C 96 } 97 98 // NewTicker overrides the stdlib time.NewTicker to enforce maximum sleepiness 99 // via option.MaxInternalTimerDelay. 100 func NewTicker(d Duration) *time.Ticker { 101 if MaxInternalTimerDelay > 0 && d > MaxInternalTimerDelay { 102 d = MaxInternalTimerDelay 103 } 104 return time.NewTicker(d) 105 } 106 107 // NewTimer overrides the stdlib time.NewTimer to enforce maximum sleepiness 108 // via option.MaxInternalTimerDelay. 109 func NewTimer(d Duration) *time.Timer { 110 if MaxInternalTimerDelay > 0 && d > MaxInternalTimerDelay { 111 d = MaxInternalTimerDelay 112 } 113 return time.NewTimer(d) 114 } 115 116 // NewTimerWithoutMaxDelay returns a time.NewTimer without enforcing maximum 117 // sleepiness. This function should only be used in cases where the timer firing 118 // early impacts correctness. If in doubt, you probably should use NewTimer. 119 func NewTimerWithoutMaxDelay(d Duration) *time.Timer { 120 return time.NewTimer(d) 121 } 122 123 // AfterFunc overrides the stdlib time.AfterFunc to enforce maximum sleepiness 124 // via option.MaxInternalTimerDelay. 125 func AfterFunc(d Duration, f func()) *time.Timer { 126 if MaxInternalTimerDelay > 0 && d > MaxInternalTimerDelay { 127 d = MaxInternalTimerDelay 128 } 129 return time.AfterFunc(d, f) 130 }