Commit faf2c821 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Add getLocalDateTime and formatLocalTime function.

Also rename formatDate function to formatLocalDate.
parent 2ee03851
import android.content.Context
import chat.rocket.android.R
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
import org.threeten.bp.Period
import org.threeten.bp.*
import org.threeten.bp.format.DateTimeFormatter
import org.threeten.bp.format.FormatStyle
import org.threeten.bp.format.TextStyle
......@@ -14,20 +12,20 @@ object DateTimeHelper {
private val lastWeek = today.minusWeeks(1)
/**
* Returns a date from a LocalDateTime or the textual representation if the LocalDateTime has a max period of a week from the current date.
* Returns a date from a [LocalDateTime] or the textual representation if the [LocalDateTime] has a max period of a week from the current date.
*
* @param localDateTime The LocalDateTime.
* @param localDateTime The [LocalDateTime].
* @param context The context.
* @return The date or the textual representation from a LocalDateTime.
* @return The date or the textual representation from a [LocalDateTime].
*/
fun getDate(localDateTime: LocalDateTime, context: Context): String {
val localDate = localDateTime.toLocalDate()
return when (localDate) {
today -> localDateTime.toLocalTime().toString()
today -> formatLocalTime(localDateTime.toLocalTime())
yesterday -> context.getString(R.string.msg_yesterday)
else -> {
if (Period.between(lastWeek, localDate).days <= 0) {
formatDate(localDate)
formatLocalDate(localDate)
} else {
localDate.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault())
}
......@@ -36,18 +34,33 @@ object DateTimeHelper {
}
/**
* Returns a time from a LocalDateTime.
* Returns a time from a [LocalDateTime].
*
* @param localDateTime The LocalDateTime.
* @return The time from a LocalDateTime.
* @param localDateTime The [LocalDateTime].
* @return The time from a [LocalDateTime].
*/
fun getTime(localDateTime: LocalDateTime): String {
val formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
return localDateTime.toLocalTime().format(formatter).toString()
}
private fun formatDate(localDate: LocalDate): String {
/**
* Returns a [LocalDateTime] from a [Long].
*
* @param long The [Long]
* @return The [LocalDateTime] from a [Long].
*/
fun getLocalDateTime(long: Long): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(long), ZoneId.systemDefault())
}
private fun formatLocalDate(localDate: LocalDate): String {
val formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
return localDate.format(formatter).toString()
}
private fun formatLocalTime(localTime: LocalTime): String {
val formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
return localTime.format(formatter).toString()
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment