| // Copyright 2015 The Vanadium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // Clock utility functions. |
| // errorThreshold is kept at 2 seconds because there is an error margin of |
| // half a second while fetching elapsed time. This means that the combined |
| // error for fetching two elapsed timestamps can be up to 1 second. |
| var errorThreshold time.Duration = 2 * time.Second |
| // hasClockChanged checks if the system clock was changed between two |
| // timestamp samples taken from system clock. |
| // t1 is the first timestamp sampled from clock. |
| // e1 is the elapsed time since boot sampled before t1 was sampled. |
| // t2 is the second timestamp sampled from clock (t2 sampled after t1) |
| // e2 is the elapsed time since boot sampled after t2 was sampled. |
| // Note: e1 must be sampled before t1 and e2 must be sampled after t2. |
| func HasSysClockChanged(t1, t2 time.Time, e1, e2 time.Duration) bool { |
| vlog.VI(2).Infof("clock: hasSysClockChanged: t2 is before t1, returning true") |
| // Since elapsed time has an error margin of +/- half a second, elapsedDiff |
| // can end up being smaller than tsDiff sometimes. Hence we take abs() of |
| return abs(elapsedDiff-tsDiff) > errorThreshold |
| func abs(d time.Duration) time.Duration { |