Skip to content

Implement LocalDate.fromEpochDays #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Split math.kt into math.kt and dateCalculations.kt
  • Loading branch information
dkhalanskyjb committed Jun 24, 2022
commit 82aff29a613f7f840e9344cee3cfbdd904a5c8fb
1 change: 1 addition & 0 deletions core/common/src/DateTimePeriod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.DatePeriodIso8601Serializer
import kotlinx.datetime.serializers.DateTimePeriodIso8601Serializer
import kotlin.math.*
Expand Down
1 change: 1 addition & 0 deletions core/common/src/DateTimeUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.safeMultiply
import kotlinx.datetime.serializers.*
import kotlinx.serialization.Serializable
import kotlin.time.*
Expand Down
1 change: 1 addition & 0 deletions core/common/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import kotlin.time.*
Expand Down
41 changes: 41 additions & 0 deletions core/common/src/internal/dateCalculations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime.internal

internal const val SECONDS_PER_HOUR = 60 * 60

internal const val SECONDS_PER_MINUTE = 60

internal const val MINUTES_PER_HOUR = 60

internal const val HOURS_PER_DAY = 24

internal const val SECONDS_PER_DAY: Int = SECONDS_PER_HOUR * HOURS_PER_DAY

internal const val NANOS_PER_ONE = 1_000_000_000
internal const val NANOS_PER_MILLI = 1_000_000
internal const val MILLIS_PER_ONE = 1_000

internal const val NANOS_PER_DAY: Long = NANOS_PER_ONE * SECONDS_PER_DAY.toLong()

internal const val NANOS_PER_MINUTE: Long = NANOS_PER_ONE * SECONDS_PER_MINUTE.toLong()

internal const val NANOS_PER_HOUR = NANOS_PER_ONE * SECONDS_PER_HOUR.toLong()

internal const val MILLIS_PER_DAY: Int = SECONDS_PER_DAY * MILLIS_PER_ONE

// org.threeten.bp.chrono.IsoChronology#isLeapYear
internal fun isLeapYear(year: Int): Boolean {
val prolepticYear: Long = year.toLong()
return prolepticYear and 3 == 0L && (prolepticYear % 100 != 0L || prolepticYear % 400 == 0L)
}

internal fun Int.monthLength(isLeapYear: Boolean): Int =
when (this) {
2 -> if (isLeapYear) 29 else 28
4, 6, 9, 11 -> 30
else -> 31
}
37 changes: 1 addition & 36 deletions core/common/src/math.kt → core/common/src/internal/math.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime
package kotlinx.datetime.internal

internal fun Long.clampToInt(): Int =
when {
Expand All @@ -12,28 +12,6 @@ internal fun Long.clampToInt(): Int =
else -> toInt()
}

internal const val SECONDS_PER_HOUR = 60 * 60

internal const val SECONDS_PER_MINUTE = 60

internal const val MINUTES_PER_HOUR = 60

internal const val HOURS_PER_DAY = 24

internal const val SECONDS_PER_DAY: Int = SECONDS_PER_HOUR * HOURS_PER_DAY

internal const val NANOS_PER_ONE = 1_000_000_000
internal const val NANOS_PER_MILLI = 1_000_000
internal const val MILLIS_PER_ONE = 1_000

internal const val NANOS_PER_DAY: Long = NANOS_PER_ONE * SECONDS_PER_DAY.toLong()

internal const val NANOS_PER_MINUTE: Long = NANOS_PER_ONE * SECONDS_PER_MINUTE.toLong()

internal const val NANOS_PER_HOUR = NANOS_PER_ONE * SECONDS_PER_HOUR.toLong()

internal const val MILLIS_PER_DAY: Int = SECONDS_PER_DAY * MILLIS_PER_ONE

internal expect fun safeMultiply(a: Long, b: Long): Long
internal expect fun safeMultiply(a: Int, b: Int): Int
internal expect fun safeAdd(a: Long, b: Long): Long
Expand Down Expand Up @@ -201,16 +179,3 @@ internal fun multiplyAndAdd(d: Long, n: Long, r: Long): Long {
}
return safeAdd(safeMultiply(md, n), mr)
}

// org.threeten.bp.chrono.IsoChronology#isLeapYear
internal fun isLeapYear(year: Int): Boolean {
val prolepticYear: Long = year.toLong()
return prolepticYear and 3 == 0L && (prolepticYear % 100 != 0L || prolepticYear % 400 == 0L)
}

internal fun Int.monthLength(isLeapYear: Boolean): Int =
when (this) {
2 -> if (isLeapYear) 29 else 28
4, 6, 9, 11 -> 30
else -> 31
}
1 change: 1 addition & 0 deletions core/common/test/InstantTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package kotlinx.datetime.test

import kotlinx.datetime.*
import kotlinx.datetime.Clock // currently, requires an explicit import due to a conflict with the deprecated Clock from kotlin.time
import kotlinx.datetime.internal.*
import kotlin.random.*
import kotlin.test.*
import kotlin.time.*
Expand Down
1 change: 1 addition & 0 deletions core/common/test/LocalDateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package kotlinx.datetime.test

import kotlinx.datetime.*
import kotlinx.datetime.internal.*
import kotlin.random.*
import kotlin.test.*

Expand Down
1 change: 1 addition & 0 deletions core/common/test/LocalTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package kotlinx.datetime.test

import kotlinx.datetime.*
import kotlinx.datetime.internal.*
import kotlin.math.*
import kotlin.random.*
import kotlin.test.*
Expand Down
2 changes: 1 addition & 1 deletion core/common/test/MultiplyAndDivideTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package kotlinx.datetime.test
import kotlin.random.*
import kotlin.test.*
import kotlinx.datetime.*
import kotlinx.datetime.internal.*

class MultiplyAndDivideTest {

Expand Down
2 changes: 2 additions & 0 deletions core/js/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
import kotlinx.datetime.internal.JSJoda.ChronoUnit
import kotlinx.datetime.internal.safeAdd
import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import kotlin.time.*
Expand Down
1 change: 1 addition & 0 deletions core/js/src/LocalTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
import kotlinx.serialization.Serializable
import kotlinx.datetime.internal.JSJoda.LocalTime as jtLocalTime
Expand Down
4 changes: 2 additions & 2 deletions core/js/src/mathJs.kt → core/js/src/internal/mathJs.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Copyright 2019-2020 JetBrains s.r.o.
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime
package kotlinx.datetime.internal

/**
* Safely adds two long values.
Expand Down
2 changes: 2 additions & 0 deletions core/jvm/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package kotlinx.datetime

import kotlinx.datetime.internal.safeMultiply
import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import java.time.DateTimeException
Expand Down
3 changes: 3 additions & 0 deletions core/jvm/src/LocalDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
@file:JvmName("LocalDateJvmKt")
package kotlinx.datetime

import kotlinx.datetime.internal.safeAdd
import kotlinx.datetime.internal.safeMultiply
import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.LocalDateIso8601Serializer
import kotlinx.serialization.Serializable
import java.time.DateTimeException
Expand Down
1 change: 1 addition & 0 deletions core/jvm/src/LocalTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
import kotlinx.serialization.Serializable
import java.time.DateTimeException
Expand Down
4 changes: 2 additions & 2 deletions core/jvm/src/mathJvm.kt → core/jvm/src/internal/mathJvm.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Copyright 2019-2020 JetBrains s.r.o.
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime
package kotlinx.datetime.internal

internal actual fun safeMultiply(a: Long, b: Long): Long = Math.multiplyExact(a, b)
internal actual fun safeMultiply(a: Int, b: Int): Int = Math.multiplyExact(a, b)
Expand Down
1 change: 1 addition & 0 deletions core/native/cinterop_actuals/TimeZoneNative.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.cinterop.*
import kotlinx.datetime.internal.*
import platform.posix.free

internal actual class RegionTimeZone(private val tzid: TZID, actual override val id: String): TimeZone() {
Expand Down
1 change: 1 addition & 0 deletions core/native/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import kotlin.math.*
Expand Down
3 changes: 3 additions & 0 deletions core/native/src/LocalDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.internal.safeAdd
import kotlinx.datetime.internal.safeMultiply
import kotlinx.datetime.serializers.LocalDateIso8601Serializer
import kotlinx.serialization.Serializable
import kotlin.math.*
Expand Down
1 change: 1 addition & 0 deletions core/native/src/LocalDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer
import kotlinx.serialization.Serializable

Expand Down
1 change: 1 addition & 0 deletions core/native/src/LocalTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
import kotlinx.serialization.Serializable

Expand Down
1 change: 1 addition & 0 deletions core/native/src/TimeZone.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.*
import kotlinx.serialization.Serializable

Expand Down
1 change: 1 addition & 0 deletions core/native/src/UtcOffset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.UtcOffsetSerializer
import kotlinx.serialization.Serializable
import kotlin.math.abs
Expand Down
51 changes: 51 additions & 0 deletions core/native/src/internal/dateCalculations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime.internal

import kotlin.math.*

/**
* All code below was taken from various places of https://github.com/ThreeTen/threetenbp with few changes
*/

/**
* The number of days in a 400 year cycle.
*/
internal const val DAYS_PER_CYCLE = 146097

/**
* The number of days from year zero to year 1970.
* There are five 400 year cycles from year zero to 2000.
* There are 7 leap years from 1970 to 2000.
*/
internal const val DAYS_0000_TO_1970 = DAYS_PER_CYCLE * 5 - (30 * 365 + 7)

// days in a 400 year cycle = 146097
// days in a 10,000 year cycle = 146097 * 25
// seconds per day = 86400
internal const val SECONDS_PER_10000_YEARS = 146097L * 25L * 86400L

internal const val SECONDS_0000_TO_1970 = (146097L * 5L - (30L * 365L + 7L)) * 86400L

// org.threeten.bp.ZoneOffset#buildId
internal fun zoneIdByOffset(totalSeconds: Int): String {
return if (totalSeconds == 0) {
"Z"
} else {
val absTotalSeconds: Int = abs(totalSeconds)
val buf = StringBuilder()
val absHours: Int = absTotalSeconds / SECONDS_PER_HOUR
val absMinutes: Int = absTotalSeconds / SECONDS_PER_MINUTE % MINUTES_PER_HOUR
buf.append(if (totalSeconds < 0) "-" else "+")
.append(if (absHours < 10) "0" else "").append(absHours)
.append(if (absMinutes < 10) ":0" else ":").append(absMinutes)
val absSeconds: Int = absTotalSeconds % SECONDS_PER_MINUTE
if (absSeconds != 0) {
buf.append(if (absSeconds < 10) ":0" else ":").append(absSeconds)
}
buf.toString()
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
/*
* Copyright 2019-2020 JetBrains s.r.o.
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

/* Based on the ThreeTenBp project.
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
*/
package kotlinx.datetime

import kotlin.math.abs

/**
* All code below was taken from various places of https://github.com/ThreeTen/threetenbp with few changes
*/

/**
* The number of days in a 400 year cycle.
*/
internal const val DAYS_PER_CYCLE = 146097

/**
* The number of days from year zero to year 1970.
* There are five 400 year cycles from year zero to 2000.
* There are 7 leap years from 1970 to 2000.
*/
internal const val DAYS_0000_TO_1970 = DAYS_PER_CYCLE * 5 - (30 * 365 + 7)

// days in a 400 year cycle = 146097
// days in a 10,000 year cycle = 146097 * 25
// seconds per day = 86400
internal const val SECONDS_PER_10000_YEARS = 146097L * 25L * 86400L

internal const val SECONDS_0000_TO_1970 = (146097L * 5L - (30L * 365L + 7L)) * 86400L
package kotlinx.datetime.internal

/**
* Safely adds two long values.
Expand Down Expand Up @@ -97,24 +73,4 @@ internal actual fun safeMultiply(a: Int, b: Int): Int {
throw ArithmeticException("Multiplication overflows an int: $a * $b")
}
return total.toInt()
}

// org.threeten.bp.ZoneOffset#buildId
internal fun zoneIdByOffset(totalSeconds: Int): String {
return if (totalSeconds == 0) {
"Z"
} else {
val absTotalSeconds: Int = abs(totalSeconds)
val buf = StringBuilder()
val absHours: Int = absTotalSeconds / SECONDS_PER_HOUR
val absMinutes: Int = absTotalSeconds / SECONDS_PER_MINUTE % MINUTES_PER_HOUR
buf.append(if (totalSeconds < 0) "-" else "+")
.append(if (absHours < 10) "0" else "").append(absHours)
.append(if (absMinutes < 10) ":0" else ":").append(absMinutes)
val absSeconds: Int = absTotalSeconds % SECONDS_PER_MINUTE
if (absSeconds != 0) {
buf.append(if (absSeconds < 10) ":0" else ":").append(absSeconds)
}
buf.toString()
}
}
Loading