woocommerce / woocommerce-ios

WooCommerce iOS app
https://www.woocommerce.com/mobile
GNU General Public License v2.0
301 stars 109 forks source link

[Analytics Hub] Dates for selected date range can be wrong depending on timezone #12492

Open rachelmcr opened 6 months ago

rachelmcr commented 6 months ago

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:

Today Last Quarter
IMG_FDF8173F6465-1 IMG_DD80235F23F5-1

This seems to be connected to my timezone (UTC+1) and how the quarter start date is calculated:

  1. The current date is taken as reference (17 April).
  2. We find the date three months earlier (17 January).
  3. 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.
  4. 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.

dangermattic commented 6 months ago

Thanks for reporting! 👍