1 // Copyright 2016 Google LLC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package util provides general utility functions for the CT personality. 16 package util 17 18 import "time" 19 20 // TimeSource can provide the current time, or be replaced by a mock in tests to return 21 // specific values. 22 type TimeSource interface { 23 // Now returns the current time in real implementations or a suitable value in others 24 Now() time.Time 25 } 26 27 // SystemTimeSource provides the current system local time 28 type SystemTimeSource struct{} 29 30 // Now returns the true current local time. 31 func (s SystemTimeSource) Now() time.Time { 32 return time.Now() 33 } 34 35 // FixedTimeSource provides a fixed time for use in tests. 36 // It should not be used in production code. 37 type FixedTimeSource struct { 38 fakeTime time.Time 39 } 40 41 // NewFixedTimeSource creates a FixedTimeSource instance 42 func NewFixedTimeSource(t time.Time) *FixedTimeSource { 43 return &FixedTimeSource{fakeTime: t} 44 } 45 46 // Now returns the time value this instance contains 47 func (f *FixedTimeSource) Now() time.Time { 48 return f.fakeTime 49 } 50