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.text.SimpleDateFormat;
22 import java.time.Instant;
23 import java.util.Date;
24 import java.util.GregorianCalendar;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.TimeZone;
28
29 import org.apache.maven.api.Constants;
30
31
32
33
34 public class MavenBuildTimestamp {
35
36 public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ssXXX";
37
38 public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
39
40 private final String formattedTimestamp;
41
42 public MavenBuildTimestamp() {
43 this(Instant.now());
44 }
45
46 public MavenBuildTimestamp(Instant time) {
47 this(time, DEFAULT_BUILD_TIMESTAMP_FORMAT);
48 }
49
50 public MavenBuildTimestamp(Instant time, Map<String, String> properties) {
51 this(time, properties != null ? properties.get(Constants.MAVEN_BUILD_TIMESTAMP_FORMAT) : null);
52 }
53
54
55
56
57
58
59 @Deprecated
60 public MavenBuildTimestamp(Instant time, Properties properties) {
61 this(time, properties != null ? properties.getProperty(Constants.MAVEN_BUILD_TIMESTAMP_FORMAT) : null);
62 }
63
64 public MavenBuildTimestamp(Instant time, String timestampFormat) {
65 if (timestampFormat == null) {
66 timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT;
67 }
68 if (time == null) {
69 time = Instant.now();
70 }
71 SimpleDateFormat dateFormat = new SimpleDateFormat(timestampFormat);
72 dateFormat.setCalendar(new GregorianCalendar());
73 dateFormat.setTimeZone(DEFAULT_BUILD_TIME_ZONE);
74 formattedTimestamp = dateFormat.format(new Date(time.toEpochMilli()));
75 }
76
77 public String formattedTimestamp() {
78 return formattedTimestamp;
79 }
80 }