|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 JetBrains s.r.o. |
| 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 | +package kotlinx.datetime |
| 6 | +import kotlinx.cinterop.* |
| 7 | +import platform.Foundation.* |
| 8 | +import kotlin.math.* |
| 9 | +import kotlin.random.* |
| 10 | +import kotlin.test.* |
| 11 | + |
| 12 | +class ConvertersTest { |
| 13 | + |
| 14 | + private val dateFormatter = NSDateFormatter() |
| 15 | + private val locale = NSLocale.localeWithLocaleIdentifier("en_US_POSIX") |
| 16 | + private val gregorian = NSCalendar.calendarWithIdentifier(NSCalendarIdentifierGregorian)!! |
| 17 | + private val utc = NSTimeZone.timeZoneForSecondsFromGMT(0) |
| 18 | + |
| 19 | + init { |
| 20 | + dateFormatter.locale = locale |
| 21 | + dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" |
| 22 | + dateFormatter.calendar = gregorian |
| 23 | + dateFormatter.timeZone = utc |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun testToFromNSDate() { |
| 28 | + // The first day on the Gregorian calendar. The day before it is 1582-10-04 in the Julian calendar. |
| 29 | + val gregorianCalendarStart = Instant.parse("1582-10-15T00:00:00Z").toEpochMilliseconds() |
| 30 | + val minBoundMillis = (NSDate.distantPast.timeIntervalSince1970 * 1000 + 0.5).toLong() |
| 31 | + val maxBoundMillis = (NSDate.distantFuture.timeIntervalSince1970 * 1000 - 0.5).toLong() |
| 32 | + repeat (1000) { |
| 33 | + val millis = Random.nextLong(minBoundMillis, maxBoundMillis) |
| 34 | + val instant = Instant.fromEpochMilliseconds(millis) |
| 35 | + val date = instant.toNSDate() |
| 36 | + // Darwin's date printer dynamically adjusts to switching between calendars, while our Instant does not. |
| 37 | + if (millis >= gregorianCalendarStart) { |
| 38 | + assertEquals(instant, Instant.parse(dateFormatter.stringFromDate(date))) |
| 39 | + } |
| 40 | + assertEquals(instant, date.toKotlinInstant()) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun availableZoneIdsToNSTimeZone() { |
| 46 | + for (id in TimeZone.availableZoneIds) { |
| 47 | + val normalizedId = (NSTimeZone.abbreviationDictionary[id] ?: id) as String |
| 48 | + val timeZone = TimeZone.of(normalizedId) |
| 49 | + if (timeZone is ZoneOffset) { |
| 50 | + continue |
| 51 | + } |
| 52 | + val nsTimeZone = timeZone.toNSTimeZone() |
| 53 | + assertEquals(normalizedId, nsTimeZone.name) |
| 54 | + assertEquals(timeZone, nsTimeZone.toKotlinTimeZone()) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // from threetenbp's tests for time zones |
| 59 | + @Test |
| 60 | + fun zoneOffsetToNSTimeZone() { |
| 61 | + for (i in -18 * 60..18 * 60) { |
| 62 | + val hours = i / 60 |
| 63 | + val minutes = i % 60 |
| 64 | + val str = (if (i < 0) "-" else "+") + |
| 65 | + (abs(hours) + 100).toString().substring(1) + ":" + |
| 66 | + (abs(minutes) + 100).toString().substring(1) + ":" + |
| 67 | + "00" |
| 68 | + val test = TimeZone.of(str) |
| 69 | + zoneOffsetCheck(test, hours, minutes) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun localDateToNSDateComponentsTest() { |
| 75 | + val date = LocalDate.parse("2019-02-04") |
| 76 | + val components = date.toNSDateComponents() |
| 77 | + components.timeZone = utc |
| 78 | + val nsDate = gregorian.dateFromComponents(components)!! |
| 79 | + val formatter = NSDateFormatter() |
| 80 | + formatter.dateFormat = "yyyy-MM-dd" |
| 81 | + assertEquals("2019-02-04", formatter.stringFromDate(nsDate)) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun localDateTimeToNSDateComponentsTest() { |
| 86 | + val str = "2019-02-04T23:59:30.543" |
| 87 | + val dateTime = LocalDateTime.parse(str) |
| 88 | + val components = dateTime.toNSDateComponents() |
| 89 | + components.timeZone = utc |
| 90 | + val nsDate = gregorian.dateFromComponents(components)!! |
| 91 | + assertEquals(str + "Z", dateFormatter.stringFromDate(nsDate)) |
| 92 | + } |
| 93 | + |
| 94 | + private fun zoneOffsetCheck(timeZone: TimeZone, hours: Int, minutes: Int) { |
| 95 | + val nsTimeZone = timeZone.toNSTimeZone() |
| 96 | + assertEquals(hours * 3600 + minutes * 60, nsTimeZone.secondsFromGMT.convert()) |
| 97 | + assertEquals(timeZone, nsTimeZone.toKotlinTimeZone()) |
| 98 | + } |
| 99 | +} |
0 commit comments