Skip to content

Commit 66e663d

Browse files
authored
Update the Windows/IANA timezone name mappings (#216)
Also, fail the Windows/IANA mapping update task if updates were found.
1 parent 322cbd0 commit 66e663d

File tree

2 files changed

+1003
-990
lines changed

2 files changed

+1003
-990
lines changed

core/build.gradle.kts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
33
import java.net.URL
44
import java.util.Locale
55
import javax.xml.parsers.DocumentBuilderFactory
6+
import java.io.ByteArrayOutputStream
7+
import java.io.PrintWriter
68

79
plugins {
810
kotlin("multiplatform")
@@ -275,7 +277,8 @@ tasks {
275277

276278
val downloadWindowsZonesMapping by tasks.registering {
277279
description = "Updates the mapping between Windows-specific and usual names for timezones"
278-
val output = "$projectDir/nativeMain/cinterop/public/windows_zones.hpp"
280+
val output = "$projectDir/native/cinterop/public/windows_zones.hpp"
281+
val initialFileContents = File(output).readBytes()
279282
outputs.file(output)
280283
doLast {
281284
val documentBuilderFactory = DocumentBuilderFactory.newInstance()
@@ -302,17 +305,19 @@ val downloadWindowsZonesMapping by tasks.registering {
302305
}
303306
}
304307
}
305-
File(output).printWriter().use { out ->
308+
val sortedMapping = mapping.toSortedMap()
309+
val bos = ByteArrayOutputStream()
310+
PrintWriter(bos).use { out ->
306311
out.println("""// generated with gradle task `$name`""")
307312
out.println("""#include <unordered_map>""")
308313
out.println("""#include <string>""")
309314
out.println("""static const std::unordered_map<std::string, std::string> standard_to_windows = {""")
310-
for ((usualName, windowsName) in mapping) {
315+
for ((usualName, windowsName) in sortedMapping) {
311316
out.println("\t{ \"$usualName\", \"$windowsName\" },")
312317
}
313318
out.println("};")
314319
out.println("""static const std::unordered_map<std::string, std::string> windows_to_standard = {""")
315-
val reverseMap = mutableMapOf<String, String>()
320+
val reverseMap = sortedMapOf<String, String>()
316321
for ((usualName, windowsName) in mapping) {
317322
if (reverseMap[windowsName] == null) {
318323
reverseMap[windowsName] = usualName
@@ -324,11 +329,17 @@ val downloadWindowsZonesMapping by tasks.registering {
324329
out.println("};")
325330
out.println("""static const std::unordered_map<std::string, size_t> zone_ids = {""")
326331
var i = 0
327-
for ((usualName, windowsName) in mapping) {
332+
for ((usualName, windowsName) in sortedMapping) {
328333
out.println("\t{ \"$usualName\", $i },")
329334
++i
330335
}
331336
out.println("};")
332337
}
338+
val newFileContents = bos.toByteArray()
339+
if (!(initialFileContents contentEquals newFileContents)) {
340+
File(output).writeBytes(newFileContents)
341+
throw GradleException("The mappings between Windows and IANA timezone names changed. " +
342+
"The new mappings were written to the filesystem.")
343+
}
333344
}
334345
}

0 commit comments

Comments
 (0)