-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
283afa3
commit 7e8f55e
Showing
5 changed files
with
193 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
core/common/test/samples/format/DateTimeComponentsFormatSamples.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.test.samples.format | ||
|
||
import kotlinx.datetime.* | ||
import kotlinx.datetime.format.* | ||
import kotlin.test.* | ||
|
||
class DateTimeComponentsFormatSamples { | ||
@Test | ||
fun timeZoneId() { | ||
val format = DateTimeComponents.Format { | ||
dateTime(LocalDateTime.Formats.ISO) | ||
char('[') | ||
timeZoneId() | ||
char(']') | ||
} | ||
val formatted = format.format { | ||
setDateTime(LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000)) | ||
timeZoneId = "Europe/Paris" | ||
} | ||
check(formatted == "2021-01-13T09:34:58.12[Europe/Paris]") | ||
val parsed = format.parse("2021-01-13T09:34:58.12[Europe/Paris]") | ||
check(parsed.toLocalDateTime() == LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000)) | ||
check(parsed.timeZoneId == "Europe/Paris") | ||
} | ||
|
||
@Test | ||
fun dateTimeComponents() { | ||
val format = DateTimeComponents.Format { | ||
char('{') | ||
dateTimeComponents(DateTimeComponents.Formats.RFC_1123) | ||
char('}') | ||
} | ||
val formatted = format.format { | ||
setDateTimeOffset( | ||
LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000), | ||
UtcOffset(hours = 3, minutes = 30) | ||
) | ||
} | ||
check(formatted == "{Wed, 13 Jan 2021 09:34:58 +0330}") | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
core/common/test/samples/format/LocalDateTimeFormatSamples.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.test.samples.format | ||
|
||
import kotlinx.datetime.* | ||
import kotlinx.datetime.format.* | ||
import kotlin.test.* | ||
|
||
class LocalDateTimeFormatSamples { | ||
@Test | ||
fun dateTime() { | ||
val format = DateTimeComponents.Format { | ||
dateTime(LocalDateTime.Formats.ISO) | ||
offset(UtcOffset.Formats.FOUR_DIGITS) | ||
} | ||
val formatted = format.format { | ||
setDateTimeOffset( | ||
LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000), | ||
UtcOffset(hours = 2) | ||
) | ||
} | ||
check(formatted == "2021-01-13T09:34:58.12+0200") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.test.samples.format | ||
|
||
import kotlinx.datetime.* | ||
import kotlinx.datetime.format.* | ||
import kotlin.test.* | ||
|
||
class LocalTimeFormatSamples { | ||
@Test | ||
fun hhmmss() { | ||
// format the local time as a single number | ||
val format = LocalTime.Format { | ||
hour(); minute(); second() | ||
optional { char('.'); secondFraction(1, 9) } | ||
} | ||
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000)) | ||
check(formatted == "093458.12") | ||
} | ||
|
||
@Test | ||
fun amPm() { | ||
val format = LocalTime.Format { | ||
amPmHour(); char(':'); minute(); char(':'); second() | ||
char(' '); amPmMarker("AM", "PM") | ||
} | ||
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000)) | ||
check(formatted == "09:34:58 AM") | ||
} | ||
|
||
@Test | ||
fun fixedLengthSecondFraction() { | ||
val format = LocalTime.Format { | ||
hour(); char(':'); minute(); char(':'); second() | ||
char('.'); secondFraction(fixedLength = 3) | ||
} | ||
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000)) | ||
check(formatted == "09:34:58.120") | ||
} | ||
|
||
@Test | ||
fun time() { | ||
val format = LocalDateTime.Format { | ||
date(LocalDate.Formats.ISO) | ||
char(' ') | ||
time(LocalTime.Formats.ISO) | ||
} | ||
val formatted = format.format(LocalDateTime(2021, 1, 13, 9, 34, 58, 120_000_000)) | ||
check(formatted == "2021-01-13 09:34:58.12") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2019-2024 JetBrains s.r.o. and contributors. | ||
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package kotlinx.datetime.test.samples.format | ||
|
||
import kotlinx.datetime.* | ||
import kotlinx.datetime.format.* | ||
import kotlin.test.* | ||
|
||
class UtcOffsetFormatSamples { | ||
@Test | ||
fun isoOrGmt() { | ||
val format = UtcOffset.Format { | ||
// if the offset is zero, `GMT` is printed | ||
optional("GMT") { | ||
offsetHours(); char(':'); offsetMinutesOfHour() | ||
// if seconds are zero, they are omitted | ||
optional { char(':'); offsetSecondsOfMinute() } | ||
} | ||
} | ||
check(format.format(UtcOffset.ZERO) == "GMT") | ||
check(format.format(UtcOffset(hours = -2)) == "-02:00") | ||
check(format.format(UtcOffset(hours = -2, minutes = -30)) == "-02:30") | ||
check(format.format(UtcOffset(hours = -2, minutes = -30, seconds = -59)) == "-02:30:59") | ||
} | ||
|
||
@Test | ||
fun offset() { | ||
val format = DateTimeComponents.Format { | ||
dateTime(LocalDateTime.Formats.ISO) | ||
offset(UtcOffset.Formats.FOUR_DIGITS) | ||
} | ||
val formatted = format.format { | ||
setDateTimeOffset( | ||
LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000), | ||
UtcOffset(hours = 2) | ||
) | ||
} | ||
check(formatted == "2021-01-13T09:34:58.12+0200") | ||
} | ||
} |