Timezone service
The TimezoneService provides functionality for managing and retrieving time zone information.
It allows you to transform a UTC Time to a specific time zone, but also provides other details regarding the offset and the timezone.
The TimezoneService object provides methods to get timezone information online (more accurate and takes into account new changes) or offline (faster, works regardless of network access but may be outdated).
getTimezoneInfoWithCoordinatesandgetTimezoneInfoWithTimezoneIdmethods retrieve timezone information asynchronously.- Both methods support an
accurateResultparameter that determines whether to use online services (more accurate) or offline data (faster).
The TimezoneResult structure
The TimezoneResult class represents the result of a time zone lookup operation. It contains the following properties:
| Member | Type | Description |
|---|---|---|
dstOffset | Int | Daylight Saving Time (DST) offset in seconds. |
utcOffset | Int | The raw UTC offset in seconds, excluding DST. Can be negative. |
offset | Int | The total offset in seconds in respect to UTC (dstOffset + utcOffset). Can be negative. |
status | ETZStatus | Status of the response. See the values below. |
timezoneId | String? | The timezone identifier in format Continent/City_Name. Examples: Europe/Paris, America/New_York. |
localTime | Time? | The local time as a Time object representing the local time of the requested timezone. |
The TimezoneResult properties are read-only and can only be accessed after a successful timezone lookup operation.
The ETZStatus enum represents the status of a time zone lookup operation. It can have the following values:
Success- the request was successful.InvalidCoordinate- the provided geographic coordinates were invalid or out of range.WrongTimezoneId- the provided timezone identifier was malformed or not recognized.WrongTimestamp- the provided timestamp (Time) was invalid or could not be parsed.TimezoneNotFound- no timezone could be found for the given input.
Get timezone info by coordinates
The getTimezoneInfoWithCoordinates method allows you to retrieve time zone information based on geographic coordinates (latitude and longitude) and a UTC Time object.
This can be useful for applications that need to determine the local time zone for a specific location.
Basic usage example
- Kotlin
- Java
val timezoneResult = TimezoneResult()
val coordinates = Coordinates(55.626, 37.457)
val utcTime = Time.getUniversalTime() // Current UTC time
val timezoneListener = ProgressListener.create(
onCompleted = { errorCode, _ ->
if (errorCode == GemError.NoError) {
// Success - access timezone data
when (timezoneResult.status) {
ETZStatus.Success -> {
val timezoneId = timezoneResult.timezoneId
val offset = timezoneResult.offset // Total offset in seconds
val dstOffset = timezoneResult.dstOffset // DST offset in seconds
val utcOffset = timezoneResult.utcOffset // UTC offset in seconds
val localTime = timezoneResult.localTime
// Use the timezone information
//displayTimezoneInfo(timezoneId, offset, localTime)
}
ETZStatus.InvalidCoordinate -> {
Log.e("TimezoneService", "Invalid coordinates provided")
}
ETZStatus.TimezoneNotFound -> {
Log.e("TimezoneService", "No timezone found for location")
}
else -> {
Log.e("TimezoneService", "Timezone lookup failed: ${timezoneResult.status}")
}
}
} else {
Log.e("TimezoneService", "Request failed: ${GemError.getMessage(errorCode)}")
}
},
postOnMain = true
)
// Request timezone information
// accurateResult = true: Use online service (more accurate, requires internet)
// accurateResult = false: Use offline data (faster, may be outdated)
val errorCode = TimezoneService.getTimezoneInfoWithCoordinates(
timezoneResult = timezoneResult,
coordinates = coordinates,
time = utcTime!!,
listener = timezoneListener,
accurateResult = true
)
if (GemError.isError(errorCode)) {
Log.e("TimezoneService", "Failed to start timezone request: ${GemError.getMessage(errorCode)}")
}
TimezoneResult timezoneResult = new TimezoneResult();
Coordinates coordinates = new Coordinates(55.626, 37.457);
Time utcTime = Time.getUniversalTime(); // Current UTC time
ProgressListener timezoneListener = ProgressListener.create(
(errorCode, hint) -> {
if (errorCode == GemError.NoError) {
// Success - access timezone data
if (timezoneResult.getStatus() == ETZStatus.Success) {
String timezoneId = timezoneResult.getTimezoneId();
int offset = timezoneResult.getOffset(); // Total offset in seconds
int dstOffset = timezoneResult.getDstOffset(); // DST offset in seconds
int utcOffset = timezoneResult.getUtcOffset(); // UTC offset in seconds
Time localTime = timezoneResult.getLocalTime();
// Use the timezone information
//displayTimezoneInfo(timezoneId, offset, localTime);
} else if (timezoneResult.getStatus() == ETZStatus.InvalidCoordinate) {
Log.e("TimezoneService", "Invalid coordinates provided");
} else if (timezoneResult.getStatus() == ETZStatus.TimezoneNotFound) {
Log.e("TimezoneService", "No timezone found for location");
} else {
Log.e("TimezoneService", "Timezone lookup failed: " + timezoneResult.getStatus());
}
} else {
Log.e("TimezoneService", "Request failed: " + GemError.getMessage(errorCode));
}
},
true
);
// Request timezone information
// accurateResult = true: Use online service (more accurate, requires internet)
// accurateResult = false: Use offline data (faster, may be outdated)
int errorCode = TimezoneService.getTimezoneInfoWithCoordinates(
timezoneResult,
coordinates,
utcTime,
timezoneListener,
true
);
if (GemError.isError(errorCode)) {
Log.e("TimezoneService", "Failed to start timezone request: " + GemError.getMessage(errorCode));
}
Using offline data
For faster results that don't require internet connectivity, use accurateResult = false:
- Kotlin
- Java
val timezoneResult = TimezoneResult()
val coordinates = Coordinates(55.626, 37.457)
val utcTime = Time.getUniversalTime()
val timezoneListener = ProgressListener.create(
onCompleted = { errorCode, _ ->
if (errorCode == GemError.NoError && timezoneResult.status == ETZStatus.Success) {
// Process offline timezone result
val localTime = timezoneResult.localTime
val timezoneId = timezoneResult.timezoneId
// Note: Offline data may be outdated
}
},
postOnMain = true
)
// Use offline data for faster response
TimezoneService.getTimezoneInfoWithCoordinates(
timezoneResult,
coordinates,
utcTime!!,
timezoneListener,
accurateResult = false
)
TimezoneResult timezoneResult = new TimezoneResult();
Coordinates coordinates = new Coordinates(55.626, 37.457);
Time utcTime = Time.getUniversalTime();
ProgressListener timezoneListener = ProgressListener.create(
(errorCode, hint) -> {
if (errorCode == GemError.NoError && timezoneResult.getStatus() == ETZStatus.Success) {
// Process offline timezone result
Time localTime = timezoneResult.getLocalTime();
String timezoneId = timezoneResult.getTimezoneId();
// Note: Offline data may be outdated
}
},
true
);
// Use offline data for faster response
TimezoneService.getTimezoneInfoWithCoordinates(
timezoneResult,
coordinates,
utcTime,
timezoneListener,
false
);
Get timezone info by timezone ID
The getTimezoneInfoWithTimezoneId method allows you to retrieve time zone information based on a specific timezone ID and a UTC Time object.
Basic usage example
- Kotlin
- Java
val timezoneResult = TimezoneResult()
val timezoneId = "Europe/Moscow"
val utcTime = Time.getUniversalTime()
val timezoneListener = ProgressListener.create(
onCompleted = { errorCode, _ ->
if (errorCode == GemError.NoError) {
when (timezoneResult.status) {
ETZStatus.Success -> {
val offset = timezoneResult.offset
val localTime = timezoneResult.localTime
val dstOffset = timezoneResult.dstOffset
// Use the timezone information
//processTimezoneData(offset, localTime, dstOffset)
}
ETZStatus.WrongTimezoneId -> {
Log.e("TimezoneService", "Invalid timezone ID: $timezoneId")
}
else -> {
Log.e("TimezoneService", "Timezone lookup failed")
}
}
} else {
Log.e("TimezoneService", "Request failed: ${GemError.getMessage(errorCode)}")
}
},
postOnMain = true
)
// Request timezone information by ID
val errorCode = TimezoneService.getTimezoneInfoWithTimezoneId(
timezoneResult = timezoneResult,
timezoneId = timezoneId,
time = utcTime!!,
listener = timezoneListener,
accurateResult = true
)
if (GemError.isError(errorCode)) {
Log.e("TimezoneService", "Failed to start timezone request")
}
TimezoneResult timezoneResult = new TimezoneResult();
String timezoneId = "Europe/Moscow";
Time utcTime = Time.getUniversalTime();
ProgressListener timezoneListener = ProgressListener.create(
(errorCode, hint) -> {
if (errorCode == GemError.NoError) {
if (timezoneResult.getStatus() == ETZStatus.Success) {
int offset = timezoneResult.getOffset();
Time localTime = timezoneResult.getLocalTime();
int dstOffset = timezoneResult.getDstOffset();
// Use the timezone information
//processTimezoneData(offset, localTime, dstOffset);
} else if (timezoneResult.getStatus() == ETZStatus.WrongTimezoneId) {
Log.e("TimezoneService", "Invalid timezone ID: " + timezoneId);
} else {
Log.e("TimezoneService", "Timezone lookup failed");
}
} else {
Log.e("TimezoneService", "Request failed: " + GemError.getMessage(errorCode));
}
},
true
);
// Request timezone information by ID
int errorCode = TimezoneService.getTimezoneInfoWithTimezoneId(
timezoneResult,
timezoneId,
utcTime,
timezoneListener,
true
);
if (GemError.isError(errorCode)) {
Log.e("TimezoneService", "Failed to start timezone request");
}
Using offline data
For faster results without internet dependency:
- Kotlin
- Java
val timezoneResult = TimezoneResult()
val timezoneListener = ProgressListener.create(
onCompleted = { errorCode, _ ->
if (errorCode == GemError.NoError && timezoneResult.status == ETZStatus.Success) {
// Process offline result
val offset = timezoneResult.offset
val localTime = timezoneResult.localTime
}
},
postOnMain = true
)
TimezoneService.getTimezoneInfoWithTimezoneId(
timezoneResult,
"Europe/Moscow",
Time.getUniversalTime()!!,
timezoneListener,
accurateResult = false // Use offline data
)
TimezoneResult timezoneResult = new TimezoneResult();
ProgressListener timezoneListener = ProgressListener.create(
(errorCode, hint) -> {
if (errorCode == GemError.NoError && timezoneResult.getStatus() == ETZStatus.Success) {
// Process offline result
int offset = timezoneResult.getOffset();
Time localTime = timezoneResult.getLocalTime();
}
},
true
);
TimezoneService.getTimezoneInfoWithTimezoneId(
timezoneResult,
"Europe/Moscow",
Time.getUniversalTime(),
timezoneListener,
false // Use offline data
);
Working with Time objects
The Time class provides utilities for creating and manipulating time values:
- Kotlin
- Java
// Create UTC time
val utcTime = Time.getUniversalTime()
// Create local time
val localTime = Time.getLocalTime()
// Create custom time
val customTime = Time().apply {
year = 2025
month = 7
day = 1
hour = 6
minute = 0
second = 0
}
// Convert to timestamp (milliseconds since epoch)
val timestamp = customTime.asLong()
// Create from timestamp
val timeFromTimestamp = Time().apply {
fromLong(timestamp)
}
// Create UTC time
Time utcTime = Time.getUniversalTime();
// Create local time
Time localTime = Time.getLocalTime();
// Create custom time
Time customTime = new Time();
customTime.setYear(2025);
customTime.setMonth(7);
customTime.setDay(1);
customTime.setHour(6);
customTime.setMinute(0);
customTime.setSecond(0);
// Convert to timestamp (milliseconds since epoch)
long timestamp = customTime.asLong();
// Create from timestamp
Time timeFromTimestamp = new Time();
timeFromTimestamp.fromLong(timestamp);