Yuito-app-android/app/src/main/java/com/keylesspalace/tusky/util/TimestampUtils.java

109 lines
3.8 KiB
Java
Raw Normal View History

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;
public class TimestampUtils {
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) {
long span = now - then;
2017-07-15 01:45:26 +02:00
boolean future = false;
if (Math.abs(span) < SECOND_IN_MILLIS) {
return context.getString(R.string.status_created_at_now);
}
else if (span < 0) {
2017-07-15 01:45:26 +02:00
future = true;
2017-05-05 00:55:34 +02:00
span = -span;
}
int format;
if (span < MINUTE_IN_MILLIS) {
span /= SECOND_IN_MILLIS;
2017-07-15 01:45:26 +02:00
if (future) {
format = R.string.abbreviated_in_seconds;
2017-07-15 01:45:26 +02:00
} else {
format = R.string.abbreviated_seconds_ago;
2017-07-15 01:45:26 +02:00
}
} else if (span < HOUR_IN_MILLIS) {
span /= MINUTE_IN_MILLIS;
2017-07-15 01:45:26 +02:00
if (future) {
format = R.string.abbreviated_in_minutes;
2017-07-15 01:45:26 +02:00
} else {
format = R.string.abbreviated_minutes_ago;
2017-07-15 01:45:26 +02:00
}
} else if (span < DAY_IN_MILLIS) {
span /= HOUR_IN_MILLIS;
2017-07-15 01:45:26 +02:00
if (future) {
format = R.string.abbreviated_in_hours;
2017-07-15 01:45:26 +02:00
} else {
format = R.string.abbreviated_hours_ago;
2017-07-15 01:45:26 +02:00
}
} else if (span < YEAR_IN_MILLIS) {
span /= DAY_IN_MILLIS;
2017-07-15 01:45:26 +02:00
if (future) {
format = R.string.abbreviated_in_days;
2017-07-15 01:45:26 +02:00
} else {
format = R.string.abbreviated_days_ago;
2017-07-15 01:45:26 +02:00
}
2017-05-05 00:55:34 +02:00
} else {
span /= YEAR_IN_MILLIS;
2017-07-15 01:45:26 +02:00
if (future) {
format = R.string.abbreviated_in_years;
2017-07-15 01:45:26 +02:00
} else {
format = R.string.abbreviated_years_ago;
2017-07-15 01:45:26 +02:00
}
2017-05-05 00:55:34 +02:00
}
return context.getString(format, span);
2017-05-05 00:55:34 +02:00
}
public static String formatPollDuration(Context context, long then, long now) {
long span = then - now;
if (span < 0) {
span = 0;
}
int format;
if (span < MINUTE_IN_MILLIS) {
span /= SECOND_IN_MILLIS;
format = R.plurals.poll_timespan_seconds;
} else if (span < HOUR_IN_MILLIS) {
span /= MINUTE_IN_MILLIS;
format = R.plurals.poll_timespan_minutes;
} else if (span < DAY_IN_MILLIS) {
span /= HOUR_IN_MILLIS;
format = R.plurals.poll_timespan_hours;
} else {
span /= DAY_IN_MILLIS;
format = R.plurals.poll_timespan_days;
}
return context.getResources().getQuantityString(format, (int) span, (int) span);
}
2017-05-05 00:55:34 +02:00
}