# LocalDateTimeExtensions.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.shared.util.extensions

    import java.time.LocalDateTime
    import java.time.LocalTime
    import java.time.format.DateTimeFormatter
    import java.time.format.FormatStyle
    import java.time.temporal.ChronoUnit

    fun LocalDateTime.toDefaultMonthDisplay(): String {
        return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(this)
    }

    fun LocalDateTime.toPrettyString(): String =
        format(DateTimeFormatter.ofPattern("d. MMMM yyyy 'kl.' HH:mm"))

    fun LocalDateTime?.toMediumFormat(): String? =
        this?.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))

    // Monday, 01 January
    fun LocalDateTime?.toLongFormat(): String {
        this ?: return ""
        return format(DateTimeFormatter.ofPattern("EEEE, d. MMMM"))
            .replaceFirstChar { char -> char.uppercase() }
    }

    // Monday, 01 January 2020
    fun LocalDateTime?.toFullDateFormat(): String {
        this ?: return ""
        return format(DateTimeFormatter.ofPattern("EEEE, dd. MMMM yyyy"))
            .replaceFirstChar { char -> char.uppercase() }
    }

    fun LocalDateTime.toTimeFormat(): String =
        format(DateTimeFormatter.ofPattern("HH:mm"))

    fun LocalDateTime.toLongMonthFormat(): String =
        format(DateTimeFormatter.ofPattern("MMMM")).replaceFirstChar { char -> char.uppercase() }

    fun LocalDateTime.withTimeAtStartOfDay(): LocalDateTime = with(LocalTime.MIN)

    fun LocalDateTime?.isBeforeNow(): Boolean {
        return this?.isBefore(LocalDateTime.now()) == true
    }

    fun LocalDateTime?.isAfterNow(): Boolean {
        return this?.isAfter(LocalDateTime.now()) == true
    }

    fun LocalDateTime?.isBeforeNow(bySeconds: Long): Boolean {
        this ?: return true

        return this.plusSeconds(bySeconds).isBeforeNow()
    }

    fun LocalDateTime.weekNumberAfterBirthDate(): Int {
        val weeksBetween = ChronoUnit.WEEKS.between(
            this,
            LocalDateTime.now()
        ).toInt()

        // The week with the date of birth should be week 1 (hence the + 1)!
        return weeksBetween + 1
    }