Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add millis-based version of formatDateOrTime() and isThisYear(), deprecated sec-based version #43

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Int.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) =
Expand Down
33 changes: 33 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 @@ -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
Expand All @@ -24,6 +26,37 @@ fun Long.formatDate(context: Context, dateFormat: String? = null, timeFormat: St
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)
}

fun Long.toDayCode(format: String = "ddMMyy"): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = this
Expand Down