currentSystemDefault

Queries the current system time zone.

If the current system time zone changes, this function can reflect this change on the next invocation.

On Linux, this function queries the /etc/localtime symbolic link. If the link is missing, UTC is used. If the link points to an invalid location, IllegalTimeZoneException is thrown.

Samples

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

fun main() { 
   //sampleStart 
   // Obtaining the current system default time zone and using it for formatting
// a fixed-width format for log entries
val logTimeFormat = DateTimeComponents.Format {
    date(LocalDate.Formats.ISO)
    char(' ')
    hour(); char(':'); minute(); char(':'); second(); char('.'); secondFraction(3)
    offset(UtcOffset.Formats.FOUR_DIGITS)
}
fun logEntry(message: String, now: Instant = Clock.System.now()): String {
    val formattedTime = logTimeFormat.format {
        with(TimeZone.currentSystemDefault()) {
            setDateTime(now.toLocalDateTime())
            setOffset(offsetAt(now))
        }
    }
    return "[$formattedTime] $message"
}
// Outputs a text like `[2024-06-02 08:30:02.515+0200] Starting the application`
logEntry("Starting the application") 
   //sampleEnd
}