blob: 69e8a86468598c0fd61b8e6eb96a9eb5a66b87c9 [file] [log] [blame]
Dave Borowitz2b2f34b2014-04-29 16:47:20 -07001// 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
15package com.google.gitiles;
16
David Pursehousea8b331e2019-06-05 15:08:39 +090017import com.google.common.annotations.VisibleForTesting;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070018import java.io.IOException;
19import java.text.DateFormat;
20import java.text.SimpleDateFormat;
David Pursehouse7a7f5472016-10-14 09:59:20 +090021import java.util.Optional;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070022import java.util.TimeZone;
Dave Borowitz3b744b12016-08-19 16:11:10 -040023import org.eclipse.jgit.lib.PersonIdent;
24import org.eclipse.jgit.util.SystemReader;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070025
26/** Date formatter similar in spirit to JGit's {@code GitDateFormatter}. */
27public class DateFormatter {
David Pursehousee3d3ec82016-06-15 22:10:48 +090028 public enum Format {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070029 // 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 Pursehousee3d3ec82016-06-15 22:10:48 +090038 Format(String fmt) {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070039 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 Pursehousea8b331e2019-06-05 15:08:39 +090067 @VisibleForTesting
68 protected DateFormatter(Optional<TimeZone> fixedTz, Format format) {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070069 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}