TimeSpan是不必要的吗? [英] Is TimeSpan unnecessary?

查看:146
本文介绍了TimeSpan是不必要的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑2009年11月 - 04日



好的,因为我第一次发布这个问题已经有一段时间了。在我看来,许多初步的回应者没有真正得到我所说的话 - 一个常见的回应是你在说什么没有任何意义的一些变化 - 所以我已经做了一些方便图表真的说明了我的观点。



当我们说出数字时,我们一般指的是什么等级的学校学生被称为数字线:





现在,当我们学习算术时,我们的心灵学会对这个概念进行一个非常有趣的转换。例如,如果我们简单地应用了我们的数字思维,就要求我们以某种方式理解这一点:

$ b(例如, 1 + 0.5
$ b



很难真正说明这一点,因为很难想到这一点:添加两点。这是很多响应者在添加日期的想法(或者简单地将其解释为荒谬的),因为他们正在考虑日期作为积分。



然而,表达 1 + 0.5 对我们有意义,因为当我们想到这一点,我们真的想象了:





也就是说,(或)1,加上 ,它具有方便的属性,不需要参考点作为起源。所以谢谢Strilanc。然而,我正在给ChrisW的接受答案,因为第一个提出了向量和积分的概念,这真的是问题的关键。



< hr>

ORIGINAL QUESTION(用于后代)



我当然没有所有交易的编程插孔,但是我知道PHP和.NET都有一个 TimeSpan 类,除了一个 DateTime 类(或。 NET),我猜这是在各种其他语言和框架的情况(尽管我主要是参考.NET结构)。这可能是一个奇怪的问题,但不是 TimeSpan 冗余?



如果你认为答案很明显(A DateTime 是一个绝对时间点,而 TimeSpan 是一个时间范围 - 简单的!),考虑一下:整数可以被概念化为绝对值(数字行上的点)或值之间的距离,我们不需要两个单独的数据类型这些不同的概念。我仍然可以写5 + 6而不意味着什么意思。



只要有一致的零点参考,在我看来应该是没有理由为什么需要一个 TimeSpan 对象来执行 DateTime 对象的算术运算,或者获取它们之间的距离



我缺少什么?为什么不能将 TimeSpan 结构的独特方法和属性折叠成 DateTime



(免责声明:这不是我对这件事或任何事情充满激情,我很好使用 DateTime TimeSpan 对象,因为它们是所有的时间,我只是问一个问题。)



编辑:好的,过度简化的例子来说明我的观点:



考虑公式10 - 5 = 5.可以将其读为从10开始),向左移动5(跨度),最终达到5(值)。



假设只是为了让事情变得容易,我们可以在1900年1月1日为零,我们以天为单位定义 TimeSpan 对象。



然后10 - 5 = 5可以1900年1月11日1900年1月6日1900年1月6日, DateTime 条款。这很好,因为1月11日我们的定义只是10 1月6日是5。事实上,我们将10作为,前5个作为 ,最后5个作为再次只是为了我们自己的概念利益。我的观点就是这样:唯一的区别是你对数字的看法,而不是它实际上是什么。这就是为什么我们没有单独的结构,例如整数值和整数跨度 - 一个普通的旧整数涵盖我们所有的基础。



我有任何意义?

解决方案


考虑这一点:一个整数可以被概念化为一个绝对值数字行)或值之间的距离


根据你的逻辑,不是TimeSpan是不必要的:而是DateTime是不必要的,并且可以被TimeSpan(从零开始的持续时间)替换。



另外还有一个事实,整数有一个明显的零,而日期却没有明显的零;但是如果要用零/原点的距离/跨度替换数字行上的位置,则需要明显的零。






编辑:



一个点(飞机上的位置)与矢量不一样。



他们似乎相似...




  • 矢量(距原点的距离)可以表示一个点

  • 一个点(相对于原点)可以表示向量



...但是如果原点改变,则需要代表给定点的向量将会改变。



添加两个(相对)向量总是有意义的;但是,除了将这些点转换为向量然后添加向量之外,添加两个点是没有意义的。



两个向量的和不受更改的影响原点,但如果通过将它们转换为向量并添加向量来将它们相加,则两点的和将受到原点的更改的影响(因为更改原点将影响这些向量的值)。



[在上面的参数中用TimeSpan替换'point'和'vector'。]



我认为有一个真正的绝对值和相对值之间的差异。我不知道为什么这个差异在算术上不是更明显,也就是说,为什么'数字'看起来可以互换地表示绝对值和相对值。


EDIT 2009-Nov-04

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.

When we speak of numbers, we are generally referring to points on what grade school children learn is called the Number Line:

Now, when we learn arithmetic, our minds learn to perform a very interesting transformation of this concept. Evalutating the expression 1 + 0.5, for example, if we simply applied our "number line thinking", would require us to somehow make sense of this:

It's difficult to really illustrate that, because it's difficult to think 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.

However, the expression 1 + 0.5 does make sense to us, because when we think of it, we're really imagining this:

That is, the number (or point) 1, plus the vector 0.5, resulting in point 1.5.

Alternately, we may be imagining this:

That is, the vector 1, plus the vector 0.5, resulting in the vector 1.5.

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:

Notice the correspondence between the timeline and the number line? This was my point: if we perform the transformation above with numbers, we ought to be able to do it with dates as well. So, applying "timeline thinking", the expression 0001-Jan-02 00:00:00 + 0001-Jan-01 12:00:00 doesn't make a lot of sense, as plenty of responders pointed out:

But, if we do the same conceptual transformation in our head that we perform every time we add or subtract numbers, we can easily "rethink" the above as this:

So clearly, the difference between a DateTime and a TimeSpan 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. There is an obvious reference point, and I'll give you a hint where it is: about 2010 years ago.

Don't get me wrong: I'm not questioning the usefulness of drawing a conceptual divide between the notion of a DateTime and a TimeSpan. Really, my question all along should have been (as ChrisW indirectly suggested), why do we treat numbers and vectors interchangeably when dealing with regular numeric types? (Or: why do we have just one int type, instead of int and intspan?) 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.

In the end, the best answer came from Strilanc, who pointed out that the use of DateTime and TimeSpan is really an implementation of an affine space, 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.


ORIGINAL QUESTION (for posterity)

I am certainly no programming jack of all trades, but I know both PHP and .NET have a TimeSpan class in addition to a DateTime 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 TimeSpan redundant?

In case you think the answer is obvious ("A DateTime is an absolute point in time, while a TimeSpan 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 between 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.

As long as there is a consistent zero-point reference, it seems to me there should be no reason why one would need a TimeSpan object to perform arithmetic operations on DateTime objects, or to get the distance between them.

What am I missing? Why can't the unique methods and properties of the TimeSpan structure simply be folded into DateTime?

(Disclaimer: It isn't like I'm passionate about this or anything; I'm fine using DateTime and TimeSpan objects as they're intended all the time. I'm just asking a question.)

EDIT: Okay, over-simplified example to illustrate my point:

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)."

Suppose, just to make things easy, we let January 1 1900 be point zero and we define TimeSpan objects in terms of days only.

Then 10 - 5 = 5 could be understood, in DateTime terms, as January 11 1900 - January 6 1900 = January 6 1900. This is fine, because January 11 is just "10" by our definition and January 6 is "5". The fact that we are viewing the 10 as a value, the first 5 as a span, and the last 5 as a value 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.

Am I making any sense?

解决方案

consider this: an integer can be conceptualized as either an absolute value (the point on the number line) or a distance between values

By your logic, it isn't TimeSpan that's unecessary: rather it's DateTime that's unnecessary, and could be replaced by TimeSpan (duration since zero).

Plus there's the fact that integers have an obvious zero, whereas Dates however don't have an obvious zero; but having an obvious zero is necessary, if you want to replace "place on the number line" with "distance/span from the zero/origin".


Edit:

A point (location on a plane) isn't the same as a vector.

They seem similar ...

  • A vector (distance from origin) can represent a point
  • A point (relative to the origin) can represent a vector

... however the value of the vector that's required to represent a given point will change if the origin changes.

It always makes sense to add two (relative) vectors; but, it makes no sense to add two points, except by converting those points to vectors and then adding the vectors.

The sum of two vectors is unaffected by a change in the origin, but the sum of two points would be affected by a change in the origin if you summed them by converting them to vectors and adding the vectors (because changing the origin would affect the values of those vectors).

[Replace 'point' with DateTime and 'vector' with TimeSpan in the argument above.]

I think there is a genuine difference between absolute and relative values. I'm don't know why that difference isn't more apparent in arithmetic, i.e. why 'numbers' are used seemingly interchangeably to represent both absolute and relative values.

这篇关于TimeSpan是不必要的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆