apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
61.76k stars 13.51k forks source link

Advanced Period filter bug #29881

Open maksperskiy opened 1 month ago

maksperskiy commented 1 month ago

Bug description

Setting of "01.08.2023 - 31.12.2023" in Advanced Period filter it parses "01.08.2023" as January 8, 2023 instead of August 1, 2023, although 31.12.2023 parses correctly image

OS: Windows 11 OS Language: Ru Browser: YaBrowser (Chromium)

How to reproduce the bug

Add Time period filter to dashboard. Choose Advanced option and set "01.08.2023" as start and "31.12.2023" as end.

Screenshots/recordings

No response

Superset version

4.0.2

Python version

Not applicable

Node version

Not applicable

Browser

Chrome

Additional context

No response

Checklist

dosubot[bot] commented 1 month ago

The issue you're experiencing with the date parsing in the Advanced Period filter is likely due to the locale settings. The DatePicker component in the code uses the locale prop, which is derived from the datePickerLocale variable. This variable is set based on the locale defined in the ExplorePageState.

To address the issue, you should ensure that the datePickerLocale is correctly set to match your locale settings, which in your case should be Russian. The LOCALE_MAPPING should map the Russian locale to the appropriate locale for the DatePicker.

Here's the relevant part of the code that sets the datePickerLocale:

// check if there is a locale defined for explore
const localFromFlaskBabel = useSelector(
  (state: ExplorePageState) => state?.common?.locale,
);

// An undefined datePickerLocale is acceptable if no match is found in the LOCALE_MAPPING[localFromFlaskBabel] lookup
// and will fall back to antd's default locale when the antd DataPicker's prop locale === undefined
// This also protects us from the case where state is populated with a locale that antd locales does not recognize
const datePickerLocale =
  locales[LOCALE_MAPPING[localFromFlaskBabel]]?.DatePicker;

Ensure that LOCALE_MAPPING includes a mapping for the Russian locale, and that localFromFlaskBabel is correctly set to the Russian locale. If the mapping is missing or incorrect, the DatePicker might default to a locale that interprets dates in the MM.DD.YYYY format instead of DD.MM.YYYY.

If the locale mapping is correct and the issue persists, you might need to explicitly set the locale prop for the DatePicker to ensure it uses the correct date format:

<DatePicker
  showTime
  defaultValue={dttmToMoment(sinceDatetime)}
  onChange={(datetime: Moment) =>
    onChange('sinceDatetime', datetime.format(MOMENT_FORMAT))
  }
  allowClear={false}
  locale={locales['ru_RU'].DatePicker} // Explicitly set to Russian locale
/>

Additionally, the parseDttmToDate function in the getTimeOffset.ts file is responsible for parsing date strings. Ensure that this function correctly handles the DD.MM.YYYY format:

const parseDttmToDate = (
  dttm: string,
  isEndDate = false,
  computingShifts = false,
) => {
  // Existing logic...

  const parts = dttm?.split('.');
  let parsed: Date | null = null;
  if (parts && !isEmpty(parts)) {
    if (parts.length === 3) {
      parsed = new Date(
        parseInt(parts[2], 10),
        parseInt(parts[1], 10) - 1,
        parseInt(parts[0], 10),
      );
    } else {
      parsed = new Date(dttm);
    }
  } else {
    parsed = new Date(dttm);
  }

  // Existing logic...
};

This should ensure that the DatePicker correctly interprets the date format as DD.MM.YYYY [1][2].

To continue talking to Dosu, mention @dosu.

rusackas commented 1 month ago

I can't remember the name of it, but there's a library under the hood that does this parsing, so it can read most ways of typing a date, from 01/08/2024, to 01.08.24 to "last January" or "next Tuesday". If you type a date like 01.08.2024, it has to make a guess as to whether you mean US-formatted or... well... "anywhere else" formatted. If you want to work around this, you can type an ISO style date, e.g. 2024-08-01, or "August 1st"