| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 1 | // Copyright (C) 2014 Google Inc. All Rights Reserved. |
| 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.gitiles; |
| 16 | |
| David Pursehouse | a8b331e | 2019-06-05 15:08:39 +0900 | [diff] [blame] | 17 | import com.google.common.annotations.VisibleForTesting; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.text.DateFormat; |
| 20 | import java.text.SimpleDateFormat; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 21 | import java.util.Optional; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 22 | import java.util.TimeZone; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 23 | import org.eclipse.jgit.lib.PersonIdent; |
| 24 | import org.eclipse.jgit.util.SystemReader; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 25 | |
| 26 | /** Date formatter similar in spirit to JGit's {@code GitDateFormatter}. */ |
| 27 | public class DateFormatter { |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 28 | public enum Format { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 29 | // Format strings should match org.eclipse.jgit.util.GitDateFormatter except |
| 30 | // for the timezone suffix. |
| 31 | DEFAULT("EEE MMM dd HH:mm:ss yyyy"), |
| 32 | ISO("yyyy-MM-dd HH:mm:ss"); |
| 33 | |
| 34 | private final String fmt; |
| 35 | private final ThreadLocal<DateFormat> defaultFormat; |
| 36 | private final ThreadLocal<DateFormat> fixedTzFormat; |
| 37 | |
| David Pursehouse | e3d3ec8 | 2016-06-15 22:10:48 +0900 | [diff] [blame] | 38 | Format(String fmt) { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 39 | this.fmt = fmt; |
| 40 | this.defaultFormat = new ThreadLocal<>(); |
| 41 | this.fixedTzFormat = new ThreadLocal<>(); |
| 42 | } |
| 43 | |
| 44 | private DateFormat getDateFormat(Optional<TimeZone> fixedTz) { |
| 45 | DateFormat df; |
| 46 | if (fixedTz.isPresent()) { |
| 47 | df = fixedTzFormat.get(); |
| 48 | if (df == null) { |
| 49 | df = new SimpleDateFormat(fmt); |
| 50 | fixedTzFormat.set(df); |
| 51 | } |
| 52 | df.setTimeZone(fixedTz.get()); |
| 53 | } else { |
| 54 | df = defaultFormat.get(); |
| 55 | if (df == null) { |
| 56 | df = new SimpleDateFormat(fmt + " Z"); |
| 57 | defaultFormat.set(df); |
| 58 | } |
| 59 | } |
| 60 | return df; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private final Optional<TimeZone> fixedTz; |
| 65 | private final Format format; |
| 66 | |
| David Pursehouse | a8b331e | 2019-06-05 15:08:39 +0900 | [diff] [blame] | 67 | @VisibleForTesting |
| 68 | protected DateFormatter(Optional<TimeZone> fixedTz, Format format) { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 69 | this.fixedTz = fixedTz; |
| 70 | this.format = format; |
| 71 | } |
| 72 | |
| 73 | public DateFormatter(GitilesAccess access, Format format) throws IOException { |
| 74 | this(ConfigUtil.getTimeZone(access.getConfig(), "gitiles", null, "fixedTimeZone"), format); |
| 75 | } |
| 76 | |
| 77 | public String format(PersonIdent ident) { |
| 78 | DateFormat df = format.getDateFormat(fixedTz); |
| 79 | if (!fixedTz.isPresent()) { |
| 80 | TimeZone tz = ident.getTimeZone(); |
| 81 | if (tz == null) { |
| 82 | tz = SystemReader.getInstance().getTimeZone(); |
| 83 | } |
| 84 | df.setTimeZone(tz); |
| 85 | } |
| 86 | return df.format(ident.getWhen()); |
| 87 | } |
| 88 | } |