Skip to content

Commit b4e9839

Browse files
committed
Implement the RFC 1123 format
1 parent 6a5d9f6 commit b4e9839

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

core/common/src/format/ValueBagFormat.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ public class ValueBagFormat private constructor(private val actualFormat: Format
299299
appendFormatString("ld<'-'mm'-'dd>('T'|'t')lt<hh':'mm':'ss(|'.'f)>uo<('Z'|'z')|+(HH(|':'mm(|':'ss)))>")
300300
}
301301

302+
public val RFC_1123 : ValueBagFormat = build {
303+
appendDayOfWeek(listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))
304+
appendLiteral(", ")
305+
appendDayOfMonth()
306+
appendLiteral(' ')
307+
appendMonthName(listOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
308+
appendFormatString(" ld<yyyy> lt<hh':'mm':'ss> uo<'GMT'|+(HHmm)>")
309+
}
310+
302311
internal val Cache = LruCache<String, ValueBagFormat>(16) { fromFormatString(it) }
303312
}
304313

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019-2023 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.format
7+
8+
import kotlinx.datetime.*
9+
import kotlinx.datetime.format.*
10+
import kotlin.test.*
11+
12+
class ValueBagFormatTest {
13+
@Test
14+
fun testRfc1123() {
15+
val bags = buildMap<ValueBag, Pair<String, Set<String>>> {
16+
put(valueBag(LocalDate(2008, 6, 3), LocalTime(11, 5, 30), UtcOffset.ZERO), ("Tue, 3 Jun 2008 11:05:30 GMT" to setOf()))
17+
put(valueBag(LocalDate(2008, 6, 30), LocalTime(11, 5, 30), UtcOffset.ZERO), ("Mon, 30 Jun 2008 11:05:30 GMT" to setOf()))
18+
put(valueBag(LocalDate(2008, 6, 3), LocalTime(11, 5, 30), UtcOffset(hours = 2)), ("Tue, 3 Jun 2008 11:05:30 +0200" to setOf()))
19+
put(valueBag(LocalDate(2008, 6, 30), LocalTime(11, 5, 30), UtcOffset(hours = -3)), ("Mon, 30 Jun 2008 11:05:30 -0300" to setOf()))
20+
}
21+
test(ValueBagFormat.RFC_1123, bags)
22+
}
23+
24+
private fun valueBag(
25+
date: LocalDate? = null,
26+
time: LocalTime? = null,
27+
offset: UtcOffset? = null,
28+
zone: TimeZone? = null
29+
) = ValueBag().apply {
30+
date?.let { populateFrom(it) }
31+
time?.let { populateFrom(it) }
32+
offset?.let { populateFrom(it) }
33+
timeZoneId = zone?.id
34+
}
35+
36+
private fun assertValueBagsEqual(a: ValueBag, b: ValueBag, message: String? = null) {
37+
assertEquals(a.toLocaldate(), b.toLocaldate(), message)
38+
assertEquals(a.toLocalTime(), b.toLocalTime(), message)
39+
assertEquals(a.toUtcOffset(), b.toUtcOffset(), message)
40+
assertEquals(a.timeZoneId, b.timeZoneId, message)
41+
}
42+
43+
private fun test(format: ValueBagFormat, strings: Map<ValueBag, Pair<String, Set<String>>>) {
44+
for ((value, stringsForValue) in strings) {
45+
val (canonicalString, otherStrings) = stringsForValue
46+
assertEquals(canonicalString, format.format(value), "formatting $value with $format")
47+
assertValueBagsEqual(value, format.parse(canonicalString), "parsing '$canonicalString' with $format")
48+
for (otherString in otherStrings) {
49+
assertValueBagsEqual(value, format.parse(otherString), "parsing '$otherString' with $format")
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)