Skip to content

Commit

Permalink
Add milliseconds-based version of formatDateOrTime() and isThisYear()…
Browse files Browse the repository at this point in the history
…, 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.
  • Loading branch information
tom93 committed Mar 25, 2024
1 parent 5381db3 commit ca22da3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
36 changes: 6 additions & 30 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
31 changes: 31 additions & 0 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Long.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ca22da3

Please sign in to comment.