Skip to content

Commit

Permalink
Merge pull request #43 from tom93/pr/format-date-long
Browse files Browse the repository at this point in the history
Add millis-based version of formatDateOrTime() and isThisYear(), deprecated sec-based version
  • Loading branch information
naveensingh authored May 1, 2024
2 parents 5bf58fd + fb72184 commit cc8b9c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
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

0 comments on commit cc8b9c2

Please sign in to comment.