Note that there are some explanatory texts on larger screens.

plurals
  1. POIs TimeSpan unnecessary?
    text
    copied!<p><strong>EDIT 2009-Nov-04</strong></p> <p>OK, so it's been a little while since I first posted this question. It seems to me that many of the initial responders failed to really get what I was saying--a common response was some variation on "What you're saying doesn't make any sense"--and so I've made some handy diagrams to really illustrate my point.</p> <p>When we speak of numbers, we are generally referring to points on what grade school children learn is called the Number Line:</p> <p><img src="https://dl.getdropbox.com/u/674358/numberline.png" alt="The number line"></p> <p>Now, when we learn arithmetic, our minds learn to perform a very interesting transformation of this concept. Evalutating the expression <code>1 + 0.5</code>, for example, if we simply applied our "number line thinking", would require us to somehow make sense of this:</p> <p><img src="https://dl.getdropbox.com/u/674358/numberline_addition.png" alt="Adding two points on the number line"></p> <p>It's difficult to really illustrate that, because it's difficult to <em>think</em> about that: "adding" two points. This is where a lot of responders struggled with the idea of adding dates (or simply dismissed it as absurd), because they were thinking of dates as points.</p> <p>However, the expression <code>1 + 0.5</code> <em>does</em> make sense to us, because when we think of it, we're really imagining this:</p> <p><img src="https://dl.getdropbox.com/u/674358/numberline_vector_addition.png" alt="Adding a number (point) and a magnitude (vector)"></p> <p>That is, the <em>number</em> (or <strong>point</strong>) 1, plus the <strong><a href="http://en.wikipedia.org/wiki/Euclidean_vector" rel="nofollow noreferrer">vector</a></strong> 0.5, resulting in <strong>point</strong> 1.5.</p> <p>Alternately, we may be imagining this:</p> <p><img src="https://dl.getdropbox.com/u/674358/vector_vector_addition.png" alt="Adding two vectors"></p> <p>That is, the <strong>vector</strong> 1, plus the <strong>vector</strong> 0.5, resulting in the <strong>vector</strong> 1.5.</p> <p>In other words, when dealing with numbers, we treat points and vectors interchangeably. But what about dates? Dates are, after all, basically numbers. If you don't believe me, compare this line to the number line above:</p> <p><img src="https://dl.getdropbox.com/u/674358/timeline.png" alt="A timeline"></p> <p>Notice the correspondence between the timeline and the number line? This was my point: if we perform the transformation above with <em>numbers</em>, we ought to be able to do it with dates as well. So, applying "timeline thinking", the expression <code>0001-Jan-02 00:00:00 + 0001-Jan-01 12:00:00</code> doesn't make a lot of sense, as plenty of responders pointed out:</p> <p><img src="https://dl.getdropbox.com/u/674358/timeline_addition.png" alt="Adding two points on a timeline"></p> <p><strong>But</strong>, if we do the same conceptual transformation in our head that we perform <em>every time we add or subtract numbers</em>, we can easily "rethink" the above as this:</p> <p><img src="https://dl.getdropbox.com/u/674358/timeline_vector_addition.png" alt="Adding a point in time and a time vector"></p> <p>So clearly, the difference between a <code>DateTime</code> and a <code>TimeSpan</code> is the same difference that exists between a point and a vector. What I think caused a lot of people to respond negatively to my suggestion is that it just feels so unnatural to think of dates as magnitudes in this way. But I don't buy the argument that there's no obvious reference point to use as zero. <strong>There is an obvious reference point, and I'll give you a hint where it is: about 2010 years ago.</strong></p> <p>Don't get me wrong: I'm not questioning the <em>usefulness</em> of drawing a conceptual divide between the notion of a <code>DateTime</code> and a <code>TimeSpan</code>. Really, my question all along should have been (as ChrisW indirectly suggested), why <em>do</em> we treat numbers and vectors interchangeably when dealing with regular numeric types? (Or: why do we have just one <code>int</code> type, instead of <code>int</code> and <code>intspan</code>?) There's a big difference, and yet we don't ever really think about it until sometime in junior high or high school, when we begin geometry. And then it's treated as this new mathematical concept, when in reality it's something we've been utilizing ever since we learned to add numbers by counting with our fingers.</p> <p>In the end, the best answer came from Strilanc, who pointed out that the use of <code>DateTime</code> and <code>TimeSpan</code> is really an implementation of an <a href="http://en.wikipedia.org/wiki/Affine_space" rel="nofollow noreferrer">affine space</a>, which has the convenient property of not needing a reference point to treat as the origin. So thanks, Strilanc. I'm giving the accepted answer to ChrisW, however, for being the first one to bring up the concept of vectors and points, which really got to the crux of the matter.</p> <hr> <p><strong>ORIGINAL QUESTION (for posterity)</strong></p> <p>I am certainly no programming jack of all trades, but I know both PHP and .NET have a <code>TimeSpan</code> class in addition to a <code>DateTime</code> class (or structure in .NET), and I am guessing this is the case in a variety of other languages and frameworks as well (though I am writing this primarily with reference to the .NET structures). This might seem a strange question, but isn't <code>TimeSpan</code> redundant?</p> <p>In case you think the answer is obvious ("A <code>DateTime</code> is an absolute point in time, while a <code>TimeSpan</code> is a range of time -- simple as that!"), consider this: an integer can be conceptualized as either an absolute value (the point on the number line) or a distance <em>between</em> values--and we don't need two separate data types for these different conceptualizations. I can still write 5 + 6 without any ambiguity as to what I mean.</p> <p>As long as there is a consistent zero-point reference, it seems to me there should be no reason why one would need a <code>TimeSpan</code> object to perform arithmetic operations on <code>DateTime</code> objects, or to get the distance between them.</p> <p>What am I missing? Why can't the unique methods and properties of the <code>TimeSpan</code> structure simply be folded into <code>DateTime</code>?</p> <p>(Disclaimer: It isn't like I'm passionate about this or anything; I'm fine using <code>DateTime</code> and <code>TimeSpan</code> objects as they're intended all the time. I'm just asking a question.)</p> <p><strong>EDIT</strong>: Okay, over-simplified example to illustrate my point:</p> <p>Consider the equation 10 - 5 = 5. One could read this as "Start at 10 (value), move 5 to the left (span), and you end up at 5 (value)."</p> <p>Suppose, just to make things easy, we let January 1 1900 be point zero and we define <code>TimeSpan</code> objects in terms of days only.</p> <p>Then 10 - 5 = 5 could be understood, in <code>DateTime</code> terms, as January 11 1900 - January 6 1900 = January 6 1900. <strong>This is fine, because January 11 is just "10" by our definition and January 6 is "5"</strong>. The fact that we are viewing the 10 as a <em>value</em>, the first 5 as a <em>span</em>, and the last 5 as a <em>value</em> again is merely for our own conceptual benefit. My point is just this: that the only difference is in how you think of the number, not in what it actually is. This is why we don't have separate structures for, say, integer values and integer spans -- a plain old integer covers all our bases.</p> <p>Am I making any sense?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload