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