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