diff --git a/examples/index.json b/examples/index.json index 15332e6527..4fb886e683 100644 --- a/examples/index.json +++ b/examples/index.json @@ -4436,6 +4436,13 @@ "verifyBuild": true, "verifyOutput": true, "isLearnByExample": false + }, + { + "name": "Time Zone", + "url": "time-zone", + "verifyBuild": true, + "verifyOutput": false, + "isLearnByExample": false } ] }, diff --git a/examples/time-utc-and-civil/time_utc_and_civil.md b/examples/time-utc-and-civil/time_utc_and_civil.md index e3c08839ae..3f127412f2 100644 --- a/examples/time-utc-and-civil/time_utc_and_civil.md +++ b/examples/time-utc-and-civil/time_utc_and_civil.md @@ -2,7 +2,7 @@ The Ballerina `time` library contains APIs to convert UTC to local time and vice versa. -For more information on the underlying module, see the [`time` module module](https://lib.ballerina.io/ballerina/time/latest/). +For more information on the underlying module, see the [`time` module](https://lib.ballerina.io/ballerina/time/latest/). ::: code time_utc_and_civil.bal ::: diff --git a/examples/time-utc/time_utc.md b/examples/time-utc/time_utc.md index 72031847b9..2574f22364 100644 --- a/examples/time-utc/time_utc.md +++ b/examples/time-utc/time_utc.md @@ -2,7 +2,7 @@ The Ballerina `time` library contains an API to obtain the current time from the epoch `1970-01-01T00:00:00`. -For more information on the underlying module, see the [`time` module module](https://lib.ballerina.io/ballerina/time/latest/). +For more information on the underlying module, see the [`time` module](https://lib.ballerina.io/ballerina/time/latest/). ::: code time_utc.bal ::: diff --git a/examples/time-zone/time_zone.bal b/examples/time-zone/time_zone.bal new file mode 100644 index 0000000000..f12fee85a6 --- /dev/null +++ b/examples/time-zone/time_zone.bal @@ -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.`); + } +} diff --git a/examples/time-zone/time_zone.md b/examples/time-zone/time_zone.md new file mode 100644 index 0000000000..319c1b44f1 --- /dev/null +++ b/examples/time-zone/time_zone.md @@ -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 ::: \ No newline at end of file diff --git a/examples/time-zone/time_zone.metatags b/examples/time-zone/time_zone.metatags new file mode 100644 index 0000000000..400a71c859 --- /dev/null +++ b/examples/time-zone/time_zone.metatags @@ -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 diff --git a/examples/time-zone/time_zone.out b/examples/time-zone/time_zone.out new file mode 100644 index 0000000000..1c4cab5ae5 --- /dev/null +++ b/examples/time-zone/time_zone.out @@ -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}