Hacker News new | ask | show | jobs
by lukev 1590 days 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 "category" or "type" error (which is correct, but not particularly informative.)

The issue here is around the semantics of the mathematical operators. It isn't even really about the types to which they apply; there are systems where `=` is well defined on heterogenous types.

The reason the answer to all these questions is a clear "no" 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`.

So, no, the OP's conclusion "Literally we’re all just making it up" is incorrect. You cannot use the operators `=`, `<`, and `>` between dates and times because they do not satisfy the core properties that define 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 that not leading to massive confusion.)

Where you can just make it up is to define new operators as you actually want them to be. It's not `=`, it's `myDateTime=()` and then you're free to write the definition of that yourself. As long as you're consistent in the UI of how you present it (don't pretend it'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't be feeding any more to the fires of confusion, which you will as long as you keep pretending it's possible to make `=` mean something that's not reflexive.

1 comments

I'm not sure I follow.

If I define a date MM/DD/YYYY as equal to the time MM/DD/YYYY 00:00 GMT, which of the equality properties am I lacking?

Similarly, if I use that definition of equality to convert the date into a time, and then compare it, can't I get '>' and '<'?

> If I define a date MM/DD/YYYY as equal to the time MM/DD/YYYY 00:00 GMT

That's the exact problem - if you contrived a date into an int then yes, it is compatible with the =, >, and < operators.

The point of the blog post is to assert that that contrivance is not appropriate in nearly all use cases, despite how popular it seems to be. In the vast majority of use cases contriving MM/DD/YYYY into MM/DD/YYYY 00:00 GMT is not reasonable and can mask a vast amount of unintended behavior.

For example, a person in California who enters a date of 06/05/2022 in reference to their local time zone will suddenly wind up the day before - because 06/05/2022 00:00 GMT is actually 06/04/2022 16:00 PST.

If the developer wants to contrive the date into a "midnight instant", they are welcome to do so and there should be plenty of convenience functions to allow them to do that, but implicitly performing that contrivance is dangerous.

Wouldn't 06/05/2022 00:00 GMT be 05/05/2022 16:00 PST due to the DD/MM (GMT) MM/DD (PST) notation swap
Well it depends on if you have a type system that distinguishes between "date" types and "instant" types.

If you only have instants, then sure, you could do what you're saying, but the context in which the question was poised seems to imply that "dates" and "times" are separate.

And if you do have separate "date" and "instant" types, you lose the property of substitution: f(x) = f(y) if x = y for any arbitrary function f.

Sometimes the mapping is context specific...

For example non-mathematicians/programmers will specify date range as 2022-01-01 to 2022-01-31.

to map this you need a special mapping to convert to instant... (you need to map the upper range to 2022-02-01 (00:00), anything else is probably wrong).