1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package timeutil
6
7import "time"
8
9// Clock abstracts time retrieval so callers can substitute deterministic values
10// during testing.
11type Clock interface {
12 Now() time.Time
13}
14
15// UTCClock returns the system time in UTC.
16type UTCClock struct{}
17
18// Now implements Clock.
19func (UTCClock) Now() time.Time {
20 return time.Now().UTC()
21}
22
23// FixedClock always reports the same instant.
24type FixedClock struct {
25 Time time.Time
26}
27
28// Now implements Clock.
29func (f FixedClock) Now() time.Time {
30 if f.Time.IsZero() {
31 return time.Time{}
32 }
33 return f.Time
34}
35
36// EnsureUTC normalises t to UTC when it has a location set.
37func EnsureUTC(t time.Time) time.Time {
38 if t.IsZero() {
39 return t
40 }
41 return t.UTC()
42}