Skip to content

Commit

Permalink
Merge pull request #5684 from ballerina-platform/time-zone-bbe
Browse files Browse the repository at this point in the history
Add time zone BBE
  • Loading branch information
TharmiganK authored Sep 30, 2024
2 parents 3bbd882 + ba00cf8 commit e0f83cd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4436,6 +4436,13 @@
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": false
},
{
"name": "Time Zone",
"url": "time-zone",
"verifyBuild": true,
"verifyOutput": false,
"isLearnByExample": false
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion examples/time-utc-and-civil/time_utc_and_civil.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :::

Expand Down
2 changes: 1 addition & 1 deletion examples/time-utc/time_utc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :::

Expand Down
30 changes: 30 additions & 0 deletions examples/time-zone/time_zone.bal
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.`);
}
}
11 changes: 11 additions & 0 deletions examples/time-zone/time_zone.md
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 :::
2 changes: 2 additions & 0 deletions examples/time-zone/time_zone.metatags
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
5 changes: 5 additions & 0 deletions examples/time-zone/time_zone.out
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}

0 comments on commit e0f83cd

Please sign in to comment.