@ -1,14 +1,11 @@
package eu.kanade.tachiyomi.util.lang
import android.content.Context
import eu.kanade.tachiyomi.R
import java.text.DateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
fun Date . toDateTimestampString ( dateFormatter : DateFormat ) : String {
val date = dateFormatter . format ( this )
@ -45,101 +42,3 @@ fun Long.toDateKey(): Date {
cal [ Calendar . MILLISECOND ] = 0
return cal . time
}
/ * *
* Convert epoch long to Calendar instance
*
* @return Calendar instance at supplied epoch time . Null if epoch was 0.
* /
fun Long . toCalendar ( ) : Calendar ? {
if ( this == 0L ) {
return null
}
val cal = Calendar . getInstance ( )
cal . timeInMillis = this
return cal
}
/ * *
* Convert local time millisecond value to Calendar instance in UTC
*
* @return UTC Calendar instance at supplied time . Null if time is 0.
* /
fun Long . toUtcCalendar ( ) : Calendar ? {
if ( this == 0L ) {
return null
}
val rawCalendar = Calendar . getInstance ( ) . apply {
timeInMillis = this @toUtcCalendar
}
return Calendar . getInstance ( TimeZone . getTimeZone ( " UTC " ) ) . apply {
clear ( )
set (
rawCalendar . get ( Calendar . YEAR ) ,
rawCalendar . get ( Calendar . MONTH ) ,
rawCalendar . get ( Calendar . DAY _OF _MONTH ) ,
rawCalendar . get ( Calendar . HOUR _OF _DAY ) ,
rawCalendar . get ( Calendar . MINUTE ) ,
rawCalendar . get ( Calendar . SECOND ) ,
)
}
}
/ * *
* Convert UTC time millisecond to Calendar instance in local time zone
*
* @return local Calendar instance at supplied UTC time . Null if time is 0.
* /
fun Long . toLocalCalendar ( ) : Calendar ? {
if ( this == 0L ) {
return null
}
val rawCalendar = Calendar . getInstance ( TimeZone . getTimeZone ( " UTC " ) ) . apply {
timeInMillis = this @toLocalCalendar
}
return Calendar . getInstance ( ) . apply {
clear ( )
set (
rawCalendar . get ( Calendar . YEAR ) ,
rawCalendar . get ( Calendar . MONTH ) ,
rawCalendar . get ( Calendar . DAY _OF _MONTH ) ,
rawCalendar . get ( Calendar . HOUR _OF _DAY ) ,
rawCalendar . get ( Calendar . MINUTE ) ,
rawCalendar . get ( Calendar . SECOND ) ,
)
}
}
private const val MILLISECONDS _IN _DAY = 86 _400 _000L
fun Date . toRelativeString (
context : Context ,
dateFormat : DateFormat = DateFormat . getDateInstance ( DateFormat . SHORT ) ,
) : String {
val now = Date ( )
val difference = now . timeWithOffset . floorNearest ( MILLISECONDS _IN _DAY ) - this . timeWithOffset . floorNearest ( MILLISECONDS _IN _DAY )
val days = difference . floorDiv ( MILLISECONDS _IN _DAY ) . toInt ( )
return when {
difference < 0 -> dateFormat . format ( this )
difference < MILLISECONDS _IN _DAY -> context . getString ( R . string . relative _time _today )
difference < MILLISECONDS_IN_DAY . times ( 7 ) -> context . resources . getQuantityString (
R . plurals . relative _time ,
days ,
days ,
)
else -> dateFormat . format ( this )
}
}
private val Date . timeWithOffset : Long
get ( ) {
return Calendar . getInstance ( ) . run {
time = this @timeWithOffset
val dstOffset = get ( Calendar . DST _OFFSET )
this @timeWithOffset . time + timeZone . rawOffset + dstOffset
}
}
fun Long . floorNearest ( to : Long ) : Long {
return this . floorDiv ( to ) * to
}