LCOV - code coverage report
Current view: top level - server/notedb - CommentTimestampAdapter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 19 89.5 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.notedb;
      16             : 
      17             : import static java.time.format.DateTimeFormatter.ISO_INSTANT;
      18             : 
      19             : import com.google.common.annotations.VisibleForTesting;
      20             : import com.google.gson.TypeAdapter;
      21             : import com.google.gson.stream.JsonReader;
      22             : import com.google.gson.stream.JsonWriter;
      23             : import java.io.IOException;
      24             : import java.sql.Timestamp;
      25             : import java.time.Instant;
      26             : import java.time.LocalDateTime;
      27             : import java.time.ZoneId;
      28             : import java.time.format.DateTimeFormatter;
      29             : import java.time.format.DateTimeParseException;
      30             : import java.time.format.FormatStyle;
      31             : import java.util.Locale;
      32             : 
      33             : /**
      34             :  * Adapter that reads/writes {@link Timestamp}s as ISO 8601 instant in UTC.
      35             :  *
      36             :  * <p>This adapter reads and writes the ISO 8601 UTC instant format, {@code "2015-06-22T17:11:00Z"}.
      37             :  * This format is specially chosen because it is also readable by the default Gson type adapter,
      38             :  * despite the fact that the default adapter writes in a different format lacking timezones, {@code
      39             :  * "Jun 22, 2015 10:11:00 AM"}. Unlike the default adapter format, this representation is not
      40             :  * ambiguous during the transition away from DST.
      41             :  *
      42             :  * <p>This adapter is mutually compatible with the old adapter: the old adapter is able to read the
      43             :  * UTC instant format, and this adapter can fall back to parsing the old format.
      44             :  *
      45             :  * <p>Older Gson versions are not able to parse milliseconds out of ISO 8601 instants, so this
      46             :  * implementation truncates to seconds when writing. This is no worse than the truncation that
      47             :  * happens to fit NoteDb timestamps into git commit formatting.
      48             :  */
      49         153 : class CommentTimestampAdapter extends TypeAdapter<Timestamp> {
      50         153 :   private static final DateTimeFormatter FALLBACK =
      51         153 :       DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
      52             : 
      53             :   /**
      54             :    * Fixed format to parse date/time in the "Feb 7, 2017 2:20:30 AM" format
      55             :    *
      56             :    * <p>Some old comments (created in Jan-Feb 2017) can be stored in legacy format, which can't be
      57             :    * parsed with {@link #FALLBACK} formatter if the system/default locale has been changed. We will
      58             :    * try to parse with a fixed format if {@link #FALLBACK} doesn't work.
      59             :    */
      60         153 :   private static final DateTimeFormatter FIXED_FORMAT_FALLBACK =
      61         153 :       DateTimeFormatter.ofPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.US);
      62             : 
      63             :   @Override
      64             :   public void write(JsonWriter out, Timestamp ts) throws IOException {
      65          29 :     Timestamp truncated = new Timestamp(ts.getTime() / 1000 * 1000);
      66          29 :     out.value(ISO_INSTANT.format(truncated.toInstant()));
      67          29 :   }
      68             : 
      69             :   @Override
      70             :   public Timestamp read(JsonReader in) throws IOException {
      71          29 :     String str = in.nextString();
      72             :     try {
      73          29 :       return Timestamp.from(Instant.from(ISO_INSTANT.parse(str)));
      74           1 :     } catch (DateTimeParseException e) {
      75             :       try {
      76           1 :         return parseDateTimeWithDefaultLocaleFormat(str);
      77           0 :       } catch (DateTimeParseException e2) {
      78           0 :         return parseDateTimeWithFixedFormat(str);
      79             :       }
      80             :     }
      81             :   }
      82             : 
      83             :   public static Timestamp parseDateTimeWithDefaultLocaleFormat(String str) {
      84           1 :     return Timestamp.from(
      85           1 :         Instant.from(LocalDateTime.from(FALLBACK.parse(str)).atZone(ZoneId.systemDefault())));
      86             :   }
      87             : 
      88             :   @VisibleForTesting
      89             :   public static Timestamp parseDateTimeWithFixedFormat(String str) {
      90           1 :     return Timestamp.from(
      91           1 :         Instant.from(
      92           1 :             LocalDateTime.from(FIXED_FORMAT_FALLBACK.parse(str)).atZone(ZoneId.systemDefault())));
      93             :   }
      94             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750