View Javadoc
1   /*
2    * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.util;
44  
45  import java.text.MessageFormat;
46  import java.util.Date;
47  
48  import org.eclipse.jgit.internal.JGitText;
49  
50  /**
51   * Formatter to format timestamps relative to the current time using time units
52   * in the format defined by {@code git log --relative-date}.
53   */
54  public class RelativeDateFormatter {
55  	final static long SECOND_IN_MILLIS = 1000;
56  
57  	final static long MINUTE_IN_MILLIS = 60 * SECOND_IN_MILLIS;
58  
59  	final static long HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS;
60  
61  	final static long DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;
62  
63  	final static long WEEK_IN_MILLIS = 7 * DAY_IN_MILLIS;
64  
65  	final static long MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS;
66  
67  	final static long YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS;
68  
69  	/**
70  	 * Get age of given {@link java.util.Date} compared to now formatted in the
71  	 * same relative format as returned by {@code git log --relative-date}
72  	 *
73  	 * @param when
74  	 *            {@link java.util.Date} to format
75  	 * @return age of given {@link java.util.Date} compared to now formatted in
76  	 *         the same relative format as returned by
77  	 *         {@code git log --relative-date}
78  	 */
79  	@SuppressWarnings("boxing")
80  	public static String format(Date when) {
81  
82  		long ageMillis = SystemReader.getInstance().getCurrentTime()
83  				- when.getTime();
84  
85  		// shouldn't happen in a perfect world
86  		if (ageMillis < 0)
87  			return JGitText.get().inTheFuture;
88  
89  		// seconds
90  		if (ageMillis < upperLimit(MINUTE_IN_MILLIS))
91  			return MessageFormat.format(JGitText.get().secondsAgo,
92  					round(ageMillis, SECOND_IN_MILLIS));
93  
94  		// minutes
95  		if (ageMillis < upperLimit(HOUR_IN_MILLIS))
96  			return MessageFormat.format(JGitText.get().minutesAgo,
97  					round(ageMillis, MINUTE_IN_MILLIS));
98  
99  		// hours
100 		if (ageMillis < upperLimit(DAY_IN_MILLIS))
101 			return MessageFormat.format(JGitText.get().hoursAgo,
102 					round(ageMillis, HOUR_IN_MILLIS));
103 
104 		// up to 14 days use days
105 		if (ageMillis < 14 * DAY_IN_MILLIS)
106 			return MessageFormat.format(JGitText.get().daysAgo,
107 					round(ageMillis, DAY_IN_MILLIS));
108 
109 		// up to 10 weeks use weeks
110 		if (ageMillis < 10 * WEEK_IN_MILLIS)
111 			return MessageFormat.format(JGitText.get().weeksAgo,
112 					round(ageMillis, WEEK_IN_MILLIS));
113 
114 		// months
115 		if (ageMillis < YEAR_IN_MILLIS)
116 			return MessageFormat.format(JGitText.get().monthsAgo,
117 					round(ageMillis, MONTH_IN_MILLIS));
118 
119 		// up to 5 years use "year, months" rounded to months
120 		if (ageMillis < 5 * YEAR_IN_MILLIS) {
121 			long years = round(ageMillis, MONTH_IN_MILLIS) / 12;
122 			String yearLabel = (years > 1) ? JGitText.get().years : //
123 					JGitText.get().year;
124 			long months = round(ageMillis - years * YEAR_IN_MILLIS,
125 					MONTH_IN_MILLIS);
126 			String monthLabel = (months > 1) ? JGitText.get().months : //
127 					(months == 1 ? JGitText.get().month : ""); //$NON-NLS-1$
128 			return MessageFormat.format(
129 					months == 0 ? JGitText.get().years0MonthsAgo : JGitText
130 							.get().yearsMonthsAgo,
131 					new Object[] { years, yearLabel, months, monthLabel });
132 		}
133 
134 		// years
135 		return MessageFormat.format(JGitText.get().yearsAgo,
136 				round(ageMillis, YEAR_IN_MILLIS));
137 	}
138 
139 	private static long upperLimit(long unit) {
140 		long limit = unit + unit / 2;
141 		return limit;
142 	}
143 
144 	private static long round(long n, long unit) {
145 		long rounded = (n + unit / 2) / unit;
146 		return rounded;
147 	}
148 }