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

Look at using RelativeDateTimeFormatter #1300

Open
github-actions bot opened this issue Jun 2, 2023 · 0 comments
Open

Look at using RelativeDateTimeFormatter #1300

github-actions bot opened this issue Jun 2, 2023 · 0 comments

Comments

@github-actions
Copy link
Contributor

github-actions bot commented Jun 2, 2023

https://api.github.com/chrisbanes/tivi/blob/e7b824e5d7af21bf0c5c0aaef4de72190cc45fd2/common/ui/resources/src/jvmMain/kotlin/app/tivi/util/TiviDateFormatter.kt#L72


// Copyright 2018, Google LLC, Christopher Banes and the Tivi project contributors
// SPDX-License-Identifier: Apache-2.0

package app.tivi.util

import app.tivi.inject.ActivityScope
import java.time.LocalDateTime as JavaLocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.temporal.Temporal
import java.util.Locale
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalTime
import kotlinx.datetime.toJavaInstant
import kotlinx.datetime.toJavaLocalDate
import kotlinx.datetime.toJavaLocalTime
import me.tatarka.inject.annotations.Inject

@ActivityScope
@Inject
actual class TiviDateFormatter(
    private val locale: Locale = Locale.getDefault(),
) {
    private val shortDate: DateTimeFormatter by lazy {
        DateTimeFormatter
            .ofLocalizedDate(FormatStyle.SHORT)
            .withLocale(locale)
    }
    private val shortTime: DateTimeFormatter by lazy {
        DateTimeFormatter
            .ofLocalizedTime(FormatStyle.SHORT)
            .withLocale(locale)
    }
    private val mediumDate: DateTimeFormatter by lazy {
        DateTimeFormatter
            .ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(locale)
    }
    private val mediumDateTime: DateTimeFormatter by lazy {
        DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.MEDIUM)
            .withLocale(locale)
    }

    private fun Instant.toTemporal(): Temporal {
        return JavaLocalDateTime.ofInstant(toJavaInstant(), ZoneId.systemDefault())
    }

    actual fun formatShortDate(instant: Instant): String {
        return shortDate.format(instant.toTemporal())
    }

    actual fun formatShortDate(date: LocalDate): String {
        return shortDate.format(date.toJavaLocalDate())
    }

    actual fun formatMediumDate(instant: Instant): String {
        return mediumDate.format(instant.toTemporal())
    }

    actual fun formatMediumDateTime(instant: Instant): String {
        return mediumDateTime.format(instant.toTemporal())
    }

    actual fun formatShortTime(localTime: LocalTime): String {
        return shortTime.format(localTime.toJavaLocalTime())
    }

    actual fun formatShortRelativeTime(date: Instant, reference: Instant): String {
        // TODO: Look at using RelativeDateTimeFormatter
        return formatShortDate(date)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant