ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 65 forks source link

Formatting issues when UTC time is converted to Beijing time with the use of timezone.utcToCivil function #2791

Open sarindaSenevirathne opened 2 years ago

sarindaSenevirathne commented 2 years ago

Description: In this use case, what we are trying to do is convert the UTC time to the Beijing time and return the time in the string format of 2022-03-15 13:58:34. But the civil record returns by the above-mentioned function is as follows,

{"utcOffset":{"hours":8,"minutes":0},"timeAbbrev":"Asia/Harbin","dayOfWeek":2,"year":2022,"month":3,"day":15,"hour":14,"minute":1,"second":42.501306}

In there, the month, date, hour, minute and second is in the format 3 and not in the format 03. What would be the way to do this formating

The code I am using to do the conversion -

import ballerina/io;
import ballerina/time;

public function main() returns error? {
    io:println(timeConversion(time:utcNow())); 

}

function timeConversion(time:Utc utc) returns string{
    time:Zone? beijinZone = time:getZone("Asia/Harbin");
    string formatedTime = "";
    if beijinZone is time:Zone {
        time:Civil civil = beijinZone.utcToCivil(utc);
        civil.utcOffset = {"hours": 8};

            formatedTime = string `${civil.year}${"-"}${civil.month}${"-"}${civil.day}${" "}${civil.hour}${":"}${civil.minute}${":"}${civil.second.toString()}`;
    }

    return formatedTime;

}

Affected Versions: Ballerina version - Swanlake GA Time module version - 2.2.0

BuddhiWathsala commented 2 years ago

@sarindaSenevirathne could you elaborate more about the use case?. Why do you need to do that in the first place?. Why you can't use the standard RFC3339 string that we support in the time module.

sarindaSenevirathne commented 2 years ago

@BuddhiWathsala we are doing the above time zone conversion by defining the time zone. But if we use time:civilToString to that again, the conversion get reversed. That's why we need to format the civil record without using time:civilToString

BuddhiWathsala commented 2 years ago

What is the real-world use-case that you need this 2022-03-15 13:58:34 string?. AFAIK, this is not a standard format.

sarindaSenevirathne commented 2 years ago

We exactly do not know why they need this format. We came across this since we are reviewing this customer's code and they have formated the time in this format. We do not know why they need this format

janihur commented 2 years ago

What is the real-world use-case that you need this 2022-03-15 13:58:34 string?. AFAIK, this is not a standard format.

This is not my case but in general there's still (legacy) systems out there that require non standard time formats. And those systems are not going to change so if you're going to work with them you have to adapt.

daoxxx commented 2 years ago

@sarindaSenevirathne could you elaborate more about the use case?. Why do you need to do that in the first place?. Why you can't use the standard RFC3339 string that we support in the time module.

@BuddhiWathsala Because in the real world scenarios we need to communicate with different not so ideal systems. Right now i have backend that accepts time in the rfc3339 format but with the mandatory milliseconds part. 2022-05-02T11:00:14.000+03:00 civilToString function strips milliseconds in this case. This leads to unwanted string manipulations and renders Time module almost useless. It would be very nice if civilToString() function has format pattern parameter. Like in any mature programming language.