1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model;
20
21 import java.time.Instant;
22 import java.time.ZoneId;
23 import java.time.format.DateTimeFormatter;
24 import java.util.Map;
25 import java.util.Properties;
26
27 import org.apache.maven.api.Constants;
28 import org.apache.maven.api.MonotonicClock;
29
30
31
32
33 public class MavenBuildTimestamp {
34
35 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ssXXX";
36
37 private final String formattedTimestamp;
38
39 public MavenBuildTimestamp() {
40 this(MonotonicClock.now());
41 }
42
43 public MavenBuildTimestamp(Instant time) {
44 this(time, DEFAULT_BUILD_TIMESTAMP_FORMAT);
45 }
46
47 public MavenBuildTimestamp(Instant time, Map<String, String> properties) {
48 this(time, properties != null ? properties.get(Constants.MAVEN_BUILD_TIMESTAMP_FORMAT) : null);
49 }
50
51
52
53
54
55
56 @Deprecated
57 public MavenBuildTimestamp(Instant time, Properties properties) {
58 this(time, properties != null ? properties.getProperty(Constants.MAVEN_BUILD_TIMESTAMP_FORMAT) : null);
59 }
60
61 public MavenBuildTimestamp(Instant time, String timestampFormat) {
62 if (timestampFormat == null) {
63 timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT;
64 }
65 if (time == null) {
66 time = MonotonicClock.now();
67 }
68 DateTimeFormatter formatter =
69 DateTimeFormatter.ofPattern(timestampFormat).withZone(ZoneId.of("UTC"));
70 formattedTimestamp = formatter.format(time);
71 }
72
73 public String formattedTimestamp() {
74 return formattedTimestamp;
75 }
76 }