You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Apache Wiki <wi...@apache.org> on 2006/05/11 16:03:36 UTC

[Jakarta-velocity Wiki] Trivial Update of "ExtendedDateTool" by ChristopherTownson

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-velocity Wiki" for change notification.

The following page has been changed by ChristopherTownson:
http://wiki.apache.org/jakarta-velocity/ExtendedDateTool

------------------------------------------------------------------------------
- = ExtendedDateTool =
  == Initial Rough Version ==
  ExtendedDateTool handles requirements for expressing a specified date relative to now as a string in a "friendly format" (e.g. "3 minutes ago", "tomorrow", "3 days from now")
  
@@ -35, +34 @@

  /**
   * Extension to Velocity DateTool which provides methods for expressing
   * Calendar objects in a human-friendly, relative format
-  * 
+  *
   * @author <a href="mailto:c.townson@nature.com">Christopher Townson</a>
-  * 
+  *
   * TODO externalize unit conversion methods to dedicated tool?
   * TODO better handling of strings/pluralisation/i8n?
   * TODO integration of properties with velocity.properties
@@ -45, +44 @@

   * 		is not suited to "friendly-format"
   */
  public class ExtendedDateTool extends DateTool
- {	
+ {
  	/**
  	 * number of milliseconds in a second
  	 */
  	public static final long MILLIS_TO_SECOND = 1000;
- 	
+ 
  	/**
  	 * number of millseconds in a minute
  	 */
  	public static final long MILLIS_TO_MINUTE = 60000;
- 	
+ 
  	/**
  	 * number of milliseconds in an hour
  	 */
  	public static final long MILLIS_TO_HOUR = 3600000;
- 	
+ 
  	/**
  	 * number of milliseconds in a day
  	 */
  	public static final long MILLIS_TO_DAY = 86400000;
- 	
+ 
  	/**
  	 * String to append to dates which are _after_ the current date
  	 */
- 	public static final String APPEND_FUTURE = 
+ 	public static final String APPEND_FUTURE =
  		Messages.getString("ExtendedDateTool.string.date.append.future");
- 	
- 	
+ 
+ 
  	/**
  	 * String to append to dates which are _before_ the current date
  	 */
- 	public static final String APPEND_PAST = 
+ 	public static final String APPEND_PAST =
  		Messages.getString("ExtendedDateTool.string.date.append.past");
- 	
+ 
  	/**
  	 * Strings for unit: seconds (singular)
  	 */
- 	public static final String UNIT_SECOND = 
+ 	public static final String UNIT_SECOND =
  		Messages.getString("ExtendedDateTool.string.unit.seconds.singular");
- 	
+ 
  	/**
  	 * String for unit seconds (plural)
  	 */
- 	public static final String UNIT_SECONDS = 
+ 	public static final String UNIT_SECONDS =
  		Messages.getString("ExtendedDateTool.string.unit.seconds.plural");
- 	
+ 
  	/**
  	 * String for unit: minutes (singular)
  	 */
- 	public static final String UNIT_MINUTE = 
+ 	public static final String UNIT_MINUTE =
  		Messages.getString("ExtendedDateTool.string.unit.minutes.singular");
- 	
+ 
  	/**
  	 * String for unit minutes (plural)
  	 */
- 	public static final String UNIT_MINUTES = 
+ 	public static final String UNIT_MINUTES =
  		Messages.getString("ExtendedDateTool.string.unit.minutes.plural");
- 	
+ 
  	/**
  	 * String for unit: minutes (singular)
  	 */
- 	public static final String UNIT_HOUR = 
+ 	public static final String UNIT_HOUR =
  		Messages.getString("ExtendedDateTool.string.unit.hours.singular");
- 	
+ 
  	/**
  	 * String for unit minutes (plural)
  	 */
- 	public static final String UNIT_HOURS = 
+ 	public static final String UNIT_HOURS =
  		Messages.getString("ExtendedDateTool.string.unit.hours.plural");
- 	
+ 
  	/**
  	 * String for unit: days (singular)
  	 */
- 	public static final String UNIT_DAY = 
+ 	public static final String UNIT_DAY =
  		Messages.getString("ExtendedDateTool.string.unit.days.singular");
- 	
+ 
  	/**
  	 * String for unit days (plural)
  	 */
- 	public static final String UNIT_DAYS = 
+ 	public static final String UNIT_DAYS =
  		Messages.getString("ExtendedDateTool.string.unit.days.plural");
- 	
+ 
  	/**
  	 * String for unit: 1 day into the future
  	 */
- 	public static final String YESTERDAY = 
+ 	public static final String YESTERDAY =
  		Messages.getString("ExtendedDateTool.string.date.yesterday");
- 	
+ 
  	/**
  	 * String for unit: 1 day into the past
  	 */
- 	public static final String TOMORROW = 
+ 	public static final String TOMORROW =
  		Messages.getString("ExtendedDateTool.string.date.tomorrow");
- 	
+ 
  	/**
  	 * String for unit: weeks (singular)
- 	 * 
+ 	 *
  	 * Not currently in use as any period over 7 days is better displayed in a
  	 * standard date format
  	 */
- 	public static final String UNIT_WEEK = 
+ 	public static final String UNIT_WEEK =
  		Messages.getString("ExtendedDateTool.string.unit.weeks.singular");
- 	
+ 
  	/**
  	 * String for unit: weeks (plural)
- 	 * 
+ 	 *
  	 * Currently only used to determine the fact that date should be
  	 * displayed in a standard format
  	 */
- 	public static final String UNIT_WEEKS = 
+ 	public static final String UNIT_WEEKS =
  		Messages.getString("ExtendedDateTool.string.unit.weeks.plural");
- 	
+ 
  	/**
  	 * Default output format for dates which are too distant to be suitable
  	 * for rendering in "friendly format"
  	 */
- 	public static final String DEFAULT_FORMAT = 
+ 	public static final String DEFAULT_FORMAT =
  		Messages.getString("ExtendedDateTOol.string.date.format.default");
  
  	/**
  	 * Returns a provided Date instance in a human-friendly String format
- 	 * 
+ 	 *
  	 * @param then The Calendar object to convert to human-friendly format
  	 * @return The date in a human-friendly format (e.g. 3 days ago)
  	 */
@@ -174, +173 @@

  	{
  		String friendlyDate = null;
  		String append = null;
- 		
+ 
  		// get the difference between the now and then as a long
  		long diff = getDifferenceBetweenNowAnd(then);
- 		
+ 
  		// is Calendar in the past or the future?
  		if (isPast(diff))
  		{
@@ -188, +187 @@

  		{
  			append = APPEND_FUTURE;
  		}
- 		
+ 
  		if (isSeconds(diff))
  		{
  			if (isSingular(diff))
  			{
- 				friendlyDate = convertMillisToSeconds(diff) + 
+ 				friendlyDate = convertMillisToSeconds(diff) +
  				" " + UNIT_SECOND + " " + append;
  			}
  			else
  			{
- 				friendlyDate = convertMillisToSeconds(diff) + 
+ 				friendlyDate = convertMillisToSeconds(diff) +
  				" " + UNIT_SECONDS + " " + append;
  			}
  		}
@@ -206, +205 @@

  		{
  			if (isSingular(diff))
  			{
- 				friendlyDate = convertMillisToMinutes(diff) + 
+ 				friendlyDate = convertMillisToMinutes(diff) +
  				" " + UNIT_MINUTE + " " + append;
  			}
  			else
  			{
- 				friendlyDate = convertMillisToMinutes(diff) + 
+ 				friendlyDate = convertMillisToMinutes(diff) +
  				" " + UNIT_MINUTES + " " + append;
  			}
  		}
@@ -224, +223 @@

  			}
  			else
  			{
- 				friendlyDate = convertMillisToHours(diff) + 
+ 				friendlyDate = convertMillisToHours(diff) +
  				" " + UNIT_HOURS + " " + append;
  			}
  		}
@@ -240, +239 @@

  			}
  			else
  			{
- 				friendlyDate = convertMillisToDays(diff) + 
+ 				friendlyDate = convertMillisToDays(diff) +
  				" " + UNIT_DAYS + " " + append;
  			}
  		}
@@ -248, +247 @@

  		{
  			friendlyDate = super.format(DEFAULT_FORMAT,then.getTime());
  		}
- 		
+ 
  		// return friendly string
  		return friendlyDate;
  	}
- 	
+ 
  	/**
  	 * Overloaded getFriendlyDate method for convenience from templates
- 	 * 
+ 	 *
  	 * NB. The date format descriptor only applies to the date as it will be
  	 * returned if it is <strong>not</strong> suitable for friendly format
- 	 * 
+ 	 *
  	 * @param format
  	 * @param obj
  	 * @return The date in a human-friendly format (e.g. 3 days ago)
@@ -266, +265 @@

  	public String getFriendlyDate(String format, Object obj) {
  		return getFriendlyDate(super.toCalendar(super.toDate(format,obj)));
  	}
- 	
+ 
  	/**
  	 * Returns a Java timestamp (milliseconds since 1970-01-01)
- 	 * 
+ 	 *
  	 * @return long java timestamp for the current date
  	 */
  	public long getTimeInMillis()
  	{
  		return Calendar.getInstance().getTimeInMillis();
  	}
- 	
+ 
  	/**
  	 * Returns a Java timestamp for the supplied Calendar
- 	 * 
+ 	 *
  	 * @param cal
  	 * @return long the Java timestamp for the supplied calendar
  	 */
@@ -287, +286 @@

  	{
  		return cal.getTimeInMillis();
  	}
- 	
+ 
  	/**
  	 * Returns the time difference between two points expressed in milliseconds
- 	 * 
+ 	 *
  	 * @param pointA
  	 * @param pointB
  	 * @return The time difference in milliseconds
@@ -299, +298 @@

  	{
  		return pointA.getTimeInMillis() - pointB.getTimeInMillis();
  	}
- 	
+ 
  	/**
  	 * Tests whether the difference between two Calendar instances is
  	 * a matter of seconds or not
- 	 * 
+ 	 *
  	 * @param then The calendar instance for comparison
  	 * @return The difference in milliseconds between now and supplied Calendar
  	 *          Returns a positive long if then is after now and a negative long
@@ -313, +312 @@

  	{
  		return getDifferenceBetween(then, Calendar.getInstance());
  	}
- 	
+ 
  	/**
  	 * COnvert a duration expressed in milliseconds to seconds
- 	 * 
+ 	 *
  	 * @param millis
  	 * @return milliseconds expressed as seconds
  	 */
@@ -324, +323 @@

  	{
  		return millis / MILLIS_TO_SECOND;
  	}
- 	
+ 
  	/**
  	 * Convert a duration expressed in milliseconds to minutes
- 	 * 
+ 	 *
  	 * @param millis
  	 * @return milliseconds expressed as minutes
  	 */
@@ -335, +334 @@

  	{
  		return millis / MILLIS_TO_MINUTE;
  	}
- 	
+ 
  	/**
  	 * Convert a duration expressed in milliseconds to hours
- 	 * 
+ 	 *
  	 * @param millis
  	 * @return milliseconds expressed as hours
  	 */
@@ -346, +345 @@

  	{
  		return millis / MILLIS_TO_HOUR;
  	}
- 	
+ 
  	/**
  	 * Convert a duration expressed in milliseconds to days
- 	 * 
+ 	 *
  	 * @param millis
  	 * @return milliseconds expressed as days
  	 */
@@ -357, +356 @@

  	{
  		return millis / MILLIS_TO_DAY;
  	}
- 	
+ 
  	/**
  	 * Tests for positive or negative result from timestamp comparison
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return whether or not our compared time was in the past
  	 */
@@ -369, +368 @@

  		if (diff < 0)
  		{
  			return true;
- 		}		
+ 		}
  		else
  		{
  			return false;
  		}
  	}
- 	
+ 
  	/**
  	 * Returns the appropriate time unit string for expressing a
  	 * millsecond duration in a "friendly" date format
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return The time unit name
  	 */
@@ -388, +387 @@

  		if (diff > 0 && diff < 60000)
  		{
  			return UNIT_SECONDS;
- 		}		
+ 		}
  		else if (diff >= 60000 && diff < 3600000)
  		{
  			return UNIT_MINUTES;
- 		}		
+ 		}
  		else if (diff >= 3600000 && diff < 86400000)
  		{
  			return UNIT_HOURS;
- 		}		
+ 		}
  		else if (diff >= 86400000 && diff < 604800000)
  		{
  			return UNIT_DAYS;
- 		}		
+ 		}
  		else if (diff >= 604800000)
  		{
  			return UNIT_WEEKS;
- 		}		
+ 		}
  		else
  		{
  			// default
  			return null;
  		}
  	}
- 	
+ 
  	/**
  	 * Returns true if the time unit string is singular; false if the string
  	 * needs to be pluralised
- 	 * 
+ 	 *
  	 * This method will only work on positive longs.
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return Whether or not to pluralise the time unit string
  	 */
@@ -425, +424 @@

  	{
  		// run this test now to save repeating in conditionals
  		String units = calculateUnit(diff);
- 		
+ 
  		if (units == UNIT_SECONDS && diff < 2000)
  		{
  			return true;
- 		}		
+ 		}
  		else if (units == UNIT_MINUTES && diff < 120000)
  		{
  			return true;
- 		}		
+ 		}
  		else if (units == UNIT_HOURS && diff < 7200000)
  		{
  			return true;
- 		}		
+ 		}
  		else if (units == UNIT_DAYS && diff < 172800000)
  		{
  			return true;
@@ -452, +451 @@

  			return false;
  		}
  	}
- 	
+ 
  	/**
  	 * Do we want to talk in seconds?
- 	 *  
+ 	 *
  	 * @param diff
  	 * @return Whether we are dealing with seconds or not
  	 */
@@ -470, +469 @@

  			return false;
  		}
  	}
- 	
+ 
  	/**
  	 * Do we want to talk in minutes?
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return Whether we are dealing with minutes or not
  	 */
@@ -488, +487 @@

  			return false;
  		}
  	}
- 	
+ 
  	/**
  	 * Do we want to talk in hours?
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return Whether we are dealing with hours or not
  	 */
@@ -506, +505 @@

  			return false;
  		}
  	}
- 	
+ 
  	/**
  	 * Do we want to talk in days?
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return Whether we are dealing with days or not
  	 */
@@ -524, +523 @@

  			return false;
  		}
  	}
- 	
+ 
  	/**
  	 * Do we want to talk in weeks?
- 	 * 
+ 	 *
  	 * @param diff
  	 * @return Whether we are dealing with weeks or not
  	 */
@@ -550, +549 @@

  
  {{{
  /**
-  * 
+  *
   */
  package org.apache.velocity.tools.generic;
  
@@ -560, +559 @@

  /**
   * Eclipse auto-generated helper class to retrieve localised Strings
   * for ExtendedDateTool
-  * 
+  *
   * @author Christopher Townson
   */
  public class Messages {
@@ -584, +583 @@

  
  	/**
  	 * Accessor method for retrieving String properties
- 	 * 
+ 	 *
  	 * @param key The key for the String property to get
  	 * @return The string property
  	 */

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org