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

Improve yyyy-MM-dd date formatter to support dates happening at DST #771

Conversation

staskus
Copy link
Contributor

@staskus staskus commented Mar 26, 2024

Description

Fixes wordpress-mobile/WordPress-iOS#22859

We need to pass yyyy-MM-dd date to API when fetching stats/visits and other similar endpoints. However, in some cases, date conversions to date string fails.

The bug is related to TimeZone and DTS. For example, Paraguay has a DTS change at midnight 2023-10-01 which is ambiguous to DateFormatter and nil is returned.

Date formatter we use:

private var periodDataQueryDateFormatter: DateFormatter {
    let df = DateFormatter()
    df.locale = Locale(identifier: "en_US_POSIX")
    df.dateFormat = "yyyy-MM-dd"
    return df
}

Result for "America/Asuncion" time zone:

df.timeZone = TimeZone(identifier: "America/Asuncion")
df.date(from: "2023-10-01") // result nil

Result for "Europe/Vilnius" time zone:

po df.timeZone = TimeZone(identifier: "Europe/Vilnius")
po df.date(from: "2023-10-01") // result <Date> 2023-09-30 21:00:00 +0000

In documentation Apple suggests to set the timezone to UTC:

When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale ("en_US_POSIX"), and set the timeZone property to UTC.

RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

However, this is not a straightforward solution, since in Stats we're working with dates that are in a different time zone.

Solution

Created RFC339NoTimeDateFormatter that sets:

df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "yyyy-MM-dd"
df.timeZone = TimeZone(secondsFromGMT: 0)

but also recalculates the data to the current time zone after conversion. Therefore, the dates remain unchanged but conversions also succeed. I added RFC339NoTimeDateFormatterTests as an example.

Testing Details


  • Please check here if your pull request includes additional test coverage.
  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary.

@staskus staskus changed the title Fix/22859 stats traffic card information fails to load when date cannot be parsed Improve yyyy-MM-dd date formatter to support dates happening at DST Mar 26, 2024
@staskus staskus requested a review from guarani March 27, 2024 08:54
@staskus staskus closed this Jun 21, 2024
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.

Stats Traffic: Card information fails to load when date cannot be parsed when returned from API
1 participant