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

Civil time offsets are not working properly. #2946

Closed
LakshanWeerasinghe opened this issue May 23, 2022 · 3 comments
Closed

Civil time offsets are not working properly. #2946

LakshanWeerasinghe opened this issue May 23, 2022 · 3 comments
Assignees
Labels
module/time Team/DIU Data, IO, and Util packages related issues Type/Improvement

Comments

@LakshanWeerasinghe
Copy link
Contributor

LakshanWeerasinghe commented May 23, 2022

Description:
When following code snippet execute in the Ballerina Playground. It produce a incorrect time when the zone is adjusted.

Steps to reproduce:

import ballerina/io;
import ballerina/time;
public function main() returns error?{

    final string TIME_ZONE = "Asia/Colombo";
    final time:ZoneOffset TIME_UTC_OFFSET = {hours: 5, minutes: 30, seconds: 0d};

    time:Utc utc1 = time:utcNow();
    time:Civil civil1 = time:utcToCivil(utc1);
    civil1.timeAbbrev = TIME_ZONE;
    civil1.utcOffset = TIME_UTC_OFFSET;

    time:Utc utc2 = check time:utcFromCivil(civil1);

    io:println("UTC Time; "+ time:utcToEmailString(utc1));
    io:println("Local Time(SL); "+ time:utcToEmailString(utc2));
}

Screenshot 2022-05-23 at 15 38 21

@BuddhiWathsala
Copy link
Contributor

Explicitly updating the time abbreviation and time zone is not the way to shift a given timestamp to a new time zone. Here, when you change the time zone, it would mislead the time:utcToEmailString(utc2) API. ATM, we don't have an API to convert time to string with preserving the time zone. We can introduce such APIs in future releases based on your requirements if you could elaborate your requirement here.

@BuddhiWathsala BuddhiWathsala transferred this issue from ballerina-platform/ballerina-lang May 24, 2022
@niveathika niveathika added the Team/DIU Data, IO, and Util packages related issues label Nov 16, 2022
@TharmiganK
Copy link
Contributor

TharmiganK commented Sep 27, 2024

Here, when you set the offset and time abbreviation manually on the civil value then it completely changes the time value.

The time:utcFromCivil will always return a standard time and when you update the offset and time abbreviation on top of that result that will refer to a different time (stadard time 3.00 a.m. is not equal to the Colombo local time 3.00 a.m.). So when you convert it to utc it will point to a different time.

You can do zone adjustments using the Zone APIs. For example:

import ballerina/io;
import ballerina/time;

public function main() returns error? {
    time:Utc utc1 = time:utcNow();
    final string TIME_ZONE = "Asia/Colombo";
    time:Zone? zone = time:getZone(TIME_ZONE);

    if zone is time:Zone {
        time:Civil timeInZone = zone.utcToCivil(utc1);
        io:println("Time in " + TIME_ZONE + ": " + timeInZone.toString());

        time:Utc utc2 = check zone.utcFromCivil(timeInZone);
        // Both of the UTC times should be equal
        io:println("UTC Time 1 : " + time:utcToEmailString(utc1));
        io:println("UTC Time 2 : " + time:utcToEmailString(utc2));
    } else {
        io:println("Failed to load the time zone.");
    }
}

@daneshk
Copy link
Member

daneshk commented Sep 27, 2024

As @TharmiganK mentioned, if you need to convert UTC time to a different time zone, you must use the Zone APIs. We are adding a TimeZone example with the issue[1]. Hence, I am closing this issue. Please feel free to reopen if you need more help on the issue

  1. Add a BBE for time zone API usage ballerina-dev-website#9362

@daneshk daneshk closed this as completed Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/time Team/DIU Data, IO, and Util packages related issues Type/Improvement
Projects
None yet
Development

No branches or pull requests

5 participants