Skip to content

Commit

Permalink
Introduce korlibs-time module (#2045)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Nov 26, 2023
1 parent 3d4823e commit cbf8ef8
Show file tree
Hide file tree
Showing 102 changed files with 58 additions and 37 deletions.
1 change: 1 addition & 0 deletions korge-foundation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
//add("commonMainApi", libs.kotlinx.atomicfu)
//add("commonTestApi", project(":korge-test"))
add("commonTestApi", libs.kotlinx.coroutines.test)
add("commonMainApi", project(":korlibs-time"))
}

//korlibs.korge.gradle.generate.TemplateGenerator.synchronize(new File(projectDir, "template"))
Expand Down
1 change: 1 addition & 0 deletions korlibs-time/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
11 changes: 11 additions & 0 deletions korlibs-time/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import korlibs.*

description = "Korlibs Time - Former Klock"

project.extensions.extraProperties.properties.apply {
applyProjectProperties(
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-time",
"Public Domain",
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-time/LICENSE"
)
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package korlibs.time

import korlibs.math.*
import korlibs.time.DateTime.Companion.EPOCH
import korlibs.time.internal.*
import kotlin.jvm.JvmInline
Expand Down Expand Up @@ -116,14 +115,14 @@ value class DateTime(
second: Int = 0,
milliseconds: Int = 0
): DateTime {
val clampedMonth = month.clamp(1, 12)
val clampedMonth = month.coerceIn(1, 12)
return createUnchecked(
year = year,
month = clampedMonth,
day = day.clamp(1, Month(month).days(year)),
hour = hour.clamp(0, 23),
minute = minute.clamp(0, 59),
second = second.clamp(0, 59),
day = day.coerceIn(1, Month(month).days(year)),
hour = hour.coerceIn(0, 23),
minute = minute.coerceIn(0, 59),
second = second.coerceIn(0, 59),
milliseconds = milliseconds
)
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package korlibs.time

import korlibs.math.*
import korlibs.time.DayOfWeek.Friday
import korlibs.time.DayOfWeek.Monday
import korlibs.time.DayOfWeek.Saturday
import korlibs.time.DayOfWeek.Sunday
import korlibs.time.DayOfWeek.Thursday
import korlibs.time.DayOfWeek.Tuesday
import korlibs.time.DayOfWeek.Wednesday
import korlibs.time.internal.Serializable
import korlibs.time.internal.*

/** Represents the day of the week. [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday]. */
enum class DayOfWeek(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package korlibs.time

import korlibs.math.*
import korlibs.time.internal.*
import kotlin.jvm.*

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
package korlibs.time

import korlibs.math.*
import korlibs.time.Month.April
import korlibs.time.Month.August
import korlibs.time.Month.December
import korlibs.time.Month.February
import korlibs.time.Month.January
import korlibs.time.Month.July
import korlibs.time.Month.June
import korlibs.time.Month.March
import korlibs.time.Month.May
import korlibs.time.Month.November
import korlibs.time.Month.October
import korlibs.time.Month.September
import korlibs.time.internal.Serializable
import kotlin.math.abs
import korlibs.time.Month.*
import korlibs.time.internal.*
import kotlin.math.*

/** Represents one of the twelve months of the year. */
enum class Month(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package korlibs.time

import korlibs.math.*
import korlibs.time.internal.*
import korlibs.time.internal.MicroStrReader
import korlibs.time.internal.Serializable
import korlibs.time.internal.increment
import korlibs.time.internal.padded
import korlibs.time.internal.readTimeZoneOffset
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package korlibs.time

import korlibs.math.*
import korlibs.time.internal.*
import korlibs.time.internal.MicroStrReader
import korlibs.time.internal.Serializable
import korlibs.time.internal.increment
import korlibs.time.internal.padded
import korlibs.time.internal.substr
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package korlibs.time

import korlibs.datastructure.*

/**
* Class to count the number of times a sample was added in a timeWindow
*/
class TimeSampler(var timeWindow: TimeSpan = 1.seconds) {
private val events = DoubleDeque()
private val events = ArrayDeque<Double>()

val count: Int get() = events.size

fun add() {
val now = DateTime.now()
events.add(now.unixMillisDouble)
while (events.first < (now - timeWindow).unixMillisDouble) {
while (events.first() < (now - timeWindow).unixMillisDouble) {
events.removeFirst()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package korlibs.time

import korlibs.math.*
import korlibs.number.*
import korlibs.time.internal.*
import kotlin.jvm.*
import kotlin.math.*
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package korlibs.time.internal

import korlibs.math.*
import korlibs.time.*
import kotlin.jvm.*
import kotlin.math.*
Expand Down Expand Up @@ -66,8 +65,8 @@ internal fun Double.padded(intCount: Int, decCount: Int): String {
}

internal fun String.substr(start: Int, length: Int = this.length): String {
val low = (if (start >= 0) start else this.length + start).clamp(0, this.length)
val high = (if (length >= 0) low + length else this.length + length).clamp(0, this.length)
val low = (if (start >= 0) start else this.length + start).coerceIn(0, this.length)
val high = (if (length >= 0) low + length else this.length + length).coerceIn(0, this.length)
return if (high < low) "" else this.substring(low, high)
}

Expand Down
29 changes: 29 additions & 0 deletions korlibs-time/src/korlibs/time/internal/KlockInternal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package korlibs.time.internal

import kotlin.math.*

internal infix fun Int.umod(other: Int): Int {
val rm = this % other
val remainder = if (rm == -0) 0 else rm
return when {
remainder < 0 -> remainder + other
else -> remainder
}
}
internal infix fun Double.umod(other: Double): Double {
val rm = this % other
val remainder = if (rm == -0.0) 0.0 else rm
return when {
remainder < 0.0 -> remainder + other
else -> remainder
}
}
internal infix fun Int.div2(other: Int): Int = when {
this < 0 || this % other == 0 -> this / other
else -> (this / other) - 1
}

internal fun Int.cycle(min: Int, max: Int): Int = ((this - min) umod (max - min + 1)) + min
internal fun Int.cycleSteps(min: Int, max: Int): Int = (this - min) / (max - min + 1)
internal fun Double.toInt2(): Int = if (this < 0.0) floor(this).toInt() else this.toInt()
internal fun Double.toIntMod(mod: Int): Int = (this umod mod.toDouble()).toInt2()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fun isPropertyTrue(name: String): Boolean {
val inCI = isPropertyTrue("CI")
val disabledExtraKorgeLibs = isPropertyTrue("DISABLED_EXTRA_KORGE_LIBS")

include(":korlibs-time")
include(":korge-foundation")
include(":korge-core")
include(":korge")
Expand Down

0 comments on commit cbf8ef8

Please sign in to comment.