From 8d86e4abe9e091a623880853414d014138efb8e9 Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Mon, 25 Mar 2024 15:55:18 +0000 Subject: [PATCH] Add milliseconds-based version of formatDateOrTime() and isThisYear(), deprecated seconds-based version The seconds-based version uses Int so it will break due to the Year 2038 problem. Add a version that uses milliseconds (as Long). This matches the existing formatDate() extension, which already had both a seconds-based (Int) version and a milliseconds-based (Long) version. Also deprecate Int.formatDate(), and make it a simple wrapper for Long.formatDate() to reduce code repetition. --- .../org/fossify/commons/extensions/Int.kt | 40 +++---------------- .../org/fossify/commons/extensions/Long.kt | 33 +++++++++++++++ 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt index 76dc1b223..01cb453bc 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt @@ -6,13 +6,9 @@ import android.graphics.Color import android.media.ExifInterface import android.os.Handler import android.os.Looper -import android.text.format.DateFormat -import android.text.format.DateUtils -import android.text.format.Time import androidx.core.os.postDelayed import org.fossify.commons.helpers.DARK_GREY import java.text.DecimalFormat -import java.util.Calendar import java.util.Locale import java.util.Random @@ -58,43 +54,19 @@ fun Int.formatSize(): String { return "${DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble()))} ${units[digitGroups]}" } +@Deprecated("Broken due to the Year 2038 problem. Use Long.formatDate() instead (but note that it uses milliseconds, not seconds).", ReplaceWith("(this * 1000L).formatDate(context, dateFormat, timeFormat)")) fun Int.formatDate(context: Context, dateFormat: String? = null, timeFormat: String? = null): String { - val useDateFormat = dateFormat ?: context.baseConfig.dateFormat - val useTimeFormat = timeFormat ?: context.getTimeFormat() - val cal = Calendar.getInstance(Locale.ENGLISH) - cal.timeInMillis = this * 1000L - return DateFormat.format("$useDateFormat, $useTimeFormat", cal).toString() + return (this * 1000L).formatDate(context, dateFormat, timeFormat) } -// if the given date is today, we show only the time. Else we show the date and optionally the time too +@Deprecated("Broken due to the Year 2038 problem. Use Long.formatDateOrTime() instead (but note that it uses milliseconds, not seconds).", ReplaceWith("(this * 1000L).formatDateOrTime(context, hideTimeAtOtherDays, showYearEvenIfCurrent)")) fun Int.formatDateOrTime(context: Context, hideTimeAtOtherDays: Boolean, showYearEvenIfCurrent: Boolean): String { - val cal = Calendar.getInstance(Locale.ENGLISH) - cal.timeInMillis = this * 1000L - - return if (DateUtils.isToday(this * 1000L)) { - DateFormat.format(context.getTimeFormat(), cal).toString() - } else { - var format = context.baseConfig.dateFormat - if (!showYearEvenIfCurrent && isThisYear()) { - format = format.replace("y", "").trim().trim('-').trim('.').trim('/') - } - - if (!hideTimeAtOtherDays) { - format += ", ${context.getTimeFormat()}" - } - - DateFormat.format(format, cal).toString() - } + return (this * 1000L).formatDateOrTime(context, hideTimeAtOtherDays, showYearEvenIfCurrent) } +@Deprecated("Broken due to the Year 2038 problem. Use Long.isThisYear() instead (but note that it uses milliseconds, not seconds).", ReplaceWith("(this * 1000L).isThisYear()")) fun Int.isThisYear(): Boolean { - val time = Time() - time.set(this * 1000L) - - val thenYear = time.year - time.set(System.currentTimeMillis()) - - return (thenYear == time.year) + return (this * 1000L).isThisYear() } fun Int.addBitIf(add: Boolean, bit: Int) = diff --git a/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt b/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt index c01973554..b0491addf 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt @@ -2,6 +2,8 @@ package org.fossify.commons.extensions import android.content.Context import android.text.format.DateFormat +import android.text.format.DateUtils +import android.text.format.Time import java.text.DecimalFormat import java.util.Calendar import java.util.Locale @@ -23,3 +25,34 @@ fun Long.formatDate(context: Context, dateFormat: String? = null, timeFormat: St cal.timeInMillis = this return DateFormat.format("$useDateFormat, $useTimeFormat", cal).toString() } + +// if the given date is today, we show only the time. Else we show the date and optionally the time too +fun Long.formatDateOrTime(context: Context, hideTimeAtOtherDays: Boolean, showYearEvenIfCurrent: Boolean): String { + val cal = Calendar.getInstance(Locale.ENGLISH) + cal.timeInMillis = this + + return if (DateUtils.isToday(this)) { + DateFormat.format(context.getTimeFormat(), cal).toString() + } else { + var format = context.baseConfig.dateFormat + if (!showYearEvenIfCurrent && isThisYear()) { + format = format.replace("y", "").trim().trim('-').trim('.').trim('/') + } + + if (!hideTimeAtOtherDays) { + format += ", ${context.getTimeFormat()}" + } + + DateFormat.format(format, cal).toString() + } +} + +fun Long.isThisYear(): Boolean { + val time = Time() + time.set(this) + + val thenYear = time.year + time.set(System.currentTimeMillis()) + + return (thenYear == time.year) +}