Skip to content

Introduce APIs for dependency injection of TimeZone-related information#610

Open
dkhalanskyjb wants to merge 14 commits into
masterfrom
dkhalanskyjb/deprecate-datetime-singletons
Open

Introduce APIs for dependency injection of TimeZone-related information#610
dkhalanskyjb wants to merge 14 commits into
masterfrom
dkhalanskyjb/deprecate-datetime-singletons

Conversation

@dkhalanskyjb
Copy link
Copy Markdown
Collaborator

@dkhalanskyjb dkhalanskyjb commented Feb 26, 2026

Fixes #17
Fixes #201
Fixes #435

#611 is a space for comments, questions, and suggestions. Since the PR is at the conceptual stage, some discussion threads may end up being quite long, so the PR format is not particularly well-suited for it.

The most interesting places in the code are:

It's important to note that this is not a full-fledged solution for providing custom timezone databases, though! Constructing a custom TimeZone is still going to be non-viable, even with the proposed API, as there's no way to construct a custom TimeZone. To properly implement that, we would (most likely) need to expose the structure of a TimeZone object (historical data, recurring rules) and the additional logic that is likely going to be needed when implementing realistic timezone databases (see

private val impl: TimeZoneDatabase = FixedOffsetTimeZoneDatabase(object: TimeZoneDatabase {
override fun get(id: String): TimeZone = getOrNull(id)
?: throw IllegalTimeZoneException(
"Zone ID '$id' was not recognized by the bundled timezone database (version $timeZoneDatabaseVersion)."
)
override fun getOrNull(id: String): TimeZone? {
val data = zoneDataByNameOrNull(id) ?: return null
return RuleBasedTimeZoneCalculations(readTzFile(data).toTimeZoneRules(), id, this).asTimeZone()
}
override fun availableZoneIds(): Set<String> = timeZones
})
). We wanted to start small for now and watch the community response to at least the dependency injection API as a whole. Then, we would like to tweak it if necessary, and only then collect the less common use cases and expose the rest of the pieces.

Copy link
Copy Markdown

@swankjesse swankjesse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic and I appreciate how much design effort went into this.

When I set up servers and CI environments and such I always set an environment variable TZ=UTC0 so the host machine’s zone doesn’t accidentally leak through to the application layer. For example, I don’t need to worry that payment scheduling will change due to DST.

Would you consider exposing a symbol similar to TimeZoneContext.System like TimeZoneContext.Universal that is locked to UTC?

@dkhalanskyjb
Copy link
Copy Markdown
Collaborator Author

TimeZoneContext.Universal looks very specific, so probably not in the form you propose. My guess is that the API would look something like TimeZoneContext.System.withFixedCurrentTimeZone(TimeZone.UTC), though we'd need to carefully study the use cases for custom TimeZoneContexts to arrive at a good orthogonal API.

@dkhalanskyjb dkhalanskyjb force-pushed the dkhalanskyjb/deprecate-datetime-singletons branch from 087e12a to 7a159cb Compare May 28, 2026 09:18
@dkhalanskyjb dkhalanskyjb force-pushed the dkhalanskyjb/deprecate-datetime-singletons branch from 7a159cb to 10fac52 Compare May 28, 2026 09:31
@dkhalanskyjb dkhalanskyjb requested a review from ilya-g May 29, 2026 10:39
@dkhalanskyjb dkhalanskyjb marked this pull request as ready for review May 29, 2026 10:39
public final class kotlinx/datetime/TimeZoneContext$Companion {
}

public final class kotlinx/datetime/TimeZoneContext$DefaultImpls {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a new API, can it use the new mechanism of default function compilation which relies on JVM default methods? This old trampoline-based version is only needed for existing APIs targeted by old code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider a new name for TimeZone.of Provide the means for a non-default timezone database TimeZone.SYSTEM encourages static dependency

3 participants