ytti / oxidized-web

Web UI + RESTful API for Oxidized
118 stars 72 forks source link

Enhancement Suggestion for Time Conversion Script: Improved Accuracy and Optimization #258

Open HelloLoser opened 3 months ago

HelloLoser commented 3 months ago

Description:

I've reviewed the JavaScript snippet responsible for converting UTC times within elements bearing the .time class into the local browser time. The existing logic is commendable but presents an opportunity for refinement to enhance both accuracy and performance. Below are my observations and recommendations:

Identified Issue:

Proposed Enhancements:

  1. Leverage Intl.DateTimeFormat for Precise Timezone Management: To tackle the timezone inconsistency issue, I suggest employing the Intl.DateTimeFormat API. This built-in JavaScript object provides a robust mechanism for formatting dates and times according to the user's locale preferences, inherently handling timezone conversions accurately. By utilizing this API, we can avoid manual timezone string manipulations and ensure compatibility across diverse environments.

Here's the revised code snippet reflecting these improvements:

var convertTime = function() {
    $('.time').each(function() {
        var content = $(this).text();
        if(content === 'never' || content === 'unknown' || content === '') { return; }

        // Directly parse the UTC time string
        var utcTime = content.split(' ');
        var date = new Date(utcTime[0] + 'T' + utcTime[1] + 'Z');

        // Utilize Intl.DateTimeFormat for localization and timezone formatting
        var options = {
            year: 'numeric', month: '2-digit', day: '2-digit',
            hour: '2-digit', minute: '2-digit', second: '2-digit',
            hour12: false, // Use 24-hour format
            timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone // Adopt the user's browser timezone
        };
        var formattedTime = new Intl.DateTimeFormat('default', options).format(date);

        $(this).text(formattedTime);
    });
};

By adopting this updated approach, we can significantly improve the reliability of timezone conversions while simultaneously streamlining the codebase. This change is particularly crucial for applications that demand high accuracy in timezone handling and strive for a seamless user experience across global audiences.

Please consider integrating these enhancements into the project to bolster the script's effectiveness and maintainability.

Looking forward to your feedback and further discussion on implementing these improvements.

robertcheramy commented 3 months ago

Thank you for the issue and contribution. I'll have a look at this. converTime throws an error in /node/version/diffs , I want to address this too.

robertcheramy commented 2 months ago

The whole date thing needs to be updated. This needs a change in the Oxidized-API => Changing milestone to 0.15.0

Needed changes in oxidized: transmit the date as an object, not as a string. I'm not sure yet if I want a second attribute (hash[:date_object] for backward compatibility). I probably want a brand new API for external plugins, an no deep access to internal objects of oxidized. https://github.com/ytti/oxidized/blob/9dbc83038d764dd9f828a4c60d21eedacf6d4fec/lib/oxidized/output/git.rb#L88 https://github.com/ytti/oxidized/blob/9dbc83038d764dd9f828a4c60d21eedacf6d4fec/lib/oxidized/output/gitcrypt.rb#L116