From 790c3829edafcb41d6eeb14301a23db22c559e96 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Fri, 01 Jul 2011 17:45:23 -0400 Subject: [PATCH] Documentation. Added upgrade info to site. Moved todos to GoogleCode. --- src/com/gitblit/utils/TimeUtils.java | 100 +++++++++++++++++++++++++++++++++++++------------ 1 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/com/gitblit/utils/TimeUtils.java b/src/com/gitblit/utils/TimeUtils.java index 60b525b..dcb60ab 100644 --- a/src/com/gitblit/utils/TimeUtils.java +++ b/src/com/gitblit/utils/TimeUtils.java @@ -1,33 +1,80 @@ +/* + * Copyright 2011 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.gitblit.utils; +import java.util.Calendar; import java.util.Date; public class TimeUtils { - private final static long min = 1000 * 60l; + public static final long MIN = 1000 * 60L; - private final static long halfhour = min * 30l; + public static final long HALFHOUR = MIN * 30L; - private final static long onehour = halfhour * 2; + public static final long ONEHOUR = HALFHOUR * 2; - private final static long oneday = onehour * 24l; + public static final long ONEDAY = ONEHOUR * 24L; - @SuppressWarnings("deprecation") + public static final long ONEYEAR = ONEDAY * 365L; + public static boolean isToday(Date date) { - Date now = new Date(); - return now.getDate() == date.getDate() && now.getMonth() == date.getMonth() && now.getYear() == date.getYear(); + return (System.currentTimeMillis() - date.getTime()) < ONEDAY; } - @SuppressWarnings("deprecation") public static boolean isYesterday(Date date) { - Date now = new Date(); - return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth() && now.getYear() == date.getYear(); + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.add(Calendar.DATE, 1); + return (System.currentTimeMillis() - cal.getTimeInMillis()) < ONEDAY; + } + + public static String duration(int days) { + if (days <= 60) { + return days + (days > 1 ? " days" : " day"); + } else if (days < 365) { + int rem = days % 30; + return ((days / 30) + (rem >= 15 ? 1 : 0)) + " months"; + } else { + int years = days / 365; + int rem = days % 365; + String yearsString = years + (years > 1 ? " years" : " year"); + if (rem < 30) { + if (rem == 0) { + return yearsString; + } else { + return yearsString + (rem >= 15 ? ", 1 month" : ""); + } + } else { + int months = rem / 30; + int remDays = rem % 30; + if (remDays >= 15) { + months++; + } + String monthsString = yearsString + ", " + months + + (months > 1 ? " months" : " month"); + return monthsString; + } + } } public static int minutesAgo(Date date, long endTime, boolean roundup) { long diff = endTime - date.getTime(); - int mins = (int) (diff / min); - if (roundup && (diff % min) >= 30) + int mins = (int) (diff / MIN); + if (roundup && (diff % MIN) >= 30) { mins++; + } return mins; } @@ -37,17 +84,19 @@ public static int hoursAgo(Date date, boolean roundup) { long diff = System.currentTimeMillis() - date.getTime(); - int hours = (int) (diff / onehour); - if (roundup && (diff % onehour) >= halfhour) + int hours = (int) (diff / ONEHOUR); + if (roundup && (diff % ONEHOUR) >= HALFHOUR) { hours++; + } return hours; } public static int daysAgo(Date date, boolean roundup) { long diff = System.currentTimeMillis() - date.getTime(); - int days = (int) (diff / oneday); - if (roundup && (diff % oneday) > 0) + int days = (int) (diff / ONEDAY); + if (roundup && (diff % ONEDAY) > 0) { days++; + } return days; } @@ -63,7 +112,7 @@ String ago = null; if (isToday(date) || isYesterday(date)) { int mins = minutesAgo(date, true); - if (mins > 120) { + if (mins >= 120) { if (css) { return "age1"; } @@ -71,7 +120,7 @@ if (hours > 23) { ago = "yesterday"; } else { - ago = hours + " hour" + (hours > 1 ? "s" : "") + " ago"; + ago = hours + " hours ago"; } } else { if (css) { @@ -86,21 +135,22 @@ int days = daysAgo(date, true); if (days < 365) { if (days <= 30) { - ago = days + " day" + (days > 1 ? "s" : "") + " ago"; + ago = days + " days ago"; } else if (days <= 90) { int weeks = days / 7; - if (weeks == 12) + if (weeks == 12) { ago = "3 months ago"; - else + } else { ago = weeks + " weeks ago"; + } } else if (days > 90) { int months = days / 30; int weeks = (days % 30) / 7; - if (weeks >= 2) + if (weeks >= 2) { months++; - ago = months + " month" + (months > 1 ? "s" : "") + " ago"; - } else - ago = days + " day" + (days > 1 ? "s" : "") + " ago"; + } + ago = months + " months ago"; + } } else if (days == 365) { ago = "1 year ago"; } else { -- Gitblit v1.9.1