From ca22da3ef44d28c8fcd324862ceb2f8a0b4c06a0 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 | 36 ++++--------------- .../org/fossify/commons/extensions/Long.kt | 31 ++++++++++++++++ 2 files changed, 37 insertions(+), 30 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..2dc31f20d 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt @@ -58,43 +58,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()")) 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() } -// 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..6ccb16483 100644 --- a/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt +++ b/commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt @@ -23,3 +23,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) +}