Skip to content

Commit

Permalink
Prepare 2.0.0-alpha (soywiz-archive#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Oct 8, 2020
1 parent 7c0d38c commit 5147363
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 81 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
}

dependencies {
classpath("com.soywiz.korlibs:easy-kotlin-mpp-gradle-plugin:0.10.0") // Kotlin 1.4: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
classpath("com.soywiz.korlibs:easy-kotlin-mpp-gradle-plugin:0.10.2") // Kotlin 1.4.10: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kotlin.code.style=official

# version
group=com.soywiz.korlibs.klock
version=1.12.0-SNAPSHOT
version=2.0.0-SNAPSHOT

# bintray location
project.bintray.org=korlibs
Expand Down
6 changes: 4 additions & 2 deletions klock/src/commonMain/kotlin/com/soywiz/klock/DateTimeSpan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ data class DateTimeSpan(
operator fun minus(other: DateTimeSpan) = this + -other

operator fun times(times: Double) = DateTimeSpan((monthSpan * times), (timeSpan * times))
operator fun div(times: Double) = times(1.0 / times)

operator fun times(times: Int) = this * times.toDouble()
operator fun times(times: Float) = this * times.toDouble()

operator fun div(times: Double) = times(1.0 / times)
operator fun div(times: Int) = this / times.toDouble()
operator fun div(times: Float) = this / times.toDouble()

/** From the date part, all months represented as a [totalYears] [Double] */
val totalYears: Double get() = monthSpan.totalYears
Expand Down
4 changes: 2 additions & 2 deletions klock/src/commonMain/kotlin/com/soywiz/klock/Frequency.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.soywiz.klock

import com.soywiz.klock.hr.hrSeconds
import com.soywiz.klock.hr.hr

val TimeSpan.hz get() = timesPerSecond
val Int.hz get() = timesPerSecond
Expand All @@ -18,5 +18,5 @@ inline class Frequency(val hertz: Double) {
}

val timeSpan get() = (1.0 / this.hertz).seconds
val hrTimeSpan get() = (1.0 / this.hertz).hrSeconds
val hrTimeSpan get() = (1.0 / this.hertz).seconds.hr
}
6 changes: 4 additions & 2 deletions klock/src/commonMain/kotlin/com/soywiz/klock/MonthSpan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ inline class MonthSpan(
operator fun minus(other: DateTimeSpan) = this + -other

operator fun times(times: Double) = MonthSpan((totalMonths * times).toInt())
operator fun div(times: Double) = MonthSpan((totalMonths / times).toInt())

operator fun times(times: Int) = this * times.toDouble()
operator fun times(times: Float) = this * times.toDouble()

operator fun div(times: Double) = MonthSpan((totalMonths / times).toInt())
operator fun div(times: Int) = this / times.toDouble()
operator fun div(times: Float) = this / times.toDouble()

override fun compareTo(other: MonthSpan): Int = this.totalMonths.compareTo(other.totalMonths)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ data class PatternDateFormat @JvmOverloads constructor(
}

fun withLocale(locale: KlockLocale?) = this.copy(locale = locale)
fun withTimezoneNames(tzNames: TimezoneNames) = this.copy(tzNames = this.tzNames + tzNames)
fun withTimezoneNames(tzNames: TimezoneNames) = this.copy(tzNames = this.tzNames + tzNames)
fun withOptions(options: Options) = this.copy(options = options)
fun withOptional() = this.copy(options = options.copy(optionalSupport = true))
fun withNonOptional() = this.copy(options = options.copy(optionalSupport = false))

private val openOffsets = LinkedHashMap<Int, Int>()
private val closeOffsets = LinkedHashMap<Int, Int>()

internal val chunks = arrayListOf<String>().also { chunks ->
internal val chunks = arrayListOf<String>().also { chunks ->
val s = MicroStrReader(format)
while (s.hasMore) {
if (s.peekChar() == '\'') {
Expand Down Expand Up @@ -66,7 +66,7 @@ data class PatternDateFormat @JvmOverloads constructor(
}
}.toList()

internal val regexChunks = chunks.map {
internal val regexChunks = chunks.map {
when (it) {
"E", "EE", "EEE", "EEEE", "EEEEE", "EEEEEE" -> """(\w+)"""
"z", "zzz" -> """([\w\s\-\+\:]+)"""
Expand Down Expand Up @@ -107,8 +107,8 @@ data class PatternDateFormat @JvmOverloads constructor(
}
}

//val escapedFormat = Regex.escape(format)
internal val rx2: Regex = Regex("^" + regexChunks.mapIndexed { index, it ->
//val escapedFormat = Regex.escape(format)
internal val rx2: Regex = Regex("^" + regexChunks.mapIndexed { index, it ->
if (options.optionalSupport) {
val opens = openOffsets.getOrElse(index) { 0 }
val closes = closeOffsets.getOrElse(index) { 0 }
Expand All @@ -123,7 +123,7 @@ data class PatternDateFormat @JvmOverloads constructor(
}.joinToString("") + "$")


// EEE, dd MMM yyyy HH:mm:ss z -- > Sun, 06 Nov 1994 08:49:37 GMT
// EEE, dd MMM yyyy HH:mm:ss z -- > Sun, 06 Nov 1994 08:49:37 GMT
// YYYY-MM-dd HH:mm:ss

override fun format(dd: DateTimeTz): String {
Expand Down Expand Up @@ -182,9 +182,9 @@ data class PatternDateFormat @JvmOverloads constructor(
}
"a" -> if (utc.hours < 12) "am" else "pm"
else -> when {
name.startsWith('\'') -> name.substring(1, name.length - 1)
else -> name
}
name.startsWith('\'') -> name.substring(1, name.length - 1)
else -> name
}
}
}
return out
Expand Down
23 changes: 23 additions & 0 deletions klock/src/commonMain/kotlin/com/soywiz/klock/TimeSpan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ inline val Long.days get() = TimeSpan.fromDays(this.toDouble())
/** [TimeSpan] representing this number as [weeks] or 604_800 [seconds]. */
inline val Long.weeks get() = TimeSpan.fromWeeks(this.toDouble())

/** [TimeSpan] representing this number as [nanoseconds] or 1 / 1_000_000_000 [seconds]. */
inline val Float.nanoseconds get() = TimeSpan.fromNanoseconds(this.toDouble())
/** [TimeSpan] representing this number as [microseconds] or 1 / 1_000_000 [seconds]. */
inline val Float.microseconds get() = TimeSpan.fromMicroseconds(this.toDouble())
/** [TimeSpan] representing this number as [milliseconds] or 1 / 1_000 [seconds]. */
inline val Float.milliseconds get() = TimeSpan.fromMilliseconds(this.toDouble())
/** [TimeSpan] representing this number as [seconds]. */
inline val Float.seconds get() = TimeSpan.fromSeconds((this.toDouble()))
/** [TimeSpan] representing this number as [minutes] or 60 [seconds]. */
inline val Float.minutes get() = TimeSpan.fromMinutes(this.toDouble())
/** [TimeSpan] representing this number as [hours] or 3_600 [seconds]. */
inline val Float.hours get() = TimeSpan.fromHours(this.toDouble())
/** [TimeSpan] representing this number as [days] or 86_400 [seconds]. */
inline val Float.days get() = TimeSpan.fromDays(this.toDouble())
/** [TimeSpan] representing this number as [weeks] or 604_800 [seconds]. */
inline val Float.weeks get() = TimeSpan.fromWeeks(this.toDouble())

/** [TimeSpan] representing this number as [nanoseconds] or 1 / 1_000_000_000 [seconds]. */
inline val Int.nanoseconds get() = TimeSpan.fromNanoseconds(this.toDouble())
/** [TimeSpan] representing this number as [microseconds] or 1 / 1_000_000 [seconds]. */
Expand Down Expand Up @@ -66,8 +83,14 @@ inline class TimeSpan(
) : Comparable<TimeSpan>, Serializable {
/** Returns the total number of [nanoseconds] for this [TimeSpan] (1 / 1_000_000_000 [seconds]) */
val nanoseconds: Double get() = this.milliseconds / MILLIS_PER_NANOSECOND
/** Returns the total number of [nanoseconds] for this [TimeSpan] (1 / 1_000_000_000 [seconds]) as Integer */
val nanosecondsInt: Int get() = (this.milliseconds / MILLIS_PER_NANOSECOND).toInt()

/** Returns the total number of [microseconds] for this [TimeSpan] (1 / 1_000_000 [seconds]) */
val microseconds: Double get() = this.milliseconds / MILLIS_PER_MICROSECOND
/** Returns the total number of [microseconds] for this [TimeSpan] (1 / 1_000_000 [seconds]) as Integer */
val microsecondsInt: Int get() = (this.milliseconds / MILLIS_PER_MICROSECOND).toInt()

/** Returns the total number of [seconds] for this [TimeSpan] */
val seconds: Double get() = this.milliseconds / MILLIS_PER_SECOND
/** Returns the total number of [minutes] for this [TimeSpan] (60 [seconds]) */
Expand Down
28 changes: 3 additions & 25 deletions klock/src/commonMain/kotlin/com/soywiz/klock/hr/HRTimeSpan.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.soywiz.klock.hr

import com.soywiz.klock.PerformanceCounter
import com.soywiz.klock.TimeSpan
import com.soywiz.klock.nanoseconds
import com.soywiz.klock.*
import kotlin.math.round

/** Converts a [TimeSpan] into a high-resolution [HRTimeSpan] */
Expand All @@ -11,21 +9,6 @@ val TimeSpan.hr get() = HRTimeSpan.fromMilliseconds(this.milliseconds)
/** Converts a [HRTimeSpan] into a low-resolution [TimeSpan] */
val HRTimeSpan.timeSpan get() = nanosecondsRaw.nanoseconds

val Long.hrSeconds get() = HRTimeSpan.fromSeconds(this)
val Long.hrMilliseconds get() = HRTimeSpan.fromMilliseconds(this)
val Long.hrMicroseconds get() = HRTimeSpan.fromMicroseconds(this)
val Long.hrNanoseconds get() = HRTimeSpan.fromNanoseconds(this)

val Int.hrSeconds get() = HRTimeSpan.fromSeconds(this)
val Int.hrMilliseconds get() = HRTimeSpan.fromMilliseconds(this)
val Int.hrMicroseconds get() = HRTimeSpan.fromMicroseconds(this)
val Int.hrNanoseconds get() = HRTimeSpan.fromNanoseconds(this)

val Double.hrSeconds get() = HRTimeSpan.fromSeconds(this)
val Double.hrMilliseconds get() = HRTimeSpan.fromMilliseconds(this)
val Double.hrMicroseconds get() = HRTimeSpan.fromMicroseconds(this)
val Double.hrNanoseconds get() = HRTimeSpan.fromNanoseconds(this)

// @TODO: Ensure nanosecondsRaw has no decimals
/** A High-Resolution TimeSpan that stores its values as nanoseconds. Just uses 52-bits and won't store decimals */
inline class HRTimeSpan constructor(val nanosecondsRaw: Double) : Comparable<HRTimeSpan> {
Expand All @@ -44,11 +27,6 @@ inline class HRTimeSpan constructor(val nanosecondsRaw: Double) : Comparable<HRT
fun fromMilliseconds(value: Int) = fromMilliseconds(value.toDouble())
fun fromMicroseconds(value: Int) = fromMicroseconds(value.toDouble())
fun fromNanoseconds(value: Int) = fromNanoseconds(value.toDouble())

fun fromSeconds(value: Long) = fromSeconds(value.toDouble())
fun fromMilliseconds(value: Long) = fromMilliseconds(value.toDouble())
fun fromMicroseconds(value: Long) = fromMicroseconds(value.toDouble())
fun fromNanoseconds(value: Long) = fromNanoseconds(value.toDouble())
}

val nanosecondsDouble get() = (nanosecondsRaw)
Expand All @@ -72,8 +50,8 @@ inline class HRTimeSpan constructor(val nanosecondsRaw: Double) : Comparable<HRT
override fun toString(): String = "$nanosecondsRaw".removeSuffix(".0") + " ns"
}

fun max(a: HRTimeSpan, b: HRTimeSpan): HRTimeSpan = kotlin.math.max(a.nanosecondsRaw, b.nanosecondsRaw).hrNanoseconds
fun min(a: HRTimeSpan, b: HRTimeSpan): HRTimeSpan = kotlin.math.min(a.nanosecondsRaw, b.nanosecondsRaw).hrNanoseconds
fun max(a: HRTimeSpan, b: HRTimeSpan): HRTimeSpan = HRTimeSpan.fromNanoseconds(kotlin.math.max(a.nanosecondsRaw, b.nanosecondsRaw))
fun min(a: HRTimeSpan, b: HRTimeSpan): HRTimeSpan = HRTimeSpan.fromNanoseconds(kotlin.math.min(a.nanosecondsRaw, b.nanosecondsRaw))
fun HRTimeSpan.clamp(min: HRTimeSpan, max: HRTimeSpan): HRTimeSpan = when {
this < min -> min
this > max -> max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ internal class Moduler(val value: Double) {
avalue %= count
return floor(ret) * sign
}
fun int(count: Double): Int = double(count).toInt()

fun double(count: Int): Double = double(count.toDouble())
fun double(count: Float): Double = double(count.toDouble())

fun int(count: Double): Int = double(count).toInt()
fun int(count: Int): Int = int(count.toDouble())
fun int(count: Float): Int = int(count.toDouble())
}

internal infix fun Double.intDiv(other: Double) = floor(this / other)
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ data class WMonthSpan(val value: MonthSpan) : Comparable<WMonthSpan>, Serializab
operator fun minus(other: WDateTimeSpan) = (this.value - other.value).wrapped

operator fun times(times: Double) = (value * times).wrapped
operator fun div(times: Double) = (value / times).wrapped

operator fun times(times: Float) = this * times.toDouble()
operator fun times(times: Int) = this * times.toDouble()

operator fun div(times: Double) = (value / times).wrapped
operator fun div(times: Float) = this / times.toDouble()
operator fun div(times: Int) = this / times.toDouble()

override fun compareTo(other: WMonthSpan): Int = this.value.compareTo(other.value)
Expand Down
66 changes: 33 additions & 33 deletions klock/src/commonTest/kotlin/com/soywiz/klock/DateTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ class DateTimeTest {
)
}

@Test
fun testNewParserBug1() {
DateFormat("EEE, dd MMMM yyyy HH:mm:ss z").parseLong("Sat, 08 September 2018 04:08:09 UTC")
}
@Test
fun testNewParserBug1() {
DateFormat("EEE, dd MMMM yyyy HH:mm:ss z").parseLong("Sat, 08 September 2018 04:08:09 UTC")
}

@Test
fun testParsingDateTimesInCustomStringFormatsWithAmPm() {
Expand Down Expand Up @@ -333,35 +333,35 @@ class DateTimeTest {
assertEquals(c, c.clamp(a, c))
}

@Test
fun testStartEndDay() {
val date = DateTime(1568803601377)
val start = date.dateDayStart
val end = date.dateDayEnd
assertEquals("2019-09-18T00:00:00", ISO8601.DATETIME_COMPLETE.extended.format(start))
assertEquals("2019-09-18T23:59:59", ISO8601.DATETIME_COMPLETE.extended.format(end))
assertEquals(1568764800000L, start.unixMillisLong)
assertEquals(1568851199999L, end.unixMillisLong)
}

@Test
fun testTimeZones() {
"Tue, 19 Sep 2017 00:58:45 GMT-0800".let { STR -> assertEquals(STR, HttpDate.parse(STR).toString()) }
"Tue, 19 Sep 2017 00:58:45 GMT+0800".let { STR -> assertEquals(STR, HttpDate.parse(STR).toString()) }
}

@Test
fun testBug37() {
val format = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
format.parse("2019-04-15T17:28:46.862+0900")
}

@Test
fun testBug33() {
assertEquals("20190412", DateTime(2019, 4, 12).localUnadjusted.format("yyyyMMdd"))
assertEquals("2019年04月12日", Date(2019, 4, 12).format("yyyy年MM月dd日"))
assertEquals("2019年04月12日", Date(2019, 4, 12).format("yyyy'年'MM'月'dd'日'"))
}
@Test
fun testStartEndDay() {
val date = DateTime(1568803601377)
val start = date.dateDayStart
val end = date.dateDayEnd
assertEquals("2019-09-18T00:00:00", ISO8601.DATETIME_COMPLETE.extended.format(start))
assertEquals("2019-09-18T23:59:59", ISO8601.DATETIME_COMPLETE.extended.format(end))
assertEquals(1568764800000L, start.unixMillisLong)
assertEquals(1568851199999L, end.unixMillisLong)
}

@Test
fun testTimeZones() {
"Tue, 19 Sep 2017 00:58:45 GMT-0800".let { STR -> assertEquals(STR, HttpDate.parse(STR).toString()) }
"Tue, 19 Sep 2017 00:58:45 GMT+0800".let { STR -> assertEquals(STR, HttpDate.parse(STR).toString()) }
}

@Test
fun testBug37() {
val format = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
format.parse("2019-04-15T17:28:46.862+0900")
}

@Test
fun testBug33() {
assertEquals("20190412", DateTime(2019, 4, 12).localUnadjusted.format("yyyyMMdd"))
assertEquals("2019年04月12日", Date(2019, 4, 12).format("yyyy年MM月dd日"))
assertEquals("2019年04月12日", Date(2019, 4, 12).format("yyyy'年'MM'月'dd'日'"))
}

@Test
fun testBug93() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.soywiz.klock.internal

import com.soywiz.klock.*
import com.soywiz.klock.hr.HRTimeSpan
import kotlin.browser.*
import kotlin.math.*
import kotlinx.browser.*

private external val process: dynamic

Expand Down

0 comments on commit 5147363

Please sign in to comment.