001: package ru.emdev.EmForge.web.converter;
002:
003: import java.util.Calendar;
004: import java.util.Date;
005: import java.util.LinkedHashMap;
006: import java.util.Map;
007:
008: import javax.faces.component.UIComponent;
009: import javax.faces.context.FacesContext;
010: import javax.faces.convert.Converter;
011:
012: /**
013: *
014: * @author AKotenko
015: *
016: */
017: public class PrettyTimeDeltaConverter implements Converter {
018:
019: private static final String BEFORE_POSTFIX = " ago";
020: private static final String AFTER_POSTFIX = " after";
021:
022: public Object getAsObject(FacesContext context,
023: UIComponent component, String value) {
024: return null;
025: }
026:
027: /*
028: * Based on the Python function from Edgewall Trac Project (http://trac.edgewall.org)
029: *
030: * Look Also: http://bobcat.webappcabaret.net/javachina/jc/share/DiffDate.htm
031: *
032: * def pretty_timedelta(time1, time2=None):
033: if not time1: time1 = time.time()
034: if not time2: time2 = time.time()
035: if time1 > time2:
036: time2, time1 = time1, time2
037: units = ((3600 * 24 * 365, 'year', 'years'),
038: (3600 * 24 * 30, 'month', 'months'),
039: (3600 * 24 * 7, 'week', 'weeks'),
040: (3600 * 24, 'day', 'days'),
041: (3600, 'hour', 'hours'),
042: (60, 'minute', 'minutes'))
043: age_s = int(time2 - time1)
044: if age_s < 60:
045: return '%i second%s' % (age_s, age_s != 1 and 's' or '')
046: for u, unit, unit_plural in units:
047: r = float(age_s) / float(u)
048: if r >= 0.9:
049: r = int(round(r))
050: return '%d %s' % (r, r == 1 and unit or unit_plural)
051: return ''
052: */
053: public String getAsString(FacesContext context,
054: UIComponent component, Object value) {
055: if (value == null) {
056: return "";
057: }
058:
059: Calendar todayCalendar = Calendar.getInstance(); // current moment
060: Calendar valueCalendar = Calendar.getInstance();
061: valueCalendar.setTime((Date) value); // the value
062:
063: Map<Long, String> dateUnits = new LinkedHashMap<Long, String>(); // to keep the order
064: {
065: dateUnits.put(new Long(3600 * 24 * 365), "year");
066: dateUnits.put(new Long(3600 * 24 * 30), "month");
067: dateUnits.put(new Long(3600 * 24 * 7), "week");
068: dateUnits.put(new Long(3600 * 24), "day");
069: dateUnits.put(new Long(3600), "hour");
070: dateUnits.put(new Long(60), "minute");
071: }
072:
073: /* Long todaySeconds = todayCalendar.getTime().getTime() +
074: todayCalendar.get(Calendar.ZONE_OFFSET) +
075: todayCalendar.get(Calendar.DST_OFFSET);
076: Long valueSeconds = valueCalendar.getTime().getTime() +
077: valueCalendar.get(Calendar.ZONE_OFFSET) +
078: valueCalendar.get(Calendar.DST_OFFSET); */
079:
080: Long todaySeconds = todayCalendar.getTimeInMillis() / 1000;
081: Long valueSeconds = valueCalendar.getTimeInMillis() / 1000;
082:
083: Integer secondsDifference = todayCalendar.before(valueCalendar) ? Math
084: .round(valueSeconds - todaySeconds)
085: : Math.round(todaySeconds - valueSeconds);
086:
087: String postfix = (todayCalendar.compareTo(valueCalendar) >= 0) ? BEFORE_POSTFIX
088: : AFTER_POSTFIX;
089:
090: if (secondsDifference < 60) {
091: return String.format("%1$d second%2$s" + postfix,
092: secondsDifference, (secondsDifference == 1) ? ""
093: : "s");
094: }
095:
096: for (Map.Entry<Long, String> dateUnit : dateUnits.entrySet()) {
097: Float valueInDate = Float.valueOf(secondsDifference)
098: / Float.valueOf(dateUnit.getKey());
099: if (valueInDate >= 0.9) {
100: Integer intValueInDate = Integer.valueOf(Math
101: .round(valueInDate));
102: return String.format("%1$d %2$s" + postfix,
103: intValueInDate, dateUnit.getValue()
104: + ((intValueInDate == 1) ? "" : "s"));
105: }
106: }
107:
108: return "";
109: }
110:
111: }
|