1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.util;
45
46 import java.text.DateFormat;
47 import java.text.SimpleDateFormat;
48 import java.util.Locale;
49 import java.util.TimeZone;
50
51 import org.eclipse.jgit.lib.PersonIdent;
52
53
54
55
56
57
58
59 public class GitDateFormatter {
60
61 private DateFormat dateTimeInstance;
62
63 private DateFormat dateTimeInstance2;
64
65 private final Format format;
66
67
68
69
70 static public enum Format {
71
72
73
74
75 DEFAULT,
76
77
78
79
80 RELATIVE,
81
82
83
84
85 LOCAL,
86
87
88
89
90 ISO,
91
92
93
94
95 RFC,
96
97
98
99
100 SHORT,
101
102
103
104
105 RAW,
106
107
108
109
110 LOCALE,
111
112
113
114
115 LOCALELOCAL
116 }
117
118
119
120
121
122
123 public GitDateFormatter(Format format) {
124 this.format = format;
125 switch (format) {
126 default:
127 break;
128 case DEFAULT:
129 dateTimeInstance = new SimpleDateFormat(
130 "EEE MMM dd HH:mm:ss yyyy Z", Locale.US);
131 break;
132 case ISO:
133 dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z",
134 Locale.US);
135 break;
136 case LOCAL:
137 dateTimeInstance = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",
138 Locale.US);
139 break;
140 case RFC:
141 dateTimeInstance = new SimpleDateFormat(
142 "EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
143 break;
144 case SHORT:
145 dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
146 break;
147 case LOCALE:
148 case LOCALELOCAL:
149 SystemReader systemReader = SystemReader.getInstance();
150 dateTimeInstance = systemReader.getDateTimeInstance(
151 DateFormat.DEFAULT, DateFormat.DEFAULT);
152 dateTimeInstance2 = systemReader.getSimpleDateFormat("Z");
153 break;
154 }
155 }
156
157
158
159
160
161
162
163
164 @SuppressWarnings("boxing")
165 public String formatDate(PersonIdent ident) {
166 switch (format) {
167 case RAW:
168 int offset = ident.getTimeZoneOffset();
169 String sign = offset < 0 ? "-" : "+";
170 int offset2;
171 if (offset < 0)
172 offset2 = -offset;
173 else
174 offset2 = offset;
175 int hours = offset2 / 60;
176 int minutes = offset2 % 60;
177 return String.format("%d %s%02d%02d",
178 ident.getWhen().getTime() / 1000, sign, hours, minutes);
179 case RELATIVE:
180 return RelativeDateFormatter.format(ident.getWhen());
181 case LOCALELOCAL:
182 case LOCAL:
183 dateTimeInstance.setTimeZone(SystemReader.getInstance()
184 .getTimeZone());
185 return dateTimeInstance.format(ident.getWhen());
186 case LOCALE:
187 TimeZone tz = ident.getTimeZone();
188 if (tz == null)
189 tz = SystemReader.getInstance().getTimeZone();
190 dateTimeInstance.setTimeZone(tz);
191 dateTimeInstance2.setTimeZone(tz);
192 return dateTimeInstance.format(ident.getWhen()) + " "
193 + dateTimeInstance2.format(ident.getWhen());
194 default:
195 tz = ident.getTimeZone();
196 if (tz == null)
197 tz = SystemReader.getInstance().getTimeZone();
198 dateTimeInstance.setTimeZone(ident.getTimeZone());
199 return dateTimeInstance.format(ident.getWhen());
200 }
201 }
202 }