Tusky-App-Android/app/src/main/java/com/keylesspalace/tusky/util/DateUtils.java

75 lines
2.7 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;
2017-05-05 00:55:34 +02:00
public class DateUtils {
/* This is a rough duplicate of 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) {
2017-05-05 00:55:34 +02:00
final long MINUTE = 60;
final long HOUR = 60 * MINUTE;
final long DAY = 24 * HOUR;
final long YEAR = 365 * DAY;
long span = (now - then) / 1000;
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;
}
2017-07-15 01:45:26 +02:00
String format;
2017-05-05 00:55:34 +02:00
if (span < MINUTE) {
2017-07-15 01:45:26 +02:00
if (future) {
format = context.getString(R.string.abbreviated_in_seconds);
} else {
format = context.getString(R.string.abbreviated_seconds_ago);
}
2017-05-05 00:55:34 +02:00
} else if (span < HOUR) {
span /= MINUTE;
2017-07-15 01:45:26 +02:00
if (future) {
format = context.getString(R.string.abbreviated_in_minutes);
} else {
format = context.getString(R.string.abbreviated_minutes_ago);
}
2017-05-05 00:55:34 +02:00
} else if (span < DAY) {
span /= HOUR;
2017-07-15 01:45:26 +02:00
if (future) {
format = context.getString(R.string.abbreviated_in_hours);
} else {
format = context.getString(R.string.abbreviated_hours_ago);
}
2017-05-05 00:55:34 +02:00
} else if (span < YEAR) {
span /= DAY;
2017-07-15 01:45:26 +02:00
if (future) {
format = context.getString(R.string.abbreviated_in_days);
} else {
format = context.getString(R.string.abbreviated_days_ago);
}
2017-05-05 00:55:34 +02:00
} else {
span /= YEAR;
2017-07-15 01:45:26 +02:00
if (future) {
format = context.getString(R.string.abbreviated_in_years);
} else {
format = context.getString(R.string.abbreviated_years_ago);
}
2017-05-05 00:55:34 +02:00
}
2017-07-15 01:45:26 +02:00
return String.format(format, span);
2017-05-05 00:55:34 +02:00
}
}