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

Compare dates by date portion only #4195

Open
DartBot opened this issue Jul 25, 2012 · 7 comments
Open

Compare dates by date portion only #4195

DartBot opened this issue Jul 25, 2012 · 7 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jul 25, 2012

This issue was originally filed by thebinarysearc...@gmail.com


There is no easy way to compare the date portion of two dates.

eg. d1.date == d2.date

@dgrove
Copy link
Contributor

dgrove commented Jul 25, 2012

Set owner to @floitschG.
Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@alan-knight
Copy link
Contributor

This would be useful, but so would the corresponding
  d1.time == d2.time
and it's an interesting question what those would return. A date with its other fields zeroed out would work for comparing the dat portion, but not for the time. You could possibly return something like the number of some unit since EPOCH, but that would likely be confusing for naive uses of aDate.time -> some very large integer. And that would break on Javascript.

@DartBot
Copy link
Author

DartBot commented Sep 7, 2012

This comment was originally written by thebinarysearcht...@gmail.com


Maybe avoid exposing properties and instead have methods that perform comparisons, such as d1.IsAtSameTimeOfDayAs(d2)

hah.

@floitschG
Copy link
Contributor

Removed the owner.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels May 7, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-2 and removed core-m labels Aug 31, 2017
@cedvdb
Copy link

cedvdb commented May 28, 2022

This issue originates from that fact that the modeling of date is incorrect under DateTime, which holds superfluous and wrong information about a date ( if it's only a date).

I would say having two additional types ( Date & Time ) would solve this issue. Both could be merged to form a DateTime if needed.

final time = Time.now();
final date = Date.today();
final dateTime = DateTime.fromParts(date, time);
final inTenSeconds = time.add(const Duration(seconds: 10));
final tomorrow = Date.tomorrow();
final nextDateTime = DateTime.fromParts(date, inTenSeconds)); // copy with time
final tomorrowDateTime = DateTime.fromParts(tomorrow, time)); // copy with date

Those are real world, everyday distinct values, that are in everyday conversations. As such I think those should be part of the sdk.

The main issue I have with DateTime representing a date is that it furnish an incorrect api, an user might call date.hour which is invalid.

The main issue I have with no Time type is that every library that needs it implements their own little class, so you end up mapping from your internal implementation, to flutter, to another impl.

Note: Ranges would also be nice. DateRange, TimeRange, DateTimeRange.

@lrhn
Copy link
Member

lrhn commented May 29, 2022

The recommended approach is to represent a plain dates ("calendar date") as a UTC DateTime with zero hours, minutes, seconds and microseconds.
A plain time ("clock time"), is probably best represented as a Duration.

You can still ask a UTC plain date about its hours (it'll be zero).

As long as you stay away from local-time DateTimes, you should avoid most of the pitfalls.

@cedvdb
Copy link

cedvdb commented May 29, 2022

The "recommended" approach (and only remotely accurate approach that is using DateTime) still suffer from incorrect modeling and incorrect api surface. Ence my proposal of multiple types that more accurately represent reality. The fact that it also solves every issue cited here is also telling. Is there something you do not like about that proposal ? I know most languages I used do not make the distinction, that could be an hint that there is something bad about it. I was thinking maybe serialization but daysSinceEpoch would be as valid as millis, or maybe timezones would break that ?

No matter how documented it is, if it's not enforced by the compiler, someone will use the DateTime.hour to compare it to DateTime.now() and that will lead to an issue, which would have been easily avoidable if it was modeled correctly.

So I'm not a fan of the recommended approach, even if that's what I use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants