2017-05-05 00:55:34 +02:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.util;
|
|
|
|
|
2017-07-15 01:45:26 +02:00
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.R;
|
2017-07-15 00:18:29 +02:00
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
public class DateUtils {
|
2019-04-22 10:11:00 +02:00
|
|
|
|
|
|
|
private static final long SECOND_IN_MILLIS = 1000;
|
|
|
|
private static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
|
|
|
|
private static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
|
|
|
|
private static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
|
|
|
|
private static final long YEAR_IN_MILLIS = DAY_IN_MILLIS * 365;
|
|
|
|
|
2017-07-28 04:03:45 +02:00
|
|
|
/**
|
|
|
|
* This is a rough duplicate of {@link android.text.format.DateUtils#getRelativeTimeSpanString},
|
|
|
|
* but even with the FORMAT_ABBREV_RELATIVE flag it wasn't abbreviating enough.
|
|
|
|
*/
|
2017-07-15 01:45:26 +02:00
|
|
|
public static String getRelativeTimeSpanString(Context context, long then, long now) {
|
2019-04-22 10:11:00 +02:00
|
|
|
long span = now - then;
|
2017-07-15 01:45:26 +02:00
|
|
|
boolean future = false;
|
2017-05-05 00:55:34 +02:00
|
|
|
if (span < 0) {
|
2017-07-15 01:45:26 +02:00
|
|
|
future = true;
|
2017-05-05 00:55:34 +02:00
|
|
|
span = -span;
|
|
|
|
}
|
2019-04-22 10:11:00 +02:00
|
|
|
int format;
|
|
|
|
if (span < MINUTE_IN_MILLIS) {
|
|
|
|
span /= SECOND_IN_MILLIS;
|
2017-07-15 01:45:26 +02:00
|
|
|
if (future) {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_in_seconds;
|
2017-07-15 01:45:26 +02:00
|
|
|
} else {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_seconds_ago;
|
2017-07-15 01:45:26 +02:00
|
|
|
}
|
2019-04-22 10:11:00 +02:00
|
|
|
} else if (span < HOUR_IN_MILLIS) {
|
|
|
|
span /= MINUTE_IN_MILLIS;
|
2017-07-15 01:45:26 +02:00
|
|
|
if (future) {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_in_minutes;
|
2017-07-15 01:45:26 +02:00
|
|
|
} else {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_minutes_ago;
|
2017-07-15 01:45:26 +02:00
|
|
|
}
|
2019-04-22 10:11:00 +02:00
|
|
|
} else if (span < DAY_IN_MILLIS) {
|
|
|
|
span /= HOUR_IN_MILLIS;
|
2017-07-15 01:45:26 +02:00
|
|
|
if (future) {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_in_hours;
|
2017-07-15 01:45:26 +02:00
|
|
|
} else {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_hours_ago;
|
2017-07-15 01:45:26 +02:00
|
|
|
}
|
2019-04-22 10:11:00 +02:00
|
|
|
} else if (span < YEAR_IN_MILLIS) {
|
|
|
|
span /= DAY_IN_MILLIS;
|
2017-07-15 01:45:26 +02:00
|
|
|
if (future) {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_in_days;
|
2017-07-15 01:45:26 +02:00
|
|
|
} else {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_days_ago;
|
2017-07-15 01:45:26 +02:00
|
|
|
}
|
2017-05-05 00:55:34 +02:00
|
|
|
} else {
|
2019-04-22 10:11:00 +02:00
|
|
|
span /= YEAR_IN_MILLIS;
|
2017-07-15 01:45:26 +02:00
|
|
|
if (future) {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_in_years;
|
2017-07-15 01:45:26 +02:00
|
|
|
} else {
|
2019-04-22 10:11:00 +02:00
|
|
|
format = R.string.abbreviated_years_ago;
|
2017-07-15 01:45:26 +02:00
|
|
|
}
|
2017-05-05 00:55:34 +02:00
|
|
|
}
|
2019-04-22 10:11:00 +02:00
|
|
|
return context.getString(format, span);
|
2017-05-05 00:55:34 +02:00
|
|
|
}
|
2019-04-22 10:11:00 +02:00
|
|
|
|
2019-05-06 09:59:06 +02:00
|
|
|
public static String formatPollDuration(Context context, long then, long now) {
|
2019-04-22 10:11:00 +02:00
|
|
|
long span = then - now;
|
|
|
|
if (span < 0) {
|
|
|
|
span = 0;
|
|
|
|
}
|
|
|
|
int format;
|
|
|
|
if (span < MINUTE_IN_MILLIS) {
|
|
|
|
span /= SECOND_IN_MILLIS;
|
2019-05-06 09:59:06 +02:00
|
|
|
format = R.plurals.poll_timespan_seconds;
|
2019-04-22 10:11:00 +02:00
|
|
|
} else if (span < HOUR_IN_MILLIS) {
|
|
|
|
span /= MINUTE_IN_MILLIS;
|
2019-05-06 09:59:06 +02:00
|
|
|
format = R.plurals.poll_timespan_minutes;
|
2019-04-22 10:11:00 +02:00
|
|
|
|
|
|
|
} else if (span < DAY_IN_MILLIS) {
|
|
|
|
span /= HOUR_IN_MILLIS;
|
2019-05-06 09:59:06 +02:00
|
|
|
format = R.plurals.poll_timespan_hours;
|
2019-04-22 10:11:00 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
span /= DAY_IN_MILLIS;
|
2019-05-06 09:59:06 +02:00
|
|
|
format = R.plurals.poll_timespan_days;
|
2019-04-22 10:11:00 +02:00
|
|
|
}
|
2019-05-06 09:59:06 +02:00
|
|
|
return context.getResources().getQuantityString(format, (int) span, (int) span);
|
2019-04-22 10:11:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 00:55:34 +02:00
|
|
|
}
|