2014-04-12 23:53:05 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2014 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-01-08 22:29:05 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-01-15 21:35:35 +00:00
|
|
|
import android.text.format.DateFormat;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Locale;
|
2014-01-08 22:29:05 +00:00
|
|
|
|
2015-02-05 23:16:57 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2014-01-08 22:29:05 +00:00
|
|
|
/**
|
2014-04-12 23:53:05 +00:00
|
|
|
* Utility methods to help display dates in a nice, easily readable way.
|
2014-01-08 22:29:05 +00:00
|
|
|
*/
|
|
|
|
public class DateUtils extends android.text.format.DateUtils {
|
|
|
|
|
2015-02-05 23:16:57 +00:00
|
|
|
private static boolean isWithin(final long millis, final long span, final TimeUnit unit) {
|
|
|
|
return System.currentTimeMillis() - millis <= unit.toMillis(span);
|
|
|
|
}
|
2014-01-08 22:29:05 +00:00
|
|
|
|
2015-02-05 23:16:57 +00:00
|
|
|
private static int convertDelta(final long millis, TimeUnit to) {
|
|
|
|
return (int) to.convert(System.currentTimeMillis() - millis, TimeUnit.MILLISECONDS);
|
2014-01-08 22:29:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 23:16:57 +00:00
|
|
|
public static String getBriefRelativeTimeSpanString(final Context c, final long timestamp) {
|
|
|
|
if (isWithin(timestamp, 1, TimeUnit.MINUTES)) {
|
|
|
|
return c.getString(R.string.DateUtils_now);
|
|
|
|
} else if (isWithin(timestamp, 1, TimeUnit.HOURS)) {
|
|
|
|
int mins = convertDelta(timestamp, TimeUnit.MINUTES);
|
|
|
|
return c.getResources().getQuantityString(R.plurals.minutes_ago, mins, mins);
|
|
|
|
} else if (isWithin(timestamp, 1, TimeUnit.DAYS)) {
|
|
|
|
int hours = convertDelta(timestamp, TimeUnit.HOURS);
|
|
|
|
return c.getResources().getQuantityString(R.plurals.hours_ago, hours, hours);
|
|
|
|
} else if (isWithin(timestamp, 6, TimeUnit.DAYS)) {
|
|
|
|
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY);
|
|
|
|
} else if (isWithin(timestamp, 365, TimeUnit.DAYS)) {
|
|
|
|
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL);
|
|
|
|
} else {
|
|
|
|
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
|
|
|
|
}
|
2014-01-08 22:29:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 23:16:57 +00:00
|
|
|
public static String getExtendedRelativeTimeSpanString(final Context c, final long timestamp) {
|
|
|
|
if (isWithin(timestamp, 1, TimeUnit.MINUTES)) {
|
|
|
|
return c.getString(R.string.DateUtils_now);
|
|
|
|
} else if (isWithin(timestamp, 1, TimeUnit.HOURS)) {
|
|
|
|
int mins = (int)TimeUnit.MINUTES.convert(System.currentTimeMillis() - timestamp, TimeUnit.MILLISECONDS);
|
|
|
|
return c.getResources().getQuantityString(R.plurals.minutes_ago, mins, mins);
|
|
|
|
} else {
|
|
|
|
int formatFlags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_TIME;
|
|
|
|
if (isWithin(timestamp, 6, TimeUnit.DAYS)) {
|
2014-04-12 23:53:05 +00:00
|
|
|
formatFlags |= DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY;
|
2015-02-05 23:16:57 +00:00
|
|
|
} else if (isWithin(timestamp, 365, TimeUnit.DAYS)) {
|
2014-04-12 23:53:05 +00:00
|
|
|
formatFlags |= DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
|
|
|
|
} else {
|
2014-07-24 14:26:46 +00:00
|
|
|
formatFlags |= DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL;
|
2014-04-12 23:53:05 +00:00
|
|
|
}
|
2015-02-05 23:16:57 +00:00
|
|
|
return DateUtils.formatDateTime(c, timestamp, formatFlags);
|
2014-01-08 22:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 21:35:35 +00:00
|
|
|
|
|
|
|
public static SimpleDateFormat getDetailedDateFormatter(Context context) {
|
|
|
|
String dateFormatPattern;
|
|
|
|
|
|
|
|
if (DateFormat.is24HourFormat(context)) {
|
|
|
|
dateFormatPattern = "MMM d, yyyy HH:mm:ss zzz";
|
|
|
|
} else {
|
2015-03-16 03:28:23 +00:00
|
|
|
dateFormatPattern = "MMM d, yyyy hh:mm:ss a zzz";
|
2015-01-15 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new SimpleDateFormat(dateFormatPattern, Locale.getDefault());
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:29:05 +00:00
|
|
|
}
|