Removed most now unused DateFormatter functions

This commit is contained in:
dankito 2024-08-19 10:32:29 +02:00
parent cf6650e3e8
commit e9cad57e71
12 changed files with 6 additions and 270 deletions

View File

@ -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")
}

View File

@ -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"
}
}

View File

@ -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

View File

@ -1,14 +0,0 @@
package net.dankito.utils.multiplatform
enum class DateFormatStyle {
Short,
Medium,
Long,
Full
}

View File

@ -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?
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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()
}
}

View File

@ -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
}
}