Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time zone BBE #5684

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
TharmiganK marked this conversation as resolved.
Show resolved Hide resolved
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());
TharmiganK marked this conversation as resolved.
Show resolved Hide resolved

// 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}: ${timeInZone.toString()}`);
TharmiganK marked this conversation as resolved.
Show resolved Hide resolved
} 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: {"timeAbbrev":"Europe/London","dayOfWeek":6,"year":2022,"month":1,"day":29,"hour":22,"minute":48,"second":0}
Event Time in Asia/Tokyo: {"timeAbbrev":"Asia/Tokyo","dayOfWeek":0,"year":2022,"month":1,"day":30,"hour":7,"minute":48,"second":0}
Loading