Skip to content

DateTime difference sometimes calculated incorrectly #861

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

Closed
kirkmorrow opened this issue Feb 17, 2025 · 2 comments
Closed

DateTime difference sometimes calculated incorrectly #861

kirkmorrow opened this issue Feb 17, 2025 · 2 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@kirkmorrow
Copy link

void main() {
final x = DateTime(2022, 12, 31);
final y = DateTime(2023, 3, 12);
final z = DateTime(2023, 3, 13);
final foo = y.difference(x).inDays;
final bar = z.difference(x).inDays;
print(foo);
print(bar);
}

Result:
71
71

There are 71 days between Dec. 31st and March 12, but not March 13.

@kirkmorrow
Copy link
Author

Further digging:

void main() {
void main() {
final x = DateTime(2023, 3, 12, 2);
final y = DateTime(2023, 3, 12, 3);
print(x.millisecondsSinceEpoch);
print(y.millisecondsSinceEpoch);
print(x.difference(y).inMicroseconds);
}

Result:
1678608000000
1678608000000
0

No time passes between 2am and 3am on March 12, 2023?

@lrhn
Copy link
Member

lrhn commented Feb 18, 2025

Correct. Daylight saving starts at 2 AM, at which point the time zone changes so that it's now 3 AM.
No time passed between the two, it's the same point in time.

Use UTC time if you want to work with dates only.
The inDays really means "in multiples of 24 hours", and some calendar days have 23 or 25 hours.

So, working as designed. You can't ignore daylight saving when using local-time DateTimes.

@lrhn lrhn added the closed-as-intended Closed as the reported issue is expected behavior label Feb 18, 2025
@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants