Skip to content

Commit 019c71b

Browse files
committed
Fix after rebasing
1 parent 9a4d5ca commit 019c71b

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

core/commonKotlin/src/Instant.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private class UnboundedLocalDateTime(
182182
val epochSeconds = run {
183183
// org.threeten.bp.LocalDate#toEpochDay
184184
val epochDays = run {
185-
val y = year
185+
val y = year.toLong()
186186
var total = 365 * y
187187
if (y >= 0) {
188188
total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400
@@ -211,10 +211,12 @@ private class UnboundedLocalDateTime(
211211
return Instant.fromEpochSeconds(epochSeconds, nanosecond)
212212
}
213213

214+
override fun toString(): String = "UnboundedLocalDateTime($year-$month-$day $hour:$minute:$second.$nanosecond)"
215+
214216
companion object {
215217
fun fromInstant(instant: Instant, offsetSeconds: Int): UnboundedLocalDateTime {
216218
val localSecond: Long = instant.epochSeconds + offsetSeconds
217-
val epochDays = localSecond.floorDiv(SECONDS_PER_DAY.toLong()).toInt()
219+
val epochDays = localSecond.floorDiv(SECONDS_PER_DAY.toLong())
218220
val secsOfDay = localSecond.mod(SECONDS_PER_DAY.toLong()).toInt()
219221
val year: Int
220222
val month: Int
@@ -225,27 +227,27 @@ private class UnboundedLocalDateTime(
225227
// find the march-based year
226228
zeroDay -= 60 // adjust to 0000-03-01 so leap day is at end of four year cycle
227229

228-
var adjust = 0
230+
var adjust = 0L
229231
if (zeroDay < 0) { // adjust negative years to positive for calculation
230232
val adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1
231233
adjust = adjustCycles * 400
232234
zeroDay += -adjustCycles * DAYS_PER_CYCLE
233235
}
234-
var yearEst = ((400 * zeroDay.toLong() + 591) / DAYS_PER_CYCLE).toInt()
236+
var yearEst = ((400 * zeroDay.toLong() + 591) / DAYS_PER_CYCLE)
235237
var doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400)
236238
if (doyEst < 0) { // fix estimate
237239
yearEst--
238240
doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400)
239241
}
240242
yearEst += adjust // reset any negative year
241243

242-
val marchDoy0 = doyEst
244+
val marchDoy0 = doyEst.toInt()
243245

244246
// convert march-based values back to january-based
245247
val marchMonth0 = (marchDoy0 * 5 + 2) / 153
246248
month = (marchMonth0 + 2) % 12 + 1
247249
day = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1
248-
year = yearEst + marchMonth0 / 10
250+
year = (yearEst + marchMonth0 / 10).toInt()
249251
}
250252
val hours = (secsOfDay / SECONDS_PER_HOUR)
251253
val secondWithoutHours = secsOfDay - hours * SECONDS_PER_HOUR

core/commonKotlin/test/InstantIsoStringsTest.kt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class InstantIsoStringsTest {
1414
@Test
1515
fun parseDates() {
1616
fun Int.zeroPadded(digits: Int): String = when {
17-
this >= 0 -> "${toString().padStart(digits, '0')}"
17+
this >= 0 -> toString().padStart(digits, '0')
1818
else -> "-${absoluteValue.toString().padStart(digits, '0')}"
1919
}
2020
fun localDateToString(year: Int, month: Int, day: Int) =
@@ -50,8 +50,8 @@ class InstantIsoStringsTest {
5050

5151
@Test
5252
fun parseIsoString() {
53-
for ((str, seconds, nanos) in arrayOf(
54-
// all components are taken into accout
53+
for ((str, seconds, nanos) in arrayOf<Triple<String, Long, Int>>(
54+
// all components are taken into account
5555
Triple("1970-01-01T00:00:00Z", 0, 0),
5656
Triple("1970-01-01T00:00:00.000000001Z", 0, 1),
5757
Triple("1970-01-01T00:00:00.100Z", 0, 100000000),
@@ -173,13 +173,34 @@ class InstantIsoStringsTest {
173173
Triple("+83082-01-04T20:18:56.409867424Z", 2559647852336, 409867424),
174174
Triple("-916839-09-12T22:45:39.091941363Z", -28994789466861, 91941363),
175175
Triple("-147771-05-07T08:31:34.950238979Z", -4725358615706, 950238979),
176+
// random enormous values queried from java.time
177+
Triple("+639727757-10-17T17:26:30.359003681Z", 20187795978609990, 359003681),
178+
Triple("-375448814-11-11T03:04:48.637504595Z", -11848082341899312, 637504595),
179+
Triple("+99162559-10-16T11:21:05.057717803Z", 3129205972303265, 57717803),
180+
Triple("-826813174-05-22T21:35:39.018693830Z", -26091765799784661, 18693830),
181+
Triple("+254125623-04-28T11:42:22.659957596Z", 8019367929949342, 659957596),
182+
Triple("+540978343-01-03T20:31:36.945404442Z", 17071565436102696, 945404442),
183+
Triple("+988277529-06-06T13:25:41.942771350Z", 31186964391657941, 942771350),
184+
Triple("+566487909-09-04T08:09:46.007490076Z", 17876569606963786, 7490076),
185+
Triple("+442225124-02-16T18:23:29.975096773Z", 13955214848034209, 975096773),
186+
Triple("-399250586-02-19T22:58:29.585918917Z", -12599193741267691, 585918917),
187+
Triple("-190217791-04-28T00:49:29.270751921Z", -6002755857213031, 270751921),
188+
Triple("-716173704-05-24T22:05:33.639928802Z", -22600321355469267, 639928802),
189+
Triple("+910504788-10-26T05:23:43.517192887Z", 28732693749312223, 517192887),
190+
Triple("+515896807-08-17T06:36:25.012343642Z", 16280068627982185, 12343642),
191+
Triple("-623794742-03-20T17:09:07.396143995Z", -19685122891483853, 396143995),
192+
Triple("-416781718-10-06T01:41:32.866307162Z", -13152422812544308, 866307162),
193+
Triple("+287346593-09-30T23:30:55.109337183Z", 9067720499134255, 109337183),
194+
Triple("-819839065-09-06T07:25:58.953784983Z", -25871684167672442, 953784983),
195+
Triple("+673467211-06-05T02:15:40.712392732Z", 21252510297310540, 712392732),
196+
Triple("+982441727-04-13T12:12:06.776817565Z", 31002804263391126, 776817565),
176197
)) {
177198
val instant = parseInstant(str)
178199
assertEquals(
179-
seconds.toLong() * 1000 + nanos / 1000000, instant.toEpochMilliseconds(),
200+
Instant.fromEpochSeconds(seconds, nanos), instant,
180201
"Parsed $instant from $str, with Unix time = `$seconds + 10^-9 * $nanos`"
181202
)
182-
assertEquals(str, formatIso(instant))
203+
assertEquals(str, displayInstant(instant))
183204
}
184205
// non-canonical strings are parsed as well, but formatted differently
185206
for ((str, seconds, nanos) in arrayOf(
@@ -338,7 +359,7 @@ class InstantIsoStringsTest {
338359
strings.forEach { (str, strInZ) ->
339360
val instant = parseInstant(str)
340361
assertEquals(parseInstant(strInZ), instant, str)
341-
assertEquals(strInZ, formatIso(instant), str)
362+
assertEquals(strInZ, displayInstant(instant), str)
342363
}
343364
assertInvalidFormat { parseInstant("2020-01-01T00:01:01+18:01") }
344365
assertInvalidFormat { parseInstant("2020-01-01T00:01:01+1801") }
@@ -378,10 +399,12 @@ class InstantIsoStringsTest {
378399
}
379400

380401
private fun parseInstant(isoString: String): Instant {
402+
// return Instant.parse(isoString)
381403
return parseIso(isoString)
382404
}
383405

384406
private fun displayInstant(instant: Instant): String {
407+
// return instant.toString()
385408
return formatIso(instant)
386409
}
387410
}
@@ -394,4 +417,4 @@ private fun <T> assertInvalidFormat(message: String? = null, f: () -> T) {
394417
val result = f()
395418
fail(result.toString())
396419
}
397-
}
420+
}

0 commit comments

Comments
 (0)