View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.util;
26  
27  import java.time.Instant;
28  import java.time.LocalDate;
29  import java.time.LocalDateTime;
30  import java.time.OffsetDateTime;
31  import java.time.ZoneOffset;
32  import java.time.ZonedDateTime;
33  import java.time.format.DateTimeFormatter;
34  import java.time.format.DateTimeParseException;
35  
36  import org.eluder.coveralls.maven.plugin.ProcessingException;
37  
38  /**
39   * The Class TimestampParser.
40   */
41  public class TimestampParser {
42  
43      /** The Constant EPOCH_MILLIS. */
44      public static final String EPOCH_MILLIS = "EpochMillis";
45  
46      /** The Constant DEFAULT_FORMAT. */
47      public static final String DEFAULT_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
48  
49      /** The parser. */
50      private final Parser parser;
51  
52      /**
53       * Instantiates a new timestamp parser.
54       *
55       * @param format
56       *            the format
57       */
58      public TimestampParser(final String format) {
59          try {
60              if (TimestampParser.EPOCH_MILLIS.equalsIgnoreCase(format)) {
61                  this.parser = new EpochMillisParser();
62              } else if (format != null && !format.isBlank()) {
63                  this.parser = new DateTimeFormatterParser(format);
64              } else {
65                  this.parser = new DateTimeFormatterParser(TimestampParser.DEFAULT_FORMAT);
66              }
67          } catch (final IllegalArgumentException e) {
68              throw new IllegalArgumentException("Invalid timestamp format \"" + format + "\"", e);
69          }
70      }
71  
72      /**
73       * Parses the.
74       *
75       * @param timestamp
76       *            the timestamp
77       *
78       * @return the instant
79       *
80       * @throws ProcessingException
81       *             the processing exception
82       */
83      public Instant parse(final String timestamp) throws ProcessingException {
84          if (timestamp == null || timestamp.isBlank()) {
85              return null;
86          }
87          try {
88              return this.parser.parse(timestamp);
89          } catch (final Exception e) {
90              throw new ProcessingException("Unable to parse timestamp \"" + timestamp + "\"", e);
91          }
92      }
93  
94      /**
95       * The Interface Parser.
96       */
97      private interface Parser {
98  
99          /**
100          * Parses the.
101          *
102          * @param timestamp
103          *            the timestamp
104          *
105          * @return the instant
106          *
107          * @throws DateTimeParseException
108          *             the date time parse exception
109          */
110         Instant parse(String timestamp) throws DateTimeParseException;
111     }
112 
113     /**
114      * The Class DateTimeFormatterParser.
115      */
116     private static class DateTimeFormatterParser implements Parser {
117 
118         /** The formatter. */
119         final DateTimeFormatter formatter;
120 
121         /** The has zone. */
122         final boolean hasZone;
123 
124         /** The date only. */
125         final boolean dateOnly;
126 
127         /**
128          * Instantiates a new date time formatter parser.
129          *
130          * @param format
131          *            the format
132          */
133         DateTimeFormatterParser(final String format) {
134             this.formatter = DateTimeFormatter.ofPattern(format);
135             this.hasZone = format.contains("X") || format.contains("z") || format.contains("Z");
136             this.dateOnly = !format.contains("H") && !format.contains("m") && !format.contains("s");
137         }
138 
139         @Override
140         public Instant parse(final String timestamp) throws DateTimeParseException {
141             if (this.hasZone) {
142                 try {
143                     return ZonedDateTime.parse(timestamp, this.formatter).toInstant();
144                 } catch (final DateTimeParseException ex) {
145                     return OffsetDateTime.parse(timestamp, this.formatter).toInstant();
146                 }
147             }
148             if (this.dateOnly) {
149                 // Parse as LocalDate and set time to midnight UTC
150                 return LocalDate.parse(timestamp, this.formatter).atStartOfDay(ZoneOffset.UTC).toInstant();
151             }
152             // Parse as LocalDateTime and assume UTC
153             return LocalDateTime.parse(timestamp, this.formatter).toInstant(ZoneOffset.UTC);
154         }
155     }
156 
157     /**
158      * The Class EpochMillisParser.
159      */
160     private static class EpochMillisParser implements Parser {
161 
162         @Override
163         public Instant parse(final String timestamp) {
164             return Instant.ofEpochMilli(Long.parseLong(timestamp));
165         }
166     }
167 }