|
| 1 | +/* |
| 2 | + * Copyright 2019-2024 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.test |
| 7 | + |
| 8 | +import kotlinx.datetime.* |
| 9 | +import kotlin.test.* |
| 10 | + |
| 11 | +class TimezonesWithoutDatabaseTest { |
| 12 | + @Test |
| 13 | + fun system() { |
| 14 | + val tz = TimeZone.currentSystemDefault() |
| 15 | + assertEquals("SYSTEM", tz.id) |
| 16 | + assertEquals("SYSTEM", tz.toString()) |
| 17 | + val now = Clock.System.now() |
| 18 | + assertEquals(now, now.toLocalDateTime(tz).toInstant(tz)) |
| 19 | + val offset = now.offsetIn(tz) |
| 20 | + println("$now = ${now.toLocalDateTime(tz)}$offset") |
| 21 | + assertEquals(now, now.toLocalDateTime(tz).toInstant(offset)) |
| 22 | + assertEquals(Instant.DISTANT_PAST, Instant.DISTANT_PAST.toLocalDateTime(tz).toInstant(tz)) |
| 23 | + assertEquals(Instant.DISTANT_FUTURE, Instant.DISTANT_FUTURE.toLocalDateTime(tz).toInstant(tz)) |
| 24 | + val today = now.toLocalDateTime(tz).date |
| 25 | + assertEquals(today.atTime(0, 0).toInstant(tz), today.atStartOfDayIn(tz)) |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + fun utc() { |
| 30 | + val utc: FixedOffsetTimeZone = TimeZone.UTC |
| 31 | + println(utc) |
| 32 | + assertEquals("Z", utc.id) |
| 33 | + assertEquals(UtcOffset.ZERO, utc.offset) |
| 34 | + assertEquals(0, utc.offset.totalSeconds) |
| 35 | + assertEquals(utc.offset, utc.offsetAt(Clock.System.now())) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun available() { |
| 40 | + assertEquals(setOf("UTC"), TimeZone.availableZoneIds) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun of() { |
| 45 | + assertFailsWith<IllegalTimeZoneException> { TimeZone.of("Europe/Moscow") } |
| 46 | + assertSame(TimeZone.currentSystemDefault(), TimeZone.of("SYSTEM")) |
| 47 | + } |
| 48 | + |
| 49 | + // from 310bp |
| 50 | + @Test |
| 51 | + fun timeZoneEquals() { |
| 52 | + val test1 = TimeZone.of("SYSTEM") |
| 53 | + val test2 = TimeZone.of("UTC") |
| 54 | + val test2b = TimeZone.of("UTC+00:00") |
| 55 | + assertEquals(false, test1 == test2) |
| 56 | + assertEquals(false, test2 == test1) |
| 57 | + |
| 58 | + assertEquals(true, test1 == test1) |
| 59 | + assertEquals(true, test2 == test2) |
| 60 | + assertEquals(true, test2 == test2b) |
| 61 | + |
| 62 | + assertEquals(test1.hashCode(), test1.hashCode()) |
| 63 | + assertEquals(test2.hashCode(), test2.hashCode()) |
| 64 | + assertEquals(test2.hashCode(), test2b.hashCode()) |
| 65 | + } |
| 66 | + |
| 67 | + // from 310bp |
| 68 | + @Test |
| 69 | + fun timeZoneToString() { |
| 70 | + val idToString = arrayOf( |
| 71 | + Pair("Z", "Z"), |
| 72 | + Pair("UTC", "UTC"), |
| 73 | + Pair("UTC+01:00", "UTC+01:00"), |
| 74 | + Pair("GMT+01:00", "GMT+01:00"), |
| 75 | + Pair("UT+01:00", "UT+01:00")) |
| 76 | + for ((id, str) in idToString) { |
| 77 | + assertEquals(str, TimeZone.of(id).toString()) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun utcOffsetNormalization() { |
| 83 | + val sameOffsetTZs = listOf("+04", "+04:00", "UTC+4", "UT+04", "GMT+04:00:00").map { TimeZone.of(it) } |
| 84 | + for (tz in sameOffsetTZs) { |
| 85 | + assertIs<FixedOffsetTimeZone>(tz) |
| 86 | + } |
| 87 | + val offsets = sameOffsetTZs.map { (it as FixedOffsetTimeZone).offset } |
| 88 | + val zoneIds = sameOffsetTZs.map { it.id } |
| 89 | + |
| 90 | + assertTrue(offsets.distinct().size == 1, "Expected all offsets to be equal: $offsets") |
| 91 | + assertTrue(offsets.map { it.toString() }.distinct().size == 1, "Expected all offsets to have the same string representation: $offsets") |
| 92 | + |
| 93 | + assertTrue(zoneIds.distinct().size > 1, "Expected some fixed offset zones to have different ids: $zoneIds") |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments