blob: b14483f507c9208c283c3df8c7262dbfa4b76fdc [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
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070017import java.io.IOException;
18import java.text.DateFormat;
19import java.text.SimpleDateFormat;
David Pursehouse7a7f5472016-10-14 09:59:20 +090020import java.util.Optional;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070021import java.util.TimeZone;
Dave Borowitz3b744b12016-08-19 16:11:10 -040022import org.eclipse.jgit.lib.PersonIdent;
23import org.eclipse.jgit.util.SystemReader;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070024
25/** Date formatter similar in spirit to JGit's {@code GitDateFormatter}. */
26public class DateFormatter {
David Pursehousee3d3ec82016-06-15 22:10:48 +090027 public enum Format {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070028 // 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 Pursehousee3d3ec82016-06-15 22:10:48 +090037 Format(String fmt) {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070038 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}