Build a deterministic UTC offset label from a fixed numeric offset.

time-zone-labels Timezone labels should be explicit instead of depending on the host machine.

Time Zone Labels

offsetHours
TimeZoneLabels.kt
Replay: real traced execution (multi-file project)
fun twoDigits(value: Int): String {
    return value.toString().padStart(2, '0')
}

fun main() {
    val offsetHours = 2
    val sign = if (offsetHours < 0) {
        "-"
    } else {
        "+"
    }
    val absoluteHours = if (offsetHours < 0) {
        -offsetHours
    } else {
        offsetHours
    }
    val label = "UTC$sign${twoDigits(absoluteHours)}:00"

    println("offset=$offsetHours")
    println("label=$label")
}
fun twoDigits(value: Int): String {
    return value.toString().padStart(2, '0')
}

fun main() {
    val offsetHours = -5
    val sign = if (offsetHours < 0) {
        "-"
    } else {
        "+"
    }
    val absoluteHours = if (offsetHours < 0) {
        -offsetHours
    } else {
        offsetHours
    }
    val label = "UTC$sign${twoDigits(absoluteHours)}:00"

    println("offset=$offsetHours")
    println("label=$label")
}
fun twoDigits(value: Int): String {
    return value.toString().padStart(2, '0')
}

fun main() {
    val offsetHours = 0
    val sign = if (offsetHours < 0) {
        "-"
    } else {
        "+"
    }
    val absoluteHours = if (offsetHours < 0) {
        -offsetHours
    } else {
        offsetHours
    }
    val label = "UTC$sign${twoDigits(absoluteHours)}:00"

    println("offset=$offsetHours")
    println("label=$label")
}
  1. offsetHours ← 2, sign ← +, absoluteHours ← 2

    5fun main() {6    val offsetHours→ 2 = 2 //@offsetHours=-5, 07    val sign→ + = if (offsetHours2 < 0) {8        "-"9    } else {10        "+"11    }12    val absoluteHours→ 2 = if (offsetHours2 < 0) {13        -offsetHours214    } else {15        offsetHours216    }17    val label = "UTC$sign+${twoDigits(absoluteHours2)}:00"
  2. fun twoDigits(value: Int): String

    1fun twoDigits(value2: Int): String {2    return value2.toString().padStart(2, '0')3}
  3. label ← UTC+02:00

    16    }17    val label→ UTC+02:00 = "UTC$sign+${twoDigits(absoluteHours2)}:00"1819    println("offset=$offsetHours2")20    println("label=$labelUTC+02:00")21}
    outputoffset=2
    label=UTC+02:00
  1. offsetHours ← -5, sign ← -, absoluteHours ← 5

    5fun main() {6    val offsetHours→ -5 = -57    val sign→ - = if (offsetHours-5 < 0) {8        "-"9    } else {10        "+"11    }12    val absoluteHours→ 5 = if (offsetHours-5 < 0) {13        -offsetHours-514    } else {15        offsetHours-516    }17    val label = "UTC$sign-${twoDigits(absoluteHours5)}:00"
  2. fun twoDigits(value: Int): String

    1fun twoDigits(value5: Int): String {2    return value5.toString().padStart(2, '0')3}
  3. label ← UTC-05:00

    16    }17    val label→ UTC-05:00 = "UTC$sign-${twoDigits(absoluteHours5)}:00"1819    println("offset=$offsetHours-5")20    println("label=$labelUTC-05:00")21}
    outputoffset=-5
    label=UTC-05:00
  1. offsetHours ← 0, sign ← +, absoluteHours ← 0

    5fun main() {6    val offsetHours→ 0 = 07    val sign→ + = if (offsetHours0 < 0) {8        "-"9    } else {10        "+"11    }12    val absoluteHours→ 0 = if (offsetHours0 < 0) {13        -offsetHours014    } else {15        offsetHours016    }17    val label = "UTC$sign+${twoDigits(absoluteHours0)}:00"
  2. fun twoDigits(value: Int): String

    1fun twoDigits(value0: Int): String {2    return value0.toString().padStart(2, '0')3}
  3. label ← UTC+00:00

    16    }17    val label→ UTC+00:00 = "UTC$sign+${twoDigits(absoluteHours0)}:00"1819    println("offset=$offsetHours0")20    println("label=$labelUTC+00:00")21}
    outputoffset=0
    label=UTC+00:00