From e9cad57e71745e6e51a82dc48042f7ac3e1961be Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 19 Aug 2024 10:32:29 +0200 Subject: [PATCH] Removed most now unused DateFormatter functions --- .../implementierte/sepa/SepaMessageCreator.kt | 3 - .../fints/model/CreditCardTransaction.kt | 3 +- .../fints/transactions/mt940/Mt940Parser.kt | 2 +- .../utils/multiplatform/DateFormatStyle.kt | 14 --- .../utils/multiplatform/DateFormatter.kt | 13 --- .../extensions/LocalDateExtensions.kt | 12 --- .../extensions/LocalDateTimeExtensions.kt | 11 --- .../utils/multiplatform/DateFormatter.kt | 43 +------- .../extensions/NSDateExtensions.kt | 99 ------------------- .../utils/multiplatform/DateFormatter.kt | 20 ---- .../utils/multiplatform/DateFormatter.kt | 35 +------ .../DateFormatter.kt | 21 +--- 12 files changed, 6 insertions(+), 270 deletions(-) delete mode 100644 multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatStyle.kt delete mode 100644 multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/NSDateExtensions.kt diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/segmente/implementierte/sepa/SepaMessageCreator.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/segmente/implementierte/sepa/SepaMessageCreator.kt index aaebb4b3..84145d6b 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/segmente/implementierte/sepa/SepaMessageCreator.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/messages/segmente/implementierte/sepa/SepaMessageCreator.kt @@ -2,7 +2,6 @@ package net.dankito.banking.fints.messages.segmente.implementierte.sepa import kotlinx.datetime.LocalDateTime import net.dankito.utils.multiplatform.extensions.nowAtUtc -import net.dankito.utils.multiplatform.DateFormatter /** @@ -32,8 +31,6 @@ open class SepaMessageCreator : ISepaMessageCreator { const val PaymentInformationIdKey = "PaymentInformationId" const val NumberOfTransactionsKey = "NumberOfTransactions" - - val IsoDateFormat = DateFormatter("yyyy-MM-dd'T'HH:mm:ss.SSS") } diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/CreditCardTransaction.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/CreditCardTransaction.kt index 7cd10120..65e9729a 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/CreditCardTransaction.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/CreditCardTransaction.kt @@ -1,7 +1,6 @@ package net.dankito.banking.fints.model import kotlinx.datetime.LocalDate -import net.dankito.utils.multiplatform.extensions.format open class CreditCardTransaction( @@ -29,7 +28,7 @@ open class CreditCardTransaction( override fun toString(): String { - return "${valueDate.format("dd.MM.yy")} $amount $description" + return "$valueDate $amount $description" } } \ No newline at end of file diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt index 5ab303fa..0ee00f9c 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/transactions/mt940/Mt940Parser.kt @@ -52,7 +52,7 @@ open class Mt940Parser( const val ClosingBalanceCode = "62" - val DateFormatter = DateFormatter("yyMMdd") + val DateFormatter = DateFormatter("yyMMdd") // TODO: replace with LocalDate.Format { } val CurrentYearTwoDigit = LocalDate.todayAtEuropeBerlin().year - 2000 diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatStyle.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatStyle.kt deleted file mode 100644 index cdc20e94..00000000 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatStyle.kt +++ /dev/null @@ -1,14 +0,0 @@ -package net.dankito.utils.multiplatform - - -enum class DateFormatStyle { - - Short, - - Medium, - - Long, - - Full - -} \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt index 3c9e116e..f0580d7e 100644 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt +++ b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt @@ -1,25 +1,12 @@ package net.dankito.utils.multiplatform import kotlinx.datetime.LocalDate -import kotlinx.datetime.LocalDateTime /** * Be aware that Java DateFormat is not thread safe! */ expect class DateFormatter constructor(pattern: String) { - constructor(dateStyle: DateFormatStyle) - - constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) - - - fun format(date: LocalDateTime): String - - // cannot be named format() due to JavaScript name conflicts - fun formatDate(date: LocalDate): String - fun parseDate(dateString: String): LocalDate? - fun parse(dateString: String): LocalDateTime? - } \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateExtensions.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateExtensions.kt index 3d01a1fd..0a310cf8 100644 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateExtensions.kt +++ b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateExtensions.kt @@ -1,7 +1,6 @@ package net.dankito.utils.multiplatform.extensions import kotlinx.datetime.* -import net.dankito.utils.multiplatform.DateFormatter import kotlin.js.JsName @@ -77,15 +76,4 @@ fun LocalDate.addDays(days: Int): LocalDate { fun LocalDate.minusDays(days: Int): LocalDate { return this.minus(days, DateTimeUnit.DAY) -} - - -@JsName("formatDate") -fun LocalDate.format(formatter: DateFormatter): String { - return this.atTime(0, 0).format(formatter) -} - -@JsName("formatDatePattern") -fun LocalDate.format(pattern: String): String { - return this.format(DateFormatter(pattern)) } \ No newline at end of file diff --git a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateTimeExtensions.kt b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateTimeExtensions.kt index b0b67b51..47953c5a 100644 --- a/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateTimeExtensions.kt +++ b/multiplatform-utils/src/commonMain/kotlin/net/dankito/utils/multiplatform/extensions/LocalDateTimeExtensions.kt @@ -1,7 +1,6 @@ package net.dankito.utils.multiplatform.extensions import kotlinx.datetime.* -import net.dankito.utils.multiplatform.DateFormatter import kotlin.js.JsName @@ -59,14 +58,4 @@ fun LocalDateTime.Companion.nowAt(timeZone: TimeZone): LocalDateTime { fun LocalDateTime.toEpochMillisecondsAt(timeZone: TimeZone): Long { return this.toInstant(timeZone).toEpochMilliseconds() -} - - -fun LocalDateTime.format(formatter: DateFormatter): String { - return formatter.format(this) -} - -@JsName("formatPattern") -fun LocalDateTime.format(pattern: String): String { - return this.format(DateFormatter(pattern)) } \ No newline at end of file diff --git a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt b/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt index 2470baaa..53c990e0 100644 --- a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt +++ b/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt @@ -2,57 +2,16 @@ package net.dankito.utils.multiplatform import kotlinx.datetime.* import net.dankito.utils.multiplatform.extensions.toLocalDateTime -import net.dankito.utils.multiplatform.extensions.toNSDate import platform.Foundation.* -fun DateFormatStyle.convert(): ULong { - return when (this) { - DateFormatStyle.Short -> NSDateFormatterShortStyle - DateFormatStyle.Medium -> NSDateFormatterMediumStyle - DateFormatStyle.Long -> NSDateFormatterLongStyle - DateFormatStyle.Full -> NSDateFormatterFullStyle - } -} - - actual class DateFormatter actual constructor(val pattern: String): NSDateFormatter() { - actual constructor(dateStyle: DateFormatStyle) : this(NSDateFormatter().apply { - this.dateStyle = dateStyle.convert() - }.dateFormat) // TODO: does this work? - - actual constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) : this(NSDateFormatter().apply { - this.dateStyle = dateStyle.convert() - this.timeStyle = timeStyle.convert() - }.dateFormat) // TODO: does this work? - - - init { - this.dateFormat = pattern - - this.timeZone = NSTimeZone.localTimeZone // TODO: needed? - } - - - actual fun formatDate(date: LocalDate): String { - return format(date.toLocalDateTime()) - } - - actual fun format(date: LocalDateTime): String { - val nsDate = date.toNSDate() - - return this.stringFromDate(nsDate) - } actual fun parseDate(dateString: String): LocalDate? { - return parse(dateString)?.date - } - - actual fun parse(dateString: String): LocalDateTime? { this.dateFromString(dateString)?.let { nsDate -> - return nsDate.toKotlinInstant().toLocalDateTime(TimeZone.currentSystemDefault()) + return nsDate.toKotlinInstant().toLocalDateTime(TimeZone.currentSystemDefault())?.date } return null diff --git a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/NSDateExtensions.kt b/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/NSDateExtensions.kt deleted file mode 100644 index bbe1516b..00000000 --- a/multiplatform-utils/src/iosMain/kotlin/net/dankito/utils/multiplatform/extensions/NSDateExtensions.kt +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2019-2020 JetBrains s.r.o. - * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. - */ - -package net.dankito.utils.multiplatform.extensions - -import kotlinx.datetime.* -import kotlinx.cinterop.* -import platform.Foundation.* - -/** - * Converts the [Instant] to an instance of [NSDate]. - * - * The conversion is lossy: Darwin uses millisecond precision to represent dates, and [Instant] allows for nanosecond - * resolution. - */ -fun Instant.toNSDate(): NSDate { - val secs = epochSeconds * 1.0 + nanosecondsOfSecond / 1.0e9 - if (secs < NSDate.distantPast.timeIntervalSince1970 || secs > NSDate.distantFuture.timeIntervalSince1970) { - throw IllegalArgumentException("Boundaries of NSDate exceeded") - } - return NSDate.dateWithTimeIntervalSince1970(secs) -} - -/** - * Converts the [NSDate] to the corresponding [Instant]. - * - * Even though Darwin only uses millisecond precision, it is possible that [date] uses larger resolution, storing - * microseconds or even nanoseconds. In this case, the sub-millisecond parts of [date] are rounded to the nearest - * millisecond, given that they are likely to be conversion artifacts. - */ -fun NSDate.toKotlinInstant(): Instant { - val secs = timeIntervalSince1970() - val millis = secs * 1000 + if (secs > 0) 0.5 else -0.5 - return Instant.fromEpochMilliseconds(millis.toLong()) -} - - -fun LocalDateTime.toNSDate(): NSDate { - val instant = this.toInstant(TimeZone.currentSystemDefault()) - - return instant.toNSDate() -} - -fun LocalDate.toNSDate(): NSDate { - return this.toLocalDateTime().toNSDate() -} - - -/** - * Converts the [TimeZone] to [NSTimeZone]. - * - * If the time zone is represented as a fixed number of seconds from UTC+0 (for example, if it is the result of a call - * to [TimeZone.offset]) and the offset is not given in even minutes but also includes seconds, this method throws - * [DateTimeException] to denote that lossy conversion would happen, as Darwin internally rounds the offsets to the - * nearest minute. - */ -fun TimeZone.toNSTimeZone(): NSTimeZone = if (this is ZoneOffset) { - require (totalSeconds % 60 == 0) { - "Lossy conversion: Darwin uses minute precision for fixed-offset time zones" - } - NSTimeZone.timeZoneForSecondsFromGMT(totalSeconds.convert()) -} else { - NSTimeZone.timeZoneWithName(id) ?: NSTimeZone.timeZoneWithAbbreviation(id)!! -} - -/** - * Converts the [NSTimeZone] to the corresponding [TimeZone]. - */ -fun NSTimeZone.toKotlinTimeZone(): TimeZone = TimeZone.of(name) - -/** - * Converts the given [LocalDate] to [NSDateComponents]. - * - * Of all the fields, only the bare minimum required for uniquely identifying the date are set. - */ -fun LocalDate.toNSDateComponents(): NSDateComponents { - val components = NSDateComponents() - components.year = year.convert() - components.month = monthNumber.convert() - components.day = dayOfMonth.convert() - return components -} - -/** - * Converts the given [LocalDate] to [NSDateComponents]. - * - * Of all the fields, only the bare minimum required for uniquely identifying the date and time are set. - */ -public fun LocalDateTime.toNSDateComponents(): NSDateComponents { - val components = date.toNSDateComponents() - components.hour = hour.convert() - components.minute = minute.convert() - components.second = second.convert() - components.nanosecond = nanosecond.convert() - return components -} - diff --git a/multiplatform-utils/src/jsMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt b/multiplatform-utils/src/jsMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt index a0f00e83..7736b7c1 100644 --- a/multiplatform-utils/src/jsMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt +++ b/multiplatform-utils/src/jsMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt @@ -1,31 +1,11 @@ package net.dankito.utils.multiplatform import kotlinx.datetime.LocalDate -import kotlinx.datetime.LocalDateTime -import net.dankito.utils.multiplatform.extensions.toLocalDateTime actual class DateFormatter actual constructor(pattern: String) { - actual constructor(dateStyle: DateFormatStyle) : this("") - - actual constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) : this("") - - - actual fun formatDate(date: LocalDate): String { - return format(date.toLocalDateTime()) - } - - // TODO: implement for Logger, get current time formatted as string - actual fun format(date: LocalDateTime): String { - return "" // is only used in rare cases, don't implement right now - } - actual fun parseDate(dateString: String): LocalDate? { return null // is only used in rare cases, don't implement right now } - actual fun parse(dateString: String): LocalDateTime? { - return null // is only used in rare cases, don't implement right now - } - } \ No newline at end of file diff --git a/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt b/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt index 10c5278a..389d96bd 100644 --- a/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt +++ b/multiplatform-utils/src/jvmMain/kotlin/net/dankito/utils/multiplatform/DateFormatter.kt @@ -1,48 +1,17 @@ package net.dankito.utils.multiplatform -import kotlinx.datetime.* -import net.dankito.utils.multiplatform.extensions.toLocalDateTime -import java.text.DateFormat -import java.text.SimpleDateFormat +import kotlinx.datetime.LocalDate +import kotlinx.datetime.toKotlinLocalDate import java.time.format.DateTimeFormatter -fun DateFormatStyle.convert(): Int { - return when (this) { - DateFormatStyle.Short -> DateFormat.SHORT - DateFormatStyle.Medium -> DateFormat.MEDIUM - DateFormatStyle.Long -> DateFormat.LONG - DateFormatStyle.Full -> DateFormat.FULL - } -} - - actual class DateFormatter actual constructor(pattern: String) { private val formatter = DateTimeFormatter.ofPattern(pattern) - actual constructor(dateStyle: DateFormatStyle) - : this((DateFormat.getDateInstance(dateStyle.convert()) as? SimpleDateFormat)?.toPattern() ?: "") - - actual constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) - : this((DateFormat.getDateTimeInstance(dateStyle.convert(), timeStyle.convert()) as? SimpleDateFormat)?.toPattern() ?: "") - - - actual fun formatDate(date: LocalDate): String { - return format(date.toLocalDateTime()) - } - - actual fun format(date: LocalDateTime): String { - return formatter.format(date.toJavaLocalDateTime()) - } - actual fun parseDate(dateString: String): LocalDate? { return java.time.LocalDate.parse(dateString, formatter)?.toKotlinLocalDate() } - actual fun parse(dateString: String): LocalDateTime? { - return java.time.LocalDateTime.parse(dateString, formatter)?.toKotlinLocalDateTime() - } - } \ No newline at end of file diff --git a/multiplatform-utils/src/nativeMain/kotlin/net.dankito.utils.multiplatform/DateFormatter.kt b/multiplatform-utils/src/nativeMain/kotlin/net.dankito.utils.multiplatform/DateFormatter.kt index c6342723..7222f061 100644 --- a/multiplatform-utils/src/nativeMain/kotlin/net.dankito.utils.multiplatform/DateFormatter.kt +++ b/multiplatform-utils/src/nativeMain/kotlin/net.dankito.utils.multiplatform/DateFormatter.kt @@ -1,31 +1,12 @@ package net.dankito.utils.multiplatform -import kotlinx.datetime.* -import net.dankito.utils.multiplatform.extensions.toLocalDateTime +import kotlinx.datetime.LocalDate actual class DateFormatter actual constructor(pattern: String) { - actual constructor(dateStyle: DateFormatStyle) : this("") - - actual constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) : this("") - - - actual fun formatDate(date: LocalDate): String { - return format(date.toLocalDateTime()) - } - - // TODO: implement for Logger, get current time formatted as string - actual fun format(date: LocalDateTime): String { - return "" // is only used in rare cases, don't implement right now - } - actual fun parseDate(dateString: String): LocalDate? { return null // is only used in rare cases, don't implement right now } - actual fun parse(dateString: String): LocalDateTime? { - return null // is only used in rare cases, don't implement right now - } - } \ No newline at end of file