Skip to content

Fix isToday/isTomorrow day diff for UTC dates across timezone and DST boundaries - #80

Open
paul678 wants to merge 4 commits into
jogboms:masterfrom
paul678:utc_fix
Open

Fix isToday/isTomorrow day diff for UTC dates across timezone and DST boundaries#80
paul678 wants to merge 4 commits into
jogboms:masterfrom
paul678:utc_fix

Conversation

@paul678

@paul678 paul678 commented Mar 29, 2026

Copy link
Copy Markdown

Summary

This changes _calculateDifference(DateTime date) to preserve the timezone basis of the input date before comparing calendar days.

Previously, the implementation rebuilt both values as local midnights:

DateTime(date.year, date.month, date.day)
DateTime(now.year, now.month, now.day)

That was incorrect when date was UTC, because the UTC date was implicitly converted into a local calendar day before the comparison.

Thus avoids daylight saving issues on isToday isTomorrow and wasYesterday
@FMorschel

Copy link
Copy Markdown
Collaborator

Could you please come up with a test case that would fail with the previous implementation and passes now?

Thanks for noticing this!

@paul678

paul678 commented Mar 29, 2026

Copy link
Copy Markdown
Author

Could you please come up with a test case that would fail with the previous implementation and passes now?

Thanks for noticing this!

Added a very simple test. It is using the older _calculateDifference implementation with the exact behaivor I observed today which marked 2 days as "today" in my app 😅

I keep UTC dates throughout my app <-> backend communication and was doing a simple serverDate.isToday and 2 of them where showing as true

@FMorschel

Copy link
Copy Markdown
Collaborator

CC @jogboms

For me, the old calculation on the test file is not needed, but since this is a test, I'm not that worried. Since I can't push new versions, anyway, I'll defer to the package owner.

Again, thanks for the PR!

@jogboms jogboms left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good spot. Thanks @paul678

@jogboms jogboms closed this Mar 30, 2026
@jogboms jogboms reopened this Mar 30, 2026
@jogboms

jogboms commented Mar 30, 2026

Copy link
Copy Markdown
Owner

Could you investigate the failing test? @paul678

@paul678

paul678 commented Apr 8, 2026

Copy link
Copy Markdown
Author

@jogboms Sorry for the delay, got caught in some other stuff...
Seems the tests was failing due to local timezone on CI, trying to force a specific timezone on that one test now and it seems to work 🤞

@codecov

codecov Bot commented Apr 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 21.73913% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.36%. Comparing base (421b33a) to head (d0eeef4).

Files with missing lines Patch % Lines
test/time_test.dart 14.28% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #80      +/-   ##
==========================================
- Coverage   99.85%   97.36%   -2.50%     
==========================================
  Files           2        2              
  Lines         700      721      +21     
==========================================
+ Hits          699      702       +3     
- Misses          1       19      +18     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread test/time_test.dart
Comment on lines +4 to +5
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L5 can be removed, no?

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.

3 participants