iOS中 timestamp、timeInterval 的故事

文章出处,原创于 http://HawkingOuYang.com/

我的GitHub


1
2
3
/* HTmAccountEntity.h */
@interface HTmAccountEntity : BaseManagedObject

因为:NSDate 是 相对于 01.01.2001 00:00:00 GMT 的 时间差值(即 NSTimeInterval, 类型 double)

所以:

1
2
//! 视频通话SDK的信令登入,即 lecall的Login,成功登入的时间戳。(@integerValue)
@property (nonatomic, strong) NSNumber *lecallLoginTimestamp;

改成

1
2
//! 视频通话SDK的信令登入,即 lecall的Login,成功登入的时间,在数据库中的值,形如 499238502.374417
@property (nonatomic, strong) NSDate *lecallLoginTimestamp;
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
成功登录的时间,在数据库中的值,形如 499238502.374417
因为:NSDate 是 相对于 01.01.2001 00:00:00 GMT 的 时间差值(即 NSTimeInterval, 类型 double)
*/
@property (nonatomic, strong) NSDate *loginTime;
/*
视频通话SDK的信令登入,即 lecall的Login,成功登入的时间,在数据库中的值,形如 499238502.374417
因为:NSDate 是 相对于 01.01.2001 00:00:00 GMT 的 时间差值(即 NSTimeInterval, 类型 double)
*/
@property (nonatomic, strong) NSDate *lecallLoginTimestamp;

为什么呢?


注意: NSDate.h 文件中,

1
2
3
2001.1.1 00:00 UTC
#define NSTimeIntervalSince1970 978307200.0
499238502.374417

978307200.0 刚好是 2001-1970=30年,
499238502.374417 大约为 2016-2001=15年。

1
2
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;

简短地说:

iOS中的NSDate对象,本质就是timeInterval(时间差),和我们平时说的timestamp(时间戳)不同的是,NSDate选取的起点是2001.1.1 00:00 UTC

有此为证:


GMT UTC
除了定义经度,格林尼治子午线亦曾被用作时间的标准。理论上来说,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时的时间。然而因为地球自转速度并不规则,现在的标准时间已由协调世界时取代。
https://zh.wikipedia.org/wiki/%E6%9C%AC%E5%88%9D%E5%AD%90%E5%8D%88%E7%B7%9A

Why is NSTimeIntervalSince1970 defined as a fixed double?

#define NSTimeIntervalSince1970 978307200.0

It seems to be the UNIX timestamp of 01.01.2001 00:00:00 GMT.
What is it for and why is it a fixed number?

It’s because NSDate’s timeIntervalSinceReferenceDate uses 1 January 2001, GMT as its reference date while other places typically use 1970 (the “Unix epoch”).
It is useful if you want to compare the current time with time stored as an interval since the earlier reference date.
http://stackoverflow.com/questions/22781535/why-is-nstimeintervalsince1970-defined-as-a-fixed-double

The Difference Between GMT and UTC
https://www.timeanddate.com/time/gmt-utc-time.html

Greenwich Mean Time (GMT) is often interchanged or confused with Coordinated Universal Time (UTC). But GMT is a time zone and UTC is a time standard.

Although GMT and UTC share the same current time in practice, there is a basic difference between the two:

  • GMT is a time zone officially used in some European and African countries. The time can be displayed using both the 24-hour format (0 - 24) or the 12-hour format (1 - 12 am/pm).
  • UTC is not a time zone, but a time standard that is the basis for civil time and time zones worldwide. This means that no country or territory officially uses UTC as a local time.

UTC, GMT and Daylight Saving Time
Neither UTC nor GMT ever change for Daylight Saving Time (DST). However, some of the countries that use GMT switch to different time zones during their DST period.
For example, the United Kingdom is not on GMT all year, it uses British Summer Time (BST), which is one hour ahead of GMT, during the summer months.

更有意思的图片,可以得知: