The date range selection in the Analytics Hub sometimes calculates the wrong dates for the selected range.
For example, my store is set to London time (currently UTC+1) and the date is April 17. When I select "Last Quarter" I expect it to be Jan-Mar, but instead it shows Oct 1 - Jan 1:
Today
Last Quarter
This seems to be connected to my timezone (UTC+1) and how the quarter start date is calculated:
The current date is taken as reference (17 April).
We find the date three months earlier (17 January).
We take the year and month from that date (January 2024) and create a date from it in the store's timezone. This is where the problem occurs: The date in the given timezone is 1 January 2024 00:00:00 UTC+1, but we handle the date in UTC time which is 2023-12-31 23:00:00 UTC.
We calculate the start of the quarter based on the month in the UTC date value. Since that is December, the quarter start date becomes 1 October 2023 00:00:00 UTC+1.
We need to make sure we're always handling the date in the expected timezone (not UTC) to ensure accurate date calculations. This issue occurs in the Date.startOfQuarter(timezone:calendar:) extension.
Describe the bug
The date range selection in the Analytics Hub sometimes calculates the wrong dates for the selected range.
For example, my store is set to London time (currently UTC+1) and the date is April 17. When I select "Last Quarter" I expect it to be Jan-Mar, but instead it shows Oct 1 - Jan 1:
This seems to be connected to my timezone (UTC+1) and how the quarter start date is calculated:
We need to make sure we're always handling the date in the expected timezone (not UTC) to ensure accurate date calculations. This issue occurs in the
Date.startOfQuarter(timezone:calendar:)
extension.