From: <jbo...@li...> - 2005-11-03 20:01:58
|
Author: aron.gombas Date: 2005-11-03 15:01:50 -0500 (Thu, 03 Nov 2005) New Revision: 1495 Added: trunk/labs/kosmos/src/java/hu/midori/kosmos/portlet/taglib/ trunk/labs/kosmos/src/java/hu/midori/kosmos/portlet/taglib/AgeTag.java Log: Shit, this was missing! Added: trunk/labs/kosmos/src/java/hu/midori/kosmos/portlet/taglib/AgeTag.java =================================================================== --- trunk/labs/kosmos/src/java/hu/midori/kosmos/portlet/taglib/AgeTag.java 2005-11-03 19:43:12 UTC (rev 1494) +++ trunk/labs/kosmos/src/java/hu/midori/kosmos/portlet/taglib/AgeTag.java 2005-11-03 20:01:50 UTC (rev 1495) @@ -0,0 +1,88 @@ +/* + * Kosmos. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package hu.midori.kosmos.portlet.taglib; + +import java.io.IOException; +import java.text.NumberFormat; +import java.util.Locale; +import java.util.ResourceBundle; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.jstl.core.Config; +import javax.servlet.jsp.tagext.TagSupport; + +/** + * Displays localized "age" information: time difference between + * "now" and the given "birth" value, in the most appropriate unit of + * minutes, hours, days, weeks and such. + * It uses the <code>Locale</code> set for JSTL, so doesn't require + * additional configuration. + * + * @author <a href="mailto:aro...@mi...">Aron Gombas</a> + * @version $Id$ + */ +public class AgeTag extends TagSupport { + /** @see #setTime(String) */ + private String time; + /** Localized string resources. */ + private static ResourceBundle res; + + /** Sets the millisecond of the "birth" value used for computing the age. */ + public void setTime(String time) { + this.time = time; + } + + @Override + public int doStartTag() throws JspException { + res = ResourceBundle.getBundle("kosmos-taglib", new Locale((String)Config.find(pageContext, Config.FMT_LOCALE))); + + long milliseconds = Long.parseLong(time); + long value = 0; + String unit = null; + + long minutes = milliseconds / (60*1000); + if(minutes < 2*60) { + value = minutes; + unit = res.getString("age.minutes"); + } else { + long hours = minutes / 60; + if(hours < 48) { + value = hours; + unit = res.getString("age.hours"); + } else { + long days = hours / 24; + if(days < 3*7) { + value = days; + unit = res.getString("age.days"); + } else { + long weeks = days / 7; + if(weeks < 10) { + value = weeks; + unit = res.getString("age.weeks"); + } else { + long months = days / 30; + if(months < 24) { + value = months; + unit = res.getString("age.months"); + } else { + value = months / 12; + unit = res.getString("age.years"); + } + } + } + } + } + + try { + pageContext.getOut().print(String.format("%s %s", NumberFormat.getIntegerInstance().format(value), unit)); + } catch(IOException ex) { + throw new JspException(ex); + } + + return SKIP_BODY; + } +} |