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  	 * @param when
71  	 *            {@link Date} to format
72  	 * @return age of given {@link Date} compared to now formatted in the same
73  	 *         relative format as returned by {@code git log --relative-date}
74  	 */
75  	@SuppressWarnings("boxing")
76  	public static String format(Date when) {
77  
78  		long ageMillis = SystemReader.getInstance().getCurrentTime()
79  				- when.getTime();
80  
81  		// shouldn't happen in a perfect world
82  		if (ageMillis < 0)
83  			return JGitText.get().inTheFuture;
84  
85  		// seconds
86  		if (ageMillis < upperLimit(MINUTE_IN_MILLIS))
87  			return MessageFormat.format(JGitText.get().secondsAgo,
88  					round(ageMillis, SECOND_IN_MILLIS));
89  
90  		// minutes
91  		if (ageMillis < upperLimit(HOUR_IN_MILLIS))
92  			return MessageFormat.format(JGitText.get().minutesAgo,
93  					round(ageMillis, MINUTE_IN_MILLIS));
94  
95  		// hours
96  		if (ageMillis < upperLimit(DAY_IN_MILLIS))
97  			return MessageFormat.format(JGitText.get().hoursAgo,
98  					round(ageMillis, HOUR_IN_MILLIS));
99  
100 		// up to 14 days use days
101 		if (ageMillis < 14 * DAY_IN_MILLIS)
102 			return MessageFormat.format(JGitText.get().daysAgo,
103 					round(ageMillis, DAY_IN_MILLIS));
104 
105 		// up to 10 weeks use weeks
106 		if (ageMillis < 10 * WEEK_IN_MILLIS)
107 			return MessageFormat.format(JGitText.get().weeksAgo,
108 					round(ageMillis, WEEK_IN_MILLIS));
109 
110 		// months
111 		if (ageMillis < YEAR_IN_MILLIS)
112 			return MessageFormat.format(JGitText.get().monthsAgo,
113 					round(ageMillis, MONTH_IN_MILLIS));
114 
115 		// up to 5 years use "year, months" rounded to months
116 		if (ageMillis < 5 * YEAR_IN_MILLIS) {
117 			long years = ageMillis / YEAR_IN_MILLIS;
118 			String yearLabel = (years > 1) ? JGitText.get().years : //
119 					JGitText.get().year;
120 			long months = round(ageMillis % YEAR_IN_MILLIS, MONTH_IN_MILLIS);
121 			String monthLabel = (months > 1) ? JGitText.get().months : //
122 					(months == 1 ? JGitText.get().month : ""); //$NON-NLS-1$
123 			return MessageFormat.format(
124 					months == 0 ? JGitText.get().years0MonthsAgo : JGitText
125 							.get().yearsMonthsAgo,
126 					new Object[] { years, yearLabel, months, monthLabel });
127 		}
128 
129 		// years
130 		return MessageFormat.format(JGitText.get().yearsAgo,
131 				round(ageMillis, YEAR_IN_MILLIS));
132 	}
133 
134 	private static long upperLimit(long unit) {
135 		long limit = unit + unit / 2;
136 		return limit;
137 	}
138 
139 	private static long round(long n, long unit) {
140 		long rounded = (n + unit / 2) / unit;
141 		return rounded;
142 	}
143 }