TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Kobayashi Maru of comparing dates with times

132 pointsby holmanover 3 years ago

37 comments

IgorPartolaover 3 years ago
The questions do not make sense so stop trying to ask them. It’s like asking if <i>2022-01-01</i> is an orange.<p>Generally speaking you want context of what the date or datetime is being used for. If you want to know when Christmas takes place you will use a date: it does not take place on <i>2022-12-25 00:00:00</i> through <i>2022-12-25 11:59:59</i> because this would require a timezone and Christmas takes place at different UTC times around the globe. But you can reasonably say that Christmas takes place on <i>2022-12-25</i> and leave it at that to let the implementation of whatever program figure out if it is or is not currently Christmas based on the information it has about time and timezone.
评论 #30320152 未加载
评论 #30318581 未加载
评论 #30319831 未加载
评论 #30319894 未加载
bradleybudaover 3 years ago
The answer is no&#x2F;no&#x2F;no because you cannot define a general comparison operator between dates and times without additional context. It&#x27;s possible that you can define a contextual comparison operator that works for your domain and the question you want to ask, but without knowing the application for this comparison it&#x27;s pointless to try to make a general statement.<p>My mental model for these kinds of things is that Times are instants and Dates are either:<p>1. Ranges (with the start and end Time depending on TZ and possibly other context)<p>2. Discrete cells of a calendar (which are mostly TZ independent - July 5th doesn&#x27;t happen at the same time everywhere, but it is well-defined everywhere)<p>Also, I&#x27;ve been writing code for 25 years and I still have no idea what a DateTime is.
评论 #30317133 未加载
评论 #30317267 未加载
评论 #30318175 未加载
评论 #30320294 未加载
评论 #30317741 未加载
评论 #30317579 未加载
lukevover 3 years ago
I am a little shocked that neither the blog post nor the twitter discussion nor any of the discussion here clearly identifies the missteps of this approach beyond alluding to a &quot;category&quot; or &quot;type&quot; error (which is correct, but not particularly informative.)<p>The issue here is around the semantics of the mathematical operators. It isn&#x27;t even <i>really</i> about the types to which they apply; there are systems where `=` is well defined on heterogenous types.<p>The reason the answer to all these questions is a clear &quot;no&quot; is that they do not satisfy the core properties of the operators. For example, take equality. Equality in almost all mathematical constructs means that it supports substitution, is symmetric, transitive and reflexive. There are also well defined properties for the concepts of `greater than` and `less than`.<p>So, no, the OP&#x27;s conclusion &quot;Literally we’re all just making it up&quot; is <i>incorrect</i>. You cannot use the operators `=`, `&lt;`, and `&gt;` between dates and times because they do not satisfy the core properties that <i>define</i> those operators. (I guess you could try to document an alternate definition of equality without the symmetric property in your documentation but... good luck with <i>that</i> not leading to massive confusion.)<p>Where you <i>can</i> just make it up is to define new operators as you actually want them to be. It&#x27;s not `=`, it&#x27;s `myDateTime=()` and then you&#x27;re free to write the definition of that yourself. As long as you&#x27;re consistent in the UI of how you present it (don&#x27;t pretend it&#x27;s vanilla `=` to the user!) you will at least be telling the truth. It may not solve all your problems but at least you won&#x27;t be feeding any more to the fires of confusion, which you <i>will</i> as long as you keep pretending it&#x27;s possible to make `=` mean something that&#x27;s not reflexive.
评论 #30318065 未加载
ineedasernameover 3 years ago
I deal with this on a regular basis, and my approach is straightforward: I don&#x27;t compare them. If a have a date on one side and date-time on the other, then I am missing the data necessary to make a comparison. The question seems like a non-sequitur because the implied question is &quot;how do you compare two variable when one of them does not have the data needed for comparison&quot;<p>The only question in circumstance like this is whether or not my requirements allow for-- or can reasonably be modified-- to strip off time and only compare dates.<p>If time is an inherent requirement to the project then the response I give is simple: &quot;Then begin collecting time data.&quot;
评论 #30319392 未加载
andybakover 3 years ago
I hate to be &quot;that guy&quot; but I think everyone is looking at this wrong.<p>It&#x27;s not a programming issue, a type casting issue or a CS issue.<p>It&#x27;s a UI issue. That the point you got the date and the datetime values, there was a human being (either a user, an admin or a programmer) sitting in front of the screen and they were asked a question in some way.<p>The precise way that question was presented affected their intent. It&#x27;s their intent you are asking about here. So you need to think about what they were asked and how they were asked it. If that was consistent then you can make a reasonable call here. If it&#x27;s inconsistent at different times and places then the meaning of that date is different and you can&#x27;t solve this in any reasonable way.
评论 #30321265 未加载
munch117over 3 years ago
The CS answer: bottom, bottom and bottom.<p>The static typing answer: type error, type error and type error.<p>The dynamic typing answer: exception, exception and exception.<p>The philosophy answer: category error, category error and category error.
lmilcinover 3 years ago
My favourite is battling with people who use 23:59:59.<p>Where I work (banks and other financial institutions) there is frequently a need to check whether something happened within a particular day. Or maybe select records from the database for a day, or do some other kind of logic or filtering.<p>For some unknown reason, most people decide that best way to do this is to take start date as the beginning of the period and then add to it 23 hours, 59 minutes and 59 seconds and use that as the end of the period.<p>Explanations that they are missing a whole second do not seem to be working. People are absolutely convinced they are doing this correctly.<p>I thought about this for a long time and I arrived at an explanation.<p>It seems that some people use the time as a label for a span of time of unit length. March 21st is a label for an entire day. 12 pm is a label for an entire hour that starts at 12pm and lasts an hour, etc. 2021 is a whole year.<p>And in some contexts it makes sense. Way say something happened on 12th of January -- we use Jan, 12th as a label for an entire day that we would otherwise have to denote with two timestamps. But in some contexts what we need is an exact point in time, a timestamp. And here is where a lot of people just don&#x27;t think &#x2F; can&#x27;t recognise a difference between timestamps and labels for a span of time.<p>If you use a wrong model then yes, the day starts with a second labeled 00:00:00 and ends with a second labeled 23:59:59.<p>Except that&#x27;s not how most of the underlying software works. Most software in this case expects two exact timestamps to denote the end of the span of time. And 23:59:59 is just 1 second shy of the actual end of day which means that, even if we are missing an entire second of the day, most of the time everything seems to work fine.<p>Unless you are large bank and you have millions of transactions all over the clock that have to accumulated exactly. Then yes, it makes a lot of difference.<p>Another explanation is that people don&#x27;t seem to be comfortable with the concept of selecting items from between 00:00:00 of one day and 00:00:00 of the next because they are seeing another date.
评论 #30321086 未加载
评论 #30317131 未加载
评论 #30317315 未加载
评论 #30319249 未加载
atsmylesover 3 years ago
No&#x2F;No&#x2F;No definitively. Dates without times are ranges. If one person is born on 2022-06-01 12:00:00 and another person was born on 2022-06-01 13:00:00 then they have the same birthday 2022-06-01. It follows, that if you know that 2 people are born on the same day such as 2022-06-01, then it is unknown if they were born on the same time. So adding a default time to a day (such as 00:00:00) is nonsensical.
评论 #30317189 未加载
评论 #30317731 未加载
d_wattover 3 years ago
Ultimately, this is the same problem as “is ‘04’ &gt; 3”? That is to say, you can only properly compare two things of the same types, and you can either implicitly or explicitly cast.<p>The easiest option is that comparators should only work on the same data type, to avoid any ambiguity, leaving it up to the user to do explicit casting, and throwing errors if they don’t.<p>Of course, like integers and decimals, maybe it makes sense to have implicit casting, but it’s unintuitive to me if “‘2020-01-01’ &gt; ‘2020-01-01 12:000’” should be cast to two dates, two timestamps, or two timestamptzs. Even if a language allows implicit casting, it’s probably an area where not doing it as a author is a smell.
评论 #30316787 未加载
travisgriggsover 3 years ago
I only read the question and am not aware what domain&#x2F;language this is coming from.<p>It seems they’re talking about Instants with different resolutions.<p>My answers are no, no, and yes.<p>If we see what they’re calling date as integers and times as floats, the questions become<p>Is 4 equal to 4.5? No<p>Is 4 between 4.5 and 200.5? No<p>Is 4.5 after 4? Yes
评论 #30316965 未加载
评论 #30316713 未加载
评论 #30316754 未加载
rlhamilover 3 years ago
How about this: a date, or indeed even a time, is either of the shortest unit used in calculation at some moment (say by convention, the start of the date or time expressed perhaps down to nanoseconds); or it is a range, from the start of the date or time (expressed in terms of the shortest unit used in calculation, unless you want to fiddle with going down to Planck times to allow for future increase in precision) up to but not including the start of the next increment at the stated precision (for a time in seconds with a minimum resolution of nanoseconds, the billion nanoseconds starting at the beginning of the second).<p>So,an hour range starting at a half hour boundary might fall entirely within a day, entirely outside that day, or half in that day and half before or after it.<p>All of that neglects time zones, or assumes they&#x27;re at most recorded for local convenience and&#x2F;or historical purposes but converted for calculation into UTC or the like.<p>So, for instants, either they&#x27;re equal or they&#x27;re not; but for ranges, they could be equal, or a smaller range entirely within a larger, or partly overlapping, or disjoint. Pick whichever model (instant or range) works best, and be consistent thereafter, and at least the surprises shouldn&#x27;t be too surprising.
xorcistover 3 years ago
The specific question is strangely put. Why 12:00?<p>In the generic case where one has conflicting data types to work with, including all sorts of lost precision, the boring answer is that it is context dependent.<p>Direct user interfacing applications should follow the principle of least surprise, which for an interval search would be to always treat the value as inclusive when in doubt.
bruce511over 3 years ago
The root of most date&#x2F;time problems stems from the fact that we use&#x2F;have insufficient vocabulary to define what we actually want.<p>For example, &quot;time&quot; can be used as &quot;time of day&quot; or &quot;duration&quot;. In my own work we have to measure &quot;from this time, to that time&quot; and yhd result should be duration. [1]<p>We also use the phrase Time when we mean Timestamp (a date &#x2F;time combination). And we use &quot;local timezone&quot; implicitly almost everywhere, where we should be storing everything as UTC and then displaying as desired. (this would make comparisons trivial.)<p>Daylight savings is an abomination since it means time is not contiguous, and the sooner it goes away the better. This is somewhat solved by using UTC when time-stamping.<p>Overall I prefer storing everything as UTC, then displaying that as preferred by each user (ie as their local time).<p>[1] should be, but is instead stored as a time field for historical design reasons.
wbportover 3 years ago
This site finds the midpoint between date&#x2F;times as well as the difference between them. Also, time units can be added or subtracted from one date&#x2F;time to get another. All of this is done through the JavaScript Date object which leaves to trace of the original time zone in arriving at the offset in milliseconds since 1970. If doing the difference between dates in days, round the difference to the nearest day to avoid problems with daylight savings time not being in effect for one of them. <a href="https:&#x2F;&#x2F;wjporter.com&#x2F;misc&#x2F;MidPoint.htm" rel="nofollow">https:&#x2F;&#x2F;wjporter.com&#x2F;misc&#x2F;MidPoint.htm</a>
pygy_over 3 years ago
Type error.<p>A `date` is a time interval (from midnight to midnight in a given time zone). One could argue that `time` values in computer programs also cover a span, even if it is infinitesimally small for most purposes.<p>The correct operation to compare intervals of varying lengths is not equality, it is either containment or overlap.
评论 #30320060 未加载
cpetersoover 3 years ago
What date&#x2F;time mistakes did UNESCO make that the Ruby documentation is referring to?
评论 #30317658 未加载
nescioquidover 3 years ago
Isn&#x27;t this just a matter of being clear of inclusive or exclusive ranges? Perhaps this is merely a provocation that the general perception of exclusivity or inclusivity rests in the casual programmer&#x27;s perception (rather than the specification of which I confess ignorance)?<p>Or is the provocation in measuring the contour of a coast? Can it be measured more precisely (surely it can)? In which case is the provocation one of mere precision?<p>Or is the provocation one which asks how many fairies may fit on the head of a pin?
franky47over 3 years ago
A list of date&#x2F;time&#x2F;calendar quirks and oddities:<p><a href="https:&#x2F;&#x2F;yourcalendricalfallacyis.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;yourcalendricalfallacyis.com&#x2F;</a>
al_biglanover 3 years ago
No. No. No. Dates are to Integers as Date+Time is to Floating Point. You (we) -want- to compare them, but you need something akin to a language level specification for these. Otherwise, it is all what you are trying to do&#x2F;what problem you are solving. Excel (where I suspect 60% or more of this nonsense lives) at least has a clear definition (both are floats, so you can do the comparison). Otherwise, &quot;Down the Wat-hole&quot; for what to expect.
gregwtmtnoover 3 years ago
I never understood why programmers have such a hard time with time. There are two and only two pieces of information you need to work with time: seconds past the epoch and location. Everything else is applying legal and social context to the actual data.<p>Here both of those pieces of information are missing on all three questions, so no answers can be given. Any answer to these questions makes assumptions or deductions about those pieces of information.
评论 #30317302 未加载
评论 #30317312 未加载
评论 #30317385 未加载
评论 #30317314 未加载
deracover 3 years ago
My (naive) answer would be to localize any DateTime to account for any changes in Date due to timezone, strip the time and compare the Dates. I&#x27;d reckon a Date means any time in that date and is of lower specificity than a DateTime, rather than midnight on that Date. Is there any obvious issue with this in a situation where you might be (inadvisably) comparing a Date to a DateTime?
wodenokotoover 3 years ago
I was quick to say that a date equals a datetime at 0 hours and 0 minutes, but after reading and thinkiong about it, I agree that the question is wrong, but a little rephrasing can probably help solve the problem at hand:<p>- Does this datetime equal this date? No, no it doesn&#x27;t and never will.<p>- Does this datetime fall on this date? Yes, it might.<p>- Is this date, the date of this datetime? Yes, it might be.
cemaover 3 years ago
&quot;It depends&quot;<p>First, are we talking about what _is_ or what _should be_?<p>Second, what is the business logic behind the choice of Date versus Time to represent data (a data point or a segment) in the timeline? And is that logic consistent with the original design, and therefore with how the data have been collected?<p>There is more, of course. Lots more, some touched upon in the article and comments.
bjourneover 3 years ago
If I were to design a new programming language then x &gt; y or x == y when x is the float 1.2 and y the integer 1 would throw an exception. Mathematically that is wrong because obviously 1.2 &gt; 1, but in my experience, trying to compare values of different types leads to much pain and many annoying bugs.
happytoexplainover 3 years ago
Without more context, they are incomparable. The author only says they came from a UI and a database - but what do they represent? And <i>what</i> UI did they come from (i.e. what information did the user have when choosing the date[time] components, and how were those components encoded into a date[time]?). How are they being used <i>now</i>? I suspect the author is withholding some of these details to make a more interesting conversation (or to describe a &quot;general case&quot;, but unfortunately there is no such thing as a general case for date[time]s).
评论 #30316720 未加载
mdavidnover 3 years ago
This article doesn’t even delve into time zones. Each UTC timestamp maps to one of <i>two</i> dates, depending on the context of the user or the data.<p>This is important in financial systems. Transactions in Hawaii can legitimately land in a fiscal period that has already closed in New York.
Waterluvianover 3 years ago
Once I have a datetime safely in my database as UTC, I don’t need to record the original timezone, right?<p>(assuming I’m not planning to need to know the history of the database entry)
评论 #30316838 未加载
评论 #30316962 未加载
评论 #30317028 未加载
phendrenad2over 3 years ago
This seems like a Kobayashi Maru (a.k.a. a famous no-win situation test for budding starship captains in the Star Trek universe), but this isn&#x27;t a simulator, this is real life. You&#x27;ll never be in this situation. You&#x27;ll always have more information about the sources for these dates&#x2F;times. And that may give you enough context to make a decision, maybe not. But trying to solve the problem in the general sense is impossible.
评论 #30317209 未加载
shmerlover 3 years ago
Date without a time signifies a calendar day, you can&#x27;t compare it to a date with time. As simple as that.
kingcharlesover 3 years ago
When you hit problems like these, the easiest solution is often to set fire to your computer and move to Tibet.
评论 #30316530 未加载
anononymous5over 3 years ago
ok, my logic.<p>Dates are Intervals of Timestamp (I put uppercase for clarity).<p>A Date is [01&#x2F;06&#x2F;2020 0h0m0s..23h59mn59.999s]<p>A Time can be seen as an Interval of Timestamp, albeit of length 0<p>A Time is [01&#x2F;06&#x2F;2020 12:00:00.. 01&#x2F;06&#x2F;2020 12:00:00]<p>With these definitions, it&#x27;s<p>no (interval are different)<p>no [01&#x2F;06&#x2F;2020 0h0m0s..1h59mn59.999s] is not included<p>no the interval is fully included
wwilimover 3 years ago
.NET now has DateOnly. I haven&#x27;t had the opportunity to use it yet, but I have great hopes for it.
axiosgunnarover 3 years ago
&quot;Is Europe equal to France?&quot;
评论 #30317195 未加载
mdomsover 3 years ago
What is Kobayashi Maru? It&#x27;s mentioned in the headline and then never again in the article, and never defined.
评论 #30318850 未加载
faangiqover 3 years ago
Just create a new time zone.
dejjover 3 years ago
If you can compare apples with oranges, can you also compare oranges with apples?
unleashitover 3 years ago
Sorry, haven&#x27;t even read the article but I just love how the nerds are still preserving the memories and lessons of Star Trek II: The Wrath of Khan.
评论 #30316606 未加载