来自NSDate的意外值 [英] Unexpected value from NSDate

查看:114
本文介绍了来自NSDate的意外值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在约会中存储时间。
在这种情况下1.5秒。
我将时间存储在这样的日期:

I'm trying to store a time in a date. In this case 1.5 seconds. I store the time in the date like this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.SS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];  
NSDate *date1 = [dateFormatter dateFromString:@"00:01.50"];

但如果我查看调试区域,我可以看到date1的值设置为:

But if I look in the debug area I can see that the value of date1 was set to:

date1 = (NSDate *) 0x1dd32b30 2000-01-01 01:00:01 CET

有人能告诉我为什么会这样吗?

Could anyone tell me why this is happening?

推荐答案

因为在SO上存在大量与NSDate相关的问题并且它们都具有相同的阴影,我会给你一个更长的答案,可能这可以从其他问题中提及。

Because there is a mass of question related to NSDate on SO and they all have the same shade, I will give you a longer answer, probably this can be referred from other questions.

A。有一个常见的误解,日期是什么:日期是时间轴上的一个点。它处理小时或分钟,它处理日,月,年,它处理时区。

A. There is a common misunderstanding, what a date is: A date is a point on timeline. It does not deal with hours or minutes, it does not deal with days, month, years, it does not deal with timezones.

这是时间轴上的一个点。期间。

(在内部它由一个浮点数表示。这是一个惊喜吗?)

(And internally it is represented by an floating-point number. Is that a surprise?)

B。还有另一个常见的误解,即日期是什么:如果具有相同的值,则两个日期(因此时间轴上的点)相等。这意味着它们具有相同的表示。两次可以相等,即使它们的表现完全不同。 (这就是,发生在你身上的事情。)为了进行比较,无关紧要,使用哪个时区,预期,无论如何。对于比较并不重要,哪个日历正在使用,预期或$ what。如果在德国科隆是11:11(UTC + 1),则在英国伦敦(UTC + 0)为10:11。这是相同的时间。

B. There is another common misunderstanding, what a date is: Two dates, therefore points on a timeline, are equal, if the have the same value. This does not mean, that they have the same representation. Two times can be equal, even their representation is completly different. (This is, what happened to you.) For a comparison it does not matter, which timezone is in use, expected, $whatever. For a comparison is does not matter, which calendar is in use, expected or $whatever. If it is 11:11 in Cologne, Germany (UTC+1) it is 10:11 in London, England (UTC+0). This is the same time.

如果日期在另一个日期之前的时间轴上,则日期更早。

℃。那么天,月,时区等有趣的东西是什么?
由于在一个星际迷航剧集之外的时间如26,182,382,303.127373将难以理解和处理,人类开始在时间线上给出点有趣的名字。这种陈述不是日期。它们是日期的代表。数字也是同样的问题。你没有这个笑话吗?

C. So what are these funny things like days, month, timezones and so on? Since outside a star trek episode a time like 26,182,382,303.127373 would be difficult to understand and to handle, human beings began to give points on the timeline funny names. This representations are not the date. They are representations of the date. It is the same problem with numbers. Do you no this joke?

有10种人:那些懂二进制数的人和那些不懂的人。

"There are 10 kind of people: Those, who understand binary numbers and those who doesn't."

这个笑话的精神是,10可以是10的值,如果它是用十进制表示法写的,或者它可以是2的值,用二进制表示法写成。除非他知道用于表示的数字系统,否则没有人可以说,10表示什么值。大多数人认为它是十进制系统,所以10表示十。这是一个受欢迎的会议。这很好用,因为几乎全世界都采用了这种惯例。但是:10可以表示两个不同的值。反之亦然:相同的值可以使用不同的字符串写入:10十进制表示法等于 1010二进制表示法(并且等于2 + 8,sqrt (100),...也就是说,为什么允许它们之间写=。)

The spirit of the joke is, that 10 can be the value of ten, if it is written in decimal notation or it can be the value of two, written in binary notation. Nobody can say, what value "10" denotes unless he knows the numeral system which is used for the representation. Most people assume, that it is in a decimal system, so "10" means ten. This is a popular convention. And this works fine since nearly all over the world adopted that convention. But: "10" can denote two different values. And the other way around, too: The same value can be written with different "strings": 10 in decimal notation is equal to 1010 in binary notation (and is equal to "2+8", sqrt(100), … That is, why it is allowed to write = between them.)

不幸的是(不,不是真的,时间必须以简单的方式在本地工作)时间没有世界范围的惯例。如果有人写11:11你根本不能说,什么时候(记住,它是时间轴上的一个点)是指。可能这个人含蓄地说,这是他在他的时区的日历系统中的时间。但是你必须知道,他说的是世界上哪个位置。考虑到日,月等等,你可能必须知道,他的宗教是什么,因为即使在一个国家,不同宗教的人也会使用不同的日历。

Unfortunaly (no, not really, time has to work locally in an easy way) for time there is no world-wide convention. If somebody writes "11:11" you simply cannot say, what time (remember, it is a point on the timeline) is meant. Probably the person implicitly says, that this is the time in his calendaric system in his timezone. But then you have to know, at which location on the world he is saying that. Taking day, month and so on in account, you probably have to know, what is his religion, because even in one country people of different religions uses different calendars.

一方面没有共同的全球惯例,Mac OS正在世界各地的计算机系统上运行,你无法想象,打印出来的时间是用你期望的符号写的。

Since there is no common world-wide convention on one hand and Mac OS is running on computer systems world-wide, you cannot assume, that a time printed out is written in a notation you expect.

因此,通过字符串比较日期是完全的,呃,不是很聪明。您不比较日期,比较字符串。

Therefore it is completly, eh, not very intelligent to compare to dates by string. You do not compare dates, you compare strings.

在比较时间(包括日期)之前,您必须将表示转换为实际时间的时间。您必须添加有关日历的信息,写入的表示和时区(包含在Cocoa的日历中)。

Before you compare a time (including date) you have to transform the representation to a time to the "real" time. You have to add information about the calendar, the representation is written in and the timezone (which is included in the calendar in Cocoa).

在Cocoa中,您使用NSDateFormatter的实例,NSCalendar,NSDateComponents来做这个工作。如果让NSDateFormatter将表示转换为时间,反之亦然,则必须设置日历。 (NSDateFormatter假设,如果你没有设置本地日历,则选择本地日历。)如果NSDateFormatter不知道要使用哪个日历,那么它根本不可能进行任何转换工作。对你来说根本不可能。

In Cocoa you use instances of NSDateFormatter, NSCalendar, NSDateComponents to do this job. If you let NSDateFormatter transform a representation to a time and vice versa, you have to set the calendar. (NSDateFormatter assumes, that the local calendar is choosen, if you do not set one.) It is simply impossible to NSDateFormatter to do any transformation work, if it does not know which calendar to use. And it is simply impossible for you.

然后你可以用它来做一些计算,日期,时间,NSDate的实例,包括比较,至少你有使用日历将其转换为人类可读的表示。

Then you can do some calculations with it, the date, the time, the instance of NSDate, including comparison and at least you have to transform it back into a human-readable representation using a calendar.

你得到的并让你想到,这是一个错误的时间,只是正确的时间但是在你没想到的代表中。 (-description总是提供UTC + 0)。

What you get and let you think, that it is a wrong time, is simply the correct time but in a represenatation you did not expect. (-description always delivers UTC+0).

这篇关于来自NSDate的意外值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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