View Javadoc
1   /*
2    * Copyright (C) 2011, 2012 Robin Rosenberg and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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   * A utility for formatting dates according to the Git log.date formats plus
22   * extensions.
23   * <p>
24   * The enum {@link org.eclipse.jgit.util.GitDateFormatter.Format} defines the
25   * available types.
26   */
27  public class GitDateFormatter {
28  
29  	private DateFormat dateTimeInstance;
30  
31  	private DateFormat dateTimeInstance2;
32  
33  	private final Format format;
34  
35  	/**
36  	 * Git and JGit formats
37  	 */
38  	public enum Format {
39  
40  		/**
41  		 * Git format: Time and original time zone
42  		 */
43  		DEFAULT,
44  
45  		/**
46  		 * Git format: Relative time stamp
47  		 */
48  		RELATIVE,
49  
50  		/**
51  		 * Git format: Date and time in local time zone
52  		 */
53  		LOCAL,
54  
55  		/**
56  		 * Git format: ISO 8601 plus time zone
57  		 */
58  		ISO,
59  
60  		/**
61  		 * Git formt: RFC 2822 plus time zone
62  		 */
63  		RFC,
64  
65  		/**
66  		 * Git format: YYYY-MM-DD
67  		 */
68  		SHORT,
69  
70  		/**
71  		 * Git format: Seconds size 1970 in UTC plus time zone
72  		 */
73  		RAW,
74  
75  		/**
76  		 * Locale dependent formatting with original time zone
77  		 */
78  		LOCALE,
79  
80  		/**
81  		 * Locale dependent formatting in local time zone
82  		 */
83  		LOCALELOCAL
84  	}
85  
86  	/**
87  	 * Create a new Git oriented date formatter
88  	 *
89  	 * @param format
90  	 *            a {@link org.eclipse.jgit.util.GitDateFormatter.Format}
91  	 *            object.
92  	 */
93  	public GitDateFormatter(Format format) {
94  		this.format = format;
95  		switch (format) {
96  		default:
97  			break;
98  		case DEFAULT: // Not default:
99  			dateTimeInstance = new SimpleDateFormat(
100 					"EEE MMM dd HH:mm:ss yyyy Z", Locale.US); //$NON-NLS-1$
101 			break;
102 		case ISO:
103 			dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", //$NON-NLS-1$
104 					Locale.US);
105 			break;
106 		case LOCAL:
107 			dateTimeInstance = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", //$NON-NLS-1$
108 					Locale.US);
109 			break;
110 		case RFC:
111 			dateTimeInstance = new SimpleDateFormat(
112 					"EEE, dd MMM yyyy HH:mm:ss Z", Locale.US); //$NON-NLS-1$
113 			break;
114 		case SHORT:
115 			dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd", Locale.US); //$NON-NLS-1$
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"); //$NON-NLS-1$
123 			break;
124 		}
125 	}
126 
127 	/**
128 	 * Format committer, author or tagger ident according to this formatter's
129 	 * specification.
130 	 *
131 	 * @param ident
132 	 *            a {@link org.eclipse.jgit.lib.PersonIdent} object.
133 	 * @return formatted version of date, time and time zone
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 ? "-" : "+"; //$NON-NLS-1$ //$NON-NLS-2$
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", //$NON-NLS-1$
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()) + " " //$NON-NLS-1$
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 }