-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5684 from ballerina-platform/time-zone-bbe
Add time zone BBE
- Loading branch information
Showing
7 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import ballerina/io; | ||
import ballerina/time; | ||
|
||
public function main() returns error? { | ||
// Event recorded in UTC (2022-01-29 22:48:00). | ||
time:Utc eventUtcTime = check time:utcFromString("2022-01-29T22:48:00Z"); | ||
io:println("Event time in UTC: ", eventUtcTime); | ||
|
||
// Load the system's default time zone. | ||
time:Zone systemZone = check new time:TimeZone(); | ||
// Convert UTC event time to the system's local time. | ||
time:Civil eventInSystemZone = systemZone.utcToCivil(eventUtcTime); | ||
io:println("Event time in system's local time: " + eventInSystemZone.toString()); | ||
|
||
// Print the event time in 'Europe/London' time zone. | ||
printZoneTimeFromUtc(eventUtcTime, "Europe/London"); | ||
|
||
// Print the event time in 'Asia/Tokyo' time zone. | ||
printZoneTimeFromUtc(eventUtcTime, "Asia/Tokyo"); | ||
} | ||
|
||
function printZoneTimeFromUtc(time:Utc utcTime, string zoneId) { | ||
time:Zone? zone = time:getZone(zoneId); | ||
if zone is time:Zone { | ||
time:Civil timeInZone = zone.utcToCivil(utcTime); | ||
io:println(string `Event time in ${zoneId} time zone: ${timeInZone.toString()}`); | ||
} else { | ||
io:println(string `Failed to load the '${zoneId}' time zone.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Time Zone | ||
|
||
The Ballerina `time` library provides APIs for managing and converting time across different time zones. It supports loading system time zone, retrieving specific time zones based on the time zone ID, and enables seamless conversion between UTC and local times across different regions. | ||
|
||
For more information on the underlying module, see the [`time` module](https://lib.ballerina.io/ballerina/time/latest/). | ||
|
||
::: code time_zone.bal ::: | ||
|
||
To run this sample, use the `bal run` command. | ||
|
||
::: out time_zone.out ::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: BBE on how to use time zone APIs in Ballerina. | ||
keywords: ballerina, ballerina by examples, bbe, time, zone, utc, zoneId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$ bal run time_zone.bal | ||
Event time in UTC: [1643496480,0] | ||
Event time in system's local time: {"timeAbbrev":"Asia/Colombo","dayOfWeek":0,"year":2022,"month":1,"day":30,"hour":4,"minute":18,"second":0} | ||
Event time in Europe/London time zone: {"timeAbbrev":"Europe/London","dayOfWeek":6,"year":2022,"month":1,"day":29,"hour":22,"minute":48,"second":0} | ||
Event time in Asia/Tokyo time zone: {"timeAbbrev":"Asia/Tokyo","dayOfWeek":0,"year":2022,"month":1,"day":30,"hour":7,"minute":48,"second":0} |