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

fix: Wrong weekday names when using format-locale in overridden timezone #911

Merged
merged 2 commits into from
Jun 20, 2024

Conversation

basil-gor
Copy link
Contributor

The same issue we have in #817, but with days names.
In negative timezones, we faced the shift in days. Saturday became the first day of the week.
image

I've added small fix. And also implement some new tests.

I also have an alternative solution without using date-fns format function at all. Will post it bellow, and maybe we can switch to it, because of optimization effort.

@basil-gor
Copy link
Contributor Author

basil-gor commented Jun 14, 2024

The main idea is just get days and months right from formatLocale this way:

function dayNameFormatLocaleMapper(localizeDay: LocalizeFn<Day>) {
    return (day) => {
        return localizeDay(day - 1, { width: 'short' });
    };
}

Note: I didn't mange typing yet. So it's just example for now

in getDayNames use like:

    // Map day order numbers to names
    const localizeDay = formatLocale?.localize?.day;
    if (localizeDay !== undefined) {
        try {
            days = daysArray.map(dayNameFormatLocaleMapper(localizeDay));
        } catch (e) {
            days = daysArray.map(dayNameIntlMapper(locale));
        }
    } else {
        days = daysArray.map(dayNameIntlMapper(locale));
    }

And same for monht:

    const localizeMonth = formatLocale?.localize?.month;
    if (localizeMonth !== undefined) {
        try {
            const monthLocaleLocalizeFormat = monthFormat === 'long' ? 'wide' : 'abbreviated';
            return months.map((date, i) => {
                const month = localizeMonth(i, { width: monthLocaleLocalizeFormat });
                return {
                    text: month.charAt(0).toUpperCase() + month.substring(1),
                    value: i,
                };
            });
        } catch (e) {
            // Do nothing. Go ahead to execute fallback
        }
    }

This solution could be better, because we don't need to use tz utils and format function at all. Also, it could work a bit faster.

@basil-gor
Copy link
Contributor Author

I also encountered an issue when attempting to fix a problem in this PR.
Here is a new report for future #912

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants