Fixed JavaScript naming conflicts

This commit is contained in:
dankito 2023-07-06 15:23:19 +02:00
parent da212106a8
commit d702c605b2
7 changed files with 26 additions and 12 deletions

View File

@ -15,7 +15,8 @@ expect class DateFormatter constructor(pattern: String) {
fun format(date: LocalDateTime): String
fun format(date: LocalDate): String
// cannot be named format() due to JavaScript name conflicts
fun formatDate(date: LocalDate): String
fun parseDate(dateString: String): LocalDate?

View File

@ -2,23 +2,28 @@ package net.dankito.utils.multiplatform.extensions
import kotlinx.datetime.*
import net.dankito.utils.multiplatform.DateFormatter
import kotlin.js.JsName
val LocalDate.Companion.atUnixEpochStart: LocalDate
get() = fromEpochMillisecondsAtUtc(0)
@JsName("dateFromEpochMillisecondsAtUtc")
fun LocalDate.Companion.fromEpochMillisecondsAtUtc(epochMilliseconds: Long): LocalDate {
return fromEpochMilliseconds(epochMilliseconds, TimeZone.UTC)
}
@JsName("dateFromEpochMillisecondsAtSystemDefaultTimeZone")
fun LocalDate.Companion.fromEpochMillisecondsAtSystemDefaultTimeZone(epochMilliseconds: Long): LocalDate {
return fromEpochMilliseconds(epochMilliseconds, TimeZone.currentSystemDefault())
}
@JsName("dateFromEpochMillisecondsAtEuropeBerlin")
fun LocalDate.Companion.fromEpochMillisecondsAtEuropeBerlin(epochMilliseconds: Long): LocalDate {
return fromEpochMilliseconds(epochMilliseconds, TimeZone.europeBerlin)
}
@JsName("dateFromEpochMilliseconds")
fun LocalDate.Companion.fromEpochMilliseconds(epochMilliseconds: Long, timeZone: TimeZone): LocalDate {
return Instant.fromEpochMilliseconds(epochMilliseconds).toLocalDateTime(timeZone).date
}
@ -36,24 +41,27 @@ fun LocalDate.Companion.todayAtEuropeBerlin(): LocalDate {
return nowAt(TimeZone.europeBerlin)
}
@JsName("nowAtForTimeZoneStringForDate")
fun LocalDate.Companion.nowAt(timeZone: String): LocalDate {
return nowAt(TimeZone.of(timeZone))
}
@JsName("nowAtForDate")
fun LocalDate.Companion.nowAt(timeZone: TimeZone): LocalDate {
return Clock.System.todayAt(timeZone)
}
val LocalDate.millisSinceEpochAtUtc: Long
get() = this.toEpochMillisecondsAt(TimeZone.UTC)
val LocalDate.millisSinceEpochAtSystemDefaultTimeZone: Long
get() = this.toEpochMillisecondsAt(TimeZone.currentSystemDefault())
//val LocalDate.millisSinceEpochAtUtc: Long
// get() = this.toEpochMillisecondsAt(TimeZone.UTC)
//
//val LocalDate.millisSinceEpochAtSystemDefaultTimeZone: Long
// get() = this.toEpochMillisecondsAt(TimeZone.currentSystemDefault())
val LocalDate.millisSinceEpochAtEuropeBerlin: Long
get() = this.toEpochMillisecondsAt(TimeZone.europeBerlin)
@JsName("toEpochMillisecondsAtForDate")
fun LocalDate.toEpochMillisecondsAt(timeZone: TimeZone): Long {
return this.toLocalDateTime().toInstant(timeZone).toEpochMilliseconds()
}
@ -72,10 +80,12 @@ fun LocalDate.minusDays(days: Int): LocalDate {
}
@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

@ -2,6 +2,7 @@ package net.dankito.utils.multiplatform.extensions
import kotlinx.datetime.*
import net.dankito.utils.multiplatform.DateFormatter
import kotlin.js.JsName
fun LocalDateTime.Companion.of(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime {
@ -37,6 +38,7 @@ fun LocalDateTime.Companion.nowAtEuropeBerlin(): LocalDateTime {
return nowAt(TimeZone.europeBerlin)
}
@JsName("nowAtForTimeZoneString")
fun LocalDateTime.Companion.nowAt(timeZone: String): LocalDateTime {
return nowAt(TimeZone.of(timeZone))
}
@ -52,8 +54,8 @@ val LocalDateTime.millisSinceEpochAtUtc: Long
val LocalDateTime.millisSinceEpochAtSystemDefaultTimeZone: Long
get() = this.toEpochMillisecondsAt(TimeZone.currentSystemDefault())
val LocalDateTime.millisSinceEpochAtEuropeBerlin: Long
get() = this.toEpochMillisecondsAt(TimeZone.europeBerlin)
//val LocalDateTime.millisSinceEpochAtEuropeBerlin: Long
// get() = this.toEpochMillisecondsAt(TimeZone.europeBerlin)
fun LocalDateTime.toEpochMillisecondsAt(timeZone: TimeZone): Long {
return this.toInstant(timeZone).toEpochMilliseconds()
@ -64,6 +66,7 @@ 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

@ -36,7 +36,7 @@ actual class DateFormatter actual constructor(val pattern: String): NSDateFormat
}
actual fun format(date: LocalDate): String {
actual fun formatDate(date: LocalDate): String {
return format(date.toLocalDateTime())
}

View File

@ -11,7 +11,7 @@ actual class DateFormatter actual constructor(pattern: String) {
actual constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) : this("")
actual fun format(date: LocalDate): String {
actual fun formatDate(date: LocalDate): String {
return format(date.toLocalDateTime())
}

View File

@ -29,7 +29,7 @@ actual class DateFormatter actual constructor(pattern: String) {
: this((DateFormat.getDateTimeInstance(dateStyle.convert(), timeStyle.convert()) as? SimpleDateFormat)?.toPattern() ?: "")
actual fun format(date: LocalDate): String {
actual fun formatDate(date: LocalDate): String {
return format(date.toLocalDateTime())
}

View File

@ -11,7 +11,7 @@ actual class DateFormatter actual constructor(pattern: String) {
actual constructor(dateStyle: DateFormatStyle, timeStyle: DateFormatStyle) : this("")
actual fun format(date: LocalDate): String {
actual fun formatDate(date: LocalDate): String {
return format(date.toLocalDateTime())
}