|
| 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 kotlinx.datetime.format.* |
| 10 | +import kotlin.test.* |
| 11 | +import kotlin.time.* |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests the code snippets in the README.md file. |
| 15 | + */ |
| 16 | +@Suppress("UNUSED_VARIABLE") |
| 17 | +class ReadmeTest { |
| 18 | + @Test |
| 19 | + fun testGettingCurrentMoment() { |
| 20 | + val currentMoment = Clock.System.now() |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + fun testConvertingAnInstantToLocalDateAndTimeComponents() { |
| 25 | + val currentMoment: Instant = Clock.System.now() |
| 26 | + val datetimeInUtc: LocalDateTime = currentMoment.toLocalDateTime(TimeZone.UTC) |
| 27 | + val datetimeInSystemZone: LocalDateTime = currentMoment.toLocalDateTime(TimeZone.currentSystemDefault()) |
| 28 | + |
| 29 | + val tzBerlin = TimeZone.of("Europe/Berlin") |
| 30 | + val datetimeInBerlin = currentMoment.toLocalDateTime(tzBerlin) |
| 31 | + |
| 32 | + val kotlinReleaseDateTime = LocalDateTime(2016, 2, 15, 16, 57, 0, 0) |
| 33 | + |
| 34 | + val kotlinReleaseInstant = kotlinReleaseDateTime.toInstant(TimeZone.of("UTC+3")) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun testGettingLocalDateComponents() { |
| 39 | + val now: Instant = Clock.System.now() |
| 40 | + val today: LocalDate = now.toLocalDateTime(TimeZone.currentSystemDefault()).date |
| 41 | + // or shorter |
| 42 | + val today2: LocalDate = Clock.System.todayIn(TimeZone.currentSystemDefault()) |
| 43 | + |
| 44 | + val knownDate = LocalDate(2020, 2, 21) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun testGettingLocalTimeComponents() { |
| 49 | + val now: Instant = Clock.System.now() |
| 50 | + val thisTime: LocalTime = now.toLocalDateTime(TimeZone.currentSystemDefault()).time |
| 51 | + |
| 52 | + val knownTime = LocalTime(hour = 23, minute = 59, second = 12) |
| 53 | + val timeWithNanos = LocalTime(hour = 23, minute = 59, second = 12, nanosecond = 999) |
| 54 | + val hourMinute = LocalTime(hour = 12, minute = 13) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun testConvertingInstantToAndFromUnixTime() { |
| 59 | + Instant.fromEpochMilliseconds(Clock.System.now().toEpochMilliseconds()) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun testConvertingInstantAndLocalDateTimeToAndFromIso8601String() { |
| 64 | + val instantNow = Clock.System.now() |
| 65 | + instantNow.toString() // returns something like 2015-12-31T12:30:00Z |
| 66 | + val instantBefore = Instant.parse("2010-06-01T22:19:44.475Z") |
| 67 | + |
| 68 | + LocalDateTime.parse("2010-06-01T22:19:44") |
| 69 | + LocalDate.parse("2010-06-01") |
| 70 | + LocalTime.parse("12:01:03") |
| 71 | + LocalTime.parse("12:00:03.999") |
| 72 | + assertFailsWith<IllegalArgumentException> { LocalTime.parse("12:0:03.999") } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun testWorkingWithOtherStringFormats() { |
| 77 | + val dateFormat = LocalDate.Format { |
| 78 | + monthNumber(padding = Padding.SPACE) |
| 79 | + char('/') |
| 80 | + dayOfMonth() |
| 81 | + char(' ') |
| 82 | + year() |
| 83 | + } |
| 84 | + |
| 85 | + val date = dateFormat.parse("12/24 2023") |
| 86 | + assertEquals("20231224", date.format(LocalDate.Formats.ISO_BASIC)) |
| 87 | + } |
| 88 | + |
| 89 | + @OptIn(FormatStringsInDatetimeFormats::class) |
| 90 | + @Test |
| 91 | + fun testUnicodePatterns() { |
| 92 | + assertEquals(""" |
| 93 | + date(LocalDate.Formats.ISO) |
| 94 | + char('T') |
| 95 | + hour() |
| 96 | + char(':') |
| 97 | + minute() |
| 98 | + char(':') |
| 99 | + second() |
| 100 | + alternativeParsing({ |
| 101 | + }) { |
| 102 | + char('.') |
| 103 | + secondFraction(3) |
| 104 | + } |
| 105 | + offset(UtcOffset.Formats.FOUR_DIGITS) |
| 106 | + """.trimIndent(), |
| 107 | + DateTimeFormat.formatAsKotlinBuilderDsl(DateTimeComponents.Format { |
| 108 | + byUnicodePattern("uuuu-MM-dd'T'HH:mm:ss[.SSS]Z") |
| 109 | + }) |
| 110 | + ) |
| 111 | + @OptIn(FormatStringsInDatetimeFormats::class) |
| 112 | + val dateTimeFormat = LocalDateTime.Format { |
| 113 | + byUnicodePattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]") |
| 114 | + } |
| 115 | + dateTimeFormat.parse("2023-12-24T23:59:59") |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun testParsingAndFormattingPartialCompoundOrOutOfBoundsData() { |
| 120 | + val yearMonth = DateTimeComponents.Format { year(); char('-'); monthNumber() } |
| 121 | + .parse("2024-01") |
| 122 | + assertEquals(2024, yearMonth.year) |
| 123 | + assertEquals(1, yearMonth.monthNumber) |
| 124 | + |
| 125 | + val dateTimeOffset = DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET |
| 126 | + .parse("2023-01-07T23:16:15.53+02:00") |
| 127 | + assertEquals("+02:00", dateTimeOffset.toUtcOffset().toString()) |
| 128 | + assertEquals("2023-01-07T23:16:15.530", dateTimeOffset.toLocalDateTime().toString()) |
| 129 | + |
| 130 | + val time = DateTimeComponents.Format { time(LocalTime.Formats.ISO) } |
| 131 | + .parse("23:59:60").apply { |
| 132 | + if (second == 60) second = 59 |
| 133 | + }.toLocalTime() |
| 134 | + assertEquals(LocalTime(23, 59, 59), time) |
| 135 | + |
| 136 | + assertEquals("Sat, 7 Jan 2023 23:59:60 +0200", DateTimeComponents.Formats.RFC_1123.format { |
| 137 | + // the receiver of this lambda is DateTimeComponents |
| 138 | + setDate(LocalDate(2023, 1, 7)) |
| 139 | + hour = 23 |
| 140 | + minute = 59 |
| 141 | + second = 60 |
| 142 | + setOffset(UtcOffset(hours = 2)) |
| 143 | + }) |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + fun testInstantArithmetic() { |
| 148 | + run { |
| 149 | + val now = Clock.System.now() |
| 150 | + val instantInThePast: Instant = Instant.parse("2020-01-01T00:00:00Z") |
| 151 | + val durationSinceThen: Duration = now - instantInThePast |
| 152 | + val equidistantInstantInTheFuture: Instant = now + durationSinceThen |
| 153 | + |
| 154 | + val period: DateTimePeriod = instantInThePast.periodUntil(Clock.System.now(), TimeZone.UTC) |
| 155 | + |
| 156 | + instantInThePast.yearsUntil(Clock.System.now(), TimeZone.UTC) |
| 157 | + instantInThePast.monthsUntil(Clock.System.now(), TimeZone.UTC) |
| 158 | + instantInThePast.daysUntil(Clock.System.now(), TimeZone.UTC) |
| 159 | + |
| 160 | + val diffInMonths = instantInThePast.until(Clock.System.now(), DateTimeUnit.MONTH, TimeZone.UTC) |
| 161 | + } |
| 162 | + |
| 163 | + run { |
| 164 | + val now = Clock.System.now() |
| 165 | + val systemTZ = TimeZone.currentSystemDefault() |
| 166 | + val tomorrow = now.plus(2, DateTimeUnit.DAY, systemTZ) |
| 167 | + val threeYearsAndAMonthLater = now.plus(DateTimePeriod(years = 3, months = 1), systemTZ) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun testDateArithmetic() { |
| 173 | + val date = LocalDate(2023, 1, 7) |
| 174 | + val date2 = date.plus(1, DateTimeUnit.DAY) |
| 175 | + date.plus(DatePeriod(days = 1)) |
| 176 | + date.until(date2, DateTimeUnit.DAY) |
| 177 | + date.yearsUntil(date2) |
| 178 | + date.monthsUntil(date2) |
| 179 | + date.daysUntil(date2) |
| 180 | + date.periodUntil(date2) |
| 181 | + date2 - date |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + fun testDateTimeArithmetic() { |
| 186 | + val timeZone = TimeZone.of("Europe/Berlin") |
| 187 | + val localDateTime = LocalDateTime.parse("2021-03-27T02:16:20") |
| 188 | + val instant = localDateTime.toInstant(timeZone) |
| 189 | + |
| 190 | + val instantOneDayLater = instant.plus(1, DateTimeUnit.DAY, timeZone) |
| 191 | + val localDateTimeOneDayLater = instantOneDayLater.toLocalDateTime(timeZone) |
| 192 | + assertEquals(LocalDateTime(2021, 3, 28, 3, 16, 20), localDateTimeOneDayLater) |
| 193 | + // 2021-03-28T03:16:20, as 02:16:20 that day is in a time gap |
| 194 | + |
| 195 | + val instantTwoDaysLater = instant.plus(2, DateTimeUnit.DAY, timeZone) |
| 196 | + val localDateTimeTwoDaysLater = instantTwoDaysLater.toLocalDateTime(timeZone) |
| 197 | + assertEquals(LocalDateTime(2021, 3, 29, 2, 16, 20), localDateTimeTwoDaysLater) |
| 198 | + // 2021-03-29T02:16:20 |
| 199 | + } |
| 200 | +} |
0 commit comments