-
Notifications
You must be signed in to change notification settings - Fork 20
/
TimeProvider.java
131 lines (115 loc) · 3.27 KB
/
TimeProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/
package sirius.kernel.commons;
import sirius.kernel.Sirius;
import sirius.kernel.di.std.Register;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* Wrapper for static time functions which can be injected using a {@link sirius.kernel.di.std.Part} annotation.
* <p>
* Using this wrapper instead of the static methods permits to use mocking in tests.
*/
@Register(classes = TimeProvider.class)
public class TimeProvider {
private Clock clock = Clock.systemDefaultZone();
private Clock utcClock = Clock.systemDefaultZone();
/**
* Wrapper for {@link LocalDateTime#now()}.
*
* @return the current time as {@link LocalDateTime}.
*/
public LocalDateTime localDateTimeNow() {
return LocalDateTime.now(clock);
}
/**
* Wrapper for {@link LocalDate#now()}.
*
* @return the current time as {@link LocalDate}.
*/
public LocalDate localDateNow() {
return LocalDate.now(clock);
}
/**
* Wrapper for {@link LocalTime#now()}.
*
* @return the current time as {@link LocalTime}.
*/
public LocalTime localTimeNow() {
return LocalTime.now(clock);
}
/**
* Wrapper for {@link Instant#now()}.
*
* @return current time as {@link Instant}.
*/
public Instant instantNow() {
return Instant.now(utcClock);
}
/**
* Wrapper for {@link System#currentTimeMillis()}.
*
* @return the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
*/
public long currentTimeMillis() {
return System.currentTimeMillis();
}
/**
* Wrapper for {@link System#nanoTime()}.
*
* @return the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.
*/
public long nanoTime() {
return System.nanoTime();
}
/**
* Returns the current clock being used.
*
* @return the clock being used for the system default time zone
*/
public Clock getClock() {
return clock;
}
/**
* Specifies the clock to use for the system default time zone.
* <p>
* NOTE: This must only be used in test environments.
*
* @param clock the clock to use
*/
public void setClock(Clock clock) {
if (!Sirius.isStartedAsTest()) {
throw new IllegalStateException("Cannot change the clock in production systems");
}
this.clock = clock;
}
/**
* Returns the clock for UTC.
*
* @return the clock with UTC as time zone.
*/
public Clock getUTCClock() {
return utcClock;
}
/**
* Specifies the clock to use for the UTC time zone.
* <p>
* NOTE: This must only be used in test environments.
*
* @param clock the clock to use
*/
public void setUTCClock(Clock clock) {
if (!Sirius.isStartedAsTest()) {
throw new IllegalStateException("Cannot change the clock in production systems");
}
this.utcClock = clock;
}
}