offsetHours

abstract fun offsetHours(padding: Padding = Padding.ZERO)(source)

The total number of hours in the UTC offset, including the sign.

By default, it's zero-padded to two digits, but this can be changed with padding.

This field has the default value of 0. If you want to omit it, use optional.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Defining a custom format for the UTC offset
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") 
   //sampleEnd
}