Skip to content

Commit 82aff29

Browse files
committed
Split math.kt into math.kt and dateCalculations.kt
1 parent 8f0e25e commit 82aff29

27 files changed

+127
-89
lines changed

core/common/src/DateTimePeriod.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package kotlinx.datetime
77

8+
import kotlinx.datetime.internal.*
89
import kotlinx.datetime.serializers.DatePeriodIso8601Serializer
910
import kotlinx.datetime.serializers.DateTimePeriodIso8601Serializer
1011
import kotlin.math.*

core/common/src/DateTimeUnit.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package kotlinx.datetime
77

8+
import kotlinx.datetime.internal.safeMultiply
89
import kotlinx.datetime.serializers.*
910
import kotlinx.serialization.Serializable
1011
import kotlin.time.*

core/common/src/Instant.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package kotlinx.datetime
77

8+
import kotlinx.datetime.internal.*
89
import kotlinx.datetime.serializers.InstantIso8601Serializer
910
import kotlinx.serialization.Serializable
1011
import kotlin.time.*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.internal
7+
8+
internal const val SECONDS_PER_HOUR = 60 * 60
9+
10+
internal const val SECONDS_PER_MINUTE = 60
11+
12+
internal const val MINUTES_PER_HOUR = 60
13+
14+
internal const val HOURS_PER_DAY = 24
15+
16+
internal const val SECONDS_PER_DAY: Int = SECONDS_PER_HOUR * HOURS_PER_DAY
17+
18+
internal const val NANOS_PER_ONE = 1_000_000_000
19+
internal const val NANOS_PER_MILLI = 1_000_000
20+
internal const val MILLIS_PER_ONE = 1_000
21+
22+
internal const val NANOS_PER_DAY: Long = NANOS_PER_ONE * SECONDS_PER_DAY.toLong()
23+
24+
internal const val NANOS_PER_MINUTE: Long = NANOS_PER_ONE * SECONDS_PER_MINUTE.toLong()
25+
26+
internal const val NANOS_PER_HOUR = NANOS_PER_ONE * SECONDS_PER_HOUR.toLong()
27+
28+
internal const val MILLIS_PER_DAY: Int = SECONDS_PER_DAY * MILLIS_PER_ONE
29+
30+
// org.threeten.bp.chrono.IsoChronology#isLeapYear
31+
internal fun isLeapYear(year: Int): Boolean {
32+
val prolepticYear: Long = year.toLong()
33+
return prolepticYear and 3 == 0L && (prolepticYear % 100 != 0L || prolepticYear % 400 == 0L)
34+
}
35+
36+
internal fun Int.monthLength(isLeapYear: Boolean): Int =
37+
when (this) {
38+
2 -> if (isLeapYear) 29 else 28
39+
4, 6, 9, 11 -> 30
40+
else -> 31
41+
}

core/common/src/math.kt renamed to core/common/src/internal/math.kt

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

6-
package kotlinx.datetime
6+
package kotlinx.datetime.internal
77

88
internal fun Long.clampToInt(): Int =
99
when {
@@ -12,28 +12,6 @@ internal fun Long.clampToInt(): Int =
1212
else -> toInt()
1313
}
1414

15-
internal const val SECONDS_PER_HOUR = 60 * 60
16-
17-
internal const val SECONDS_PER_MINUTE = 60
18-
19-
internal const val MINUTES_PER_HOUR = 60
20-
21-
internal const val HOURS_PER_DAY = 24
22-
23-
internal const val SECONDS_PER_DAY: Int = SECONDS_PER_HOUR * HOURS_PER_DAY
24-
25-
internal const val NANOS_PER_ONE = 1_000_000_000
26-
internal const val NANOS_PER_MILLI = 1_000_000
27-
internal const val MILLIS_PER_ONE = 1_000
28-
29-
internal const val NANOS_PER_DAY: Long = NANOS_PER_ONE * SECONDS_PER_DAY.toLong()
30-
31-
internal const val NANOS_PER_MINUTE: Long = NANOS_PER_ONE * SECONDS_PER_MINUTE.toLong()
32-
33-
internal const val NANOS_PER_HOUR = NANOS_PER_ONE * SECONDS_PER_HOUR.toLong()
34-
35-
internal const val MILLIS_PER_DAY: Int = SECONDS_PER_DAY * MILLIS_PER_ONE
36-
3715
internal expect fun safeMultiply(a: Long, b: Long): Long
3816
internal expect fun safeMultiply(a: Int, b: Int): Int
3917
internal expect fun safeAdd(a: Long, b: Long): Long
@@ -201,16 +179,3 @@ internal fun multiplyAndAdd(d: Long, n: Long, r: Long): Long {
201179
}
202180
return safeAdd(safeMultiply(md, n), mr)
203181
}
204-
205-
// org.threeten.bp.chrono.IsoChronology#isLeapYear
206-
internal fun isLeapYear(year: Int): Boolean {
207-
val prolepticYear: Long = year.toLong()
208-
return prolepticYear and 3 == 0L && (prolepticYear % 100 != 0L || prolepticYear % 400 == 0L)
209-
}
210-
211-
internal fun Int.monthLength(isLeapYear: Boolean): Int =
212-
when (this) {
213-
2 -> if (isLeapYear) 29 else 28
214-
4, 6, 9, 11 -> 30
215-
else -> 31
216-
}

core/common/test/InstantTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package kotlinx.datetime.test
77

88
import kotlinx.datetime.*
99
import kotlinx.datetime.Clock // currently, requires an explicit import due to a conflict with the deprecated Clock from kotlin.time
10+
import kotlinx.datetime.internal.*
1011
import kotlin.random.*
1112
import kotlin.test.*
1213
import kotlin.time.*

core/common/test/LocalDateTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package kotlinx.datetime.test
77

88
import kotlinx.datetime.*
9+
import kotlinx.datetime.internal.*
910
import kotlin.random.*
1011
import kotlin.test.*
1112

core/common/test/LocalTimeTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package kotlinx.datetime.test
77

88
import kotlinx.datetime.*
9+
import kotlinx.datetime.internal.*
910
import kotlin.math.*
1011
import kotlin.random.*
1112
import kotlin.test.*

core/common/test/MultiplyAndDivideTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package kotlinx.datetime.test
77
import kotlin.random.*
88
import kotlin.test.*
9-
import kotlinx.datetime.*
9+
import kotlinx.datetime.internal.*
1010

1111
class MultiplyAndDivideTest {
1212

core/js/src/Instant.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
1111
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
1212
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
1313
import kotlinx.datetime.internal.JSJoda.ChronoUnit
14+
import kotlinx.datetime.internal.safeAdd
15+
import kotlinx.datetime.internal.*
1416
import kotlinx.datetime.serializers.InstantIso8601Serializer
1517
import kotlinx.serialization.Serializable
1618
import kotlin.time.*

core/js/src/LocalTime.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package kotlinx.datetime
66

7+
import kotlinx.datetime.internal.*
78
import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
89
import kotlinx.serialization.Serializable
910
import kotlinx.datetime.internal.JSJoda.LocalTime as jtLocalTime

core/js/src/mathJs.kt renamed to core/js/src/internal/mathJs.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
* Copyright 2019-2020 JetBrains s.r.o.
2+
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

6-
package kotlinx.datetime
6+
package kotlinx.datetime.internal
77

88
/**
99
* Safely adds two long values.

core/jvm/src/Instant.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package kotlinx.datetime
88

9+
import kotlinx.datetime.internal.safeMultiply
10+
import kotlinx.datetime.internal.*
911
import kotlinx.datetime.serializers.InstantIso8601Serializer
1012
import kotlinx.serialization.Serializable
1113
import java.time.DateTimeException

core/jvm/src/LocalDate.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
@file:JvmName("LocalDateJvmKt")
66
package kotlinx.datetime
77

8+
import kotlinx.datetime.internal.safeAdd
9+
import kotlinx.datetime.internal.safeMultiply
10+
import kotlinx.datetime.internal.*
811
import kotlinx.datetime.serializers.LocalDateIso8601Serializer
912
import kotlinx.serialization.Serializable
1013
import java.time.DateTimeException

core/jvm/src/LocalTime.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package kotlinx.datetime
88

9+
import kotlinx.datetime.internal.*
910
import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
1011
import kotlinx.serialization.Serializable
1112
import java.time.DateTimeException

core/jvm/src/mathJvm.kt renamed to core/jvm/src/internal/mathJvm.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
* Copyright 2019-2020 JetBrains s.r.o.
2+
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

6-
package kotlinx.datetime
6+
package kotlinx.datetime.internal
77

88
internal actual fun safeMultiply(a: Long, b: Long): Long = Math.multiplyExact(a, b)
99
internal actual fun safeMultiply(a: Int, b: Int): Int = Math.multiplyExact(a, b)

core/native/cinterop_actuals/TimeZoneNative.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package kotlinx.datetime
66

77
import kotlinx.datetime.internal.*
88
import kotlinx.cinterop.*
9+
import kotlinx.datetime.internal.*
910
import platform.posix.free
1011

1112
internal actual class RegionTimeZone(private val tzid: TZID, actual override val id: String): TimeZone() {

core/native/src/Instant.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package kotlinx.datetime
1010

11+
import kotlinx.datetime.internal.*
1112
import kotlinx.datetime.serializers.InstantIso8601Serializer
1213
import kotlinx.serialization.Serializable
1314
import kotlin.math.*

core/native/src/LocalDate.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
package kotlinx.datetime
1010

11+
import kotlinx.datetime.internal.*
12+
import kotlinx.datetime.internal.safeAdd
13+
import kotlinx.datetime.internal.safeMultiply
1114
import kotlinx.datetime.serializers.LocalDateIso8601Serializer
1215
import kotlinx.serialization.Serializable
1316
import kotlin.math.*

core/native/src/LocalDateTime.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package kotlinx.datetime
1010

11+
import kotlinx.datetime.internal.*
1112
import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer
1213
import kotlinx.serialization.Serializable
1314

core/native/src/LocalTime.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package kotlinx.datetime
1010

11+
import kotlinx.datetime.internal.*
1112
import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
1213
import kotlinx.serialization.Serializable
1314

core/native/src/TimeZone.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package kotlinx.datetime
1010

11+
import kotlinx.datetime.internal.*
1112
import kotlinx.datetime.serializers.*
1213
import kotlinx.serialization.Serializable
1314

core/native/src/UtcOffset.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package kotlinx.datetime
77

8+
import kotlinx.datetime.internal.*
89
import kotlinx.datetime.serializers.UtcOffsetSerializer
910
import kotlinx.serialization.Serializable
1011
import kotlin.math.abs
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.internal
7+
8+
import kotlin.math.*
9+
10+
/**
11+
* All code below was taken from various places of https://github.com/ThreeTen/threetenbp with few changes
12+
*/
13+
14+
/**
15+
* The number of days in a 400 year cycle.
16+
*/
17+
internal const val DAYS_PER_CYCLE = 146097
18+
19+
/**
20+
* The number of days from year zero to year 1970.
21+
* There are five 400 year cycles from year zero to 2000.
22+
* There are 7 leap years from 1970 to 2000.
23+
*/
24+
internal const val DAYS_0000_TO_1970 = DAYS_PER_CYCLE * 5 - (30 * 365 + 7)
25+
26+
// days in a 400 year cycle = 146097
27+
// days in a 10,000 year cycle = 146097 * 25
28+
// seconds per day = 86400
29+
internal const val SECONDS_PER_10000_YEARS = 146097L * 25L * 86400L
30+
31+
internal const val SECONDS_0000_TO_1970 = (146097L * 5L - (30L * 365L + 7L)) * 86400L
32+
33+
// org.threeten.bp.ZoneOffset#buildId
34+
internal fun zoneIdByOffset(totalSeconds: Int): String {
35+
return if (totalSeconds == 0) {
36+
"Z"
37+
} else {
38+
val absTotalSeconds: Int = abs(totalSeconds)
39+
val buf = StringBuilder()
40+
val absHours: Int = absTotalSeconds / SECONDS_PER_HOUR
41+
val absMinutes: Int = absTotalSeconds / SECONDS_PER_MINUTE % MINUTES_PER_HOUR
42+
buf.append(if (totalSeconds < 0) "-" else "+")
43+
.append(if (absHours < 10) "0" else "").append(absHours)
44+
.append(if (absMinutes < 10) ":0" else ":").append(absMinutes)
45+
val absSeconds: Int = absTotalSeconds % SECONDS_PER_MINUTE
46+
if (absSeconds != 0) {
47+
buf.append(if (absSeconds < 10) ":0" else ":").append(absSeconds)
48+
}
49+
buf.toString()
50+
}
51+
}

core/native/src/mathNative.kt renamed to core/native/src/internal/mathNative.kt

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
/*
2-
* Copyright 2019-2020 JetBrains s.r.o.
2+
* Copyright 2019-2022 JetBrains s.r.o. and contributors.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
5+
56
/* Based on the ThreeTenBp project.
67
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
78
*/
8-
package kotlinx.datetime
9-
10-
import kotlin.math.abs
11-
12-
/**
13-
* All code below was taken from various places of https://github.com/ThreeTen/threetenbp with few changes
14-
*/
15-
16-
/**
17-
* The number of days in a 400 year cycle.
18-
*/
19-
internal const val DAYS_PER_CYCLE = 146097
20-
21-
/**
22-
* The number of days from year zero to year 1970.
23-
* There are five 400 year cycles from year zero to 2000.
24-
* There are 7 leap years from 1970 to 2000.
25-
*/
26-
internal const val DAYS_0000_TO_1970 = DAYS_PER_CYCLE * 5 - (30 * 365 + 7)
27-
28-
// days in a 400 year cycle = 146097
29-
// days in a 10,000 year cycle = 146097 * 25
30-
// seconds per day = 86400
31-
internal const val SECONDS_PER_10000_YEARS = 146097L * 25L * 86400L
32-
33-
internal const val SECONDS_0000_TO_1970 = (146097L * 5L - (30L * 365L + 7L)) * 86400L
9+
package kotlinx.datetime.internal
3410

3511
/**
3612
* Safely adds two long values.
@@ -97,24 +73,4 @@ internal actual fun safeMultiply(a: Int, b: Int): Int {
9773
throw ArithmeticException("Multiplication overflows an int: $a * $b")
9874
}
9975
return total.toInt()
100-
}
101-
102-
// org.threeten.bp.ZoneOffset#buildId
103-
internal fun zoneIdByOffset(totalSeconds: Int): String {
104-
return if (totalSeconds == 0) {
105-
"Z"
106-
} else {
107-
val absTotalSeconds: Int = abs(totalSeconds)
108-
val buf = StringBuilder()
109-
val absHours: Int = absTotalSeconds / SECONDS_PER_HOUR
110-
val absMinutes: Int = absTotalSeconds / SECONDS_PER_MINUTE % MINUTES_PER_HOUR
111-
buf.append(if (totalSeconds < 0) "-" else "+")
112-
.append(if (absHours < 10) "0" else "").append(absHours)
113-
.append(if (absMinutes < 10) ":0" else ":").append(absMinutes)
114-
val absSeconds: Int = absTotalSeconds % SECONDS_PER_MINUTE
115-
if (absSeconds != 0) {
116-
buf.append(if (absSeconds < 10) ":0" else ":").append(absSeconds)
117-
}
118-
buf.toString()
119-
}
12076
}

0 commit comments

Comments
 (0)