NSDate dateWithTimeIntervalSince1970 不返回 GMT/UTC 时间 [英] NSDate dateWithTimeIntervalSince1970 NOT returning GMT/UTC time

查看:110
本文介绍了NSDate dateWithTimeIntervalSince1970 不返回 GMT/UTC 时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一个实验,因为我需要能够在我正在开发的应用程序中生成一个 unix 时间戳(自 1970 年以来).

I tried an experiment because I need to be able to generate a unix timestamp (since 1970) in the app I am working on.

NSLog(@"Getting timeIntervalSince1970");
    double theLoggedInTokenTimestampDateEpochSeconds = [[NSDate date] timeIntervalSince1970];

应该以格林威治标准时间(自 1970 年 1 月 1 日以来的秒数)返回纪元秒数(自 1970 年以来).但是,在 Mon Aug 15 09:54:30 2011 进行实验时,它返回了 1313427270.504315

This should've returned epoch seconds (since 1970) in GMT (Seconds since Jan 1, 1970). However, when conducting the experiment at at Mon Aug 15 09:54:30 2011, it returned 1313427270.504315

在我的 Mac OS 终端上用一个简单的 perl one-liner 测试这个,我得到:perl -e 'print scalar(localtime(1313427270))' 返回 Mon Aug 15 09:54:30 2011 ...

Testing this with a simple perl one-liner on my Mac OS Terminal, I get: perl -e 'print scalar(localtime(1313427270))' which returns Mon Aug 15 09:54:30 2011 ...

当我在旧金山湾区并且我的本地时区设置为库比蒂诺"时,这显然不是格林威治标准时间.请问这是怎么回事,我该如何解决?我需要让我的应用在通信时将 UTC 时间发送到服务器,这样无论用户在哪里,时间戳都会在一个相等的有效时区中发送.

This is obviously not GMT time when I am in the SF Bay Area and my local timezone is set to "Cupertino". What is going on and how do I fix it please? I need to have my app send UTC time to the server when it communicates so wherever the user is time timestamp would be sent in one equal, valid time zone.

在我的应用程序的另一部分中,当用户从服务器请求数据时,它会以 UTC 格式发送数据——将其转换为如下所示:

In another part of my app, when the user requests data from the server, it gets it sent in UTC -- converting it to be displayed as follows:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setTimeZone:nil];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSDate *conversationDate = [NSDate dateWithTimeIntervalSince1970:[theConversationTimeStampString intValue]];
    NSString *conversationDateString = [dateFormatter stringFromDate:conversationDate];
    [dateFormatter release];

这很好用——在用户的时区和语言环境中显示更正的时间......所以我知道它正在为传入的时间戳完成.那么,第一个阻止它出现在 GMT 中的函数 (timeIntervalSince190) 有什么问题?

and this works beautifully -- displaying the corrected time in the user's timezone and locale... so I know it is being done for incoming timestamps. So, what am I doing wrong with the first function (timeIntervalSince190) that stops it from being in GMT?

谢谢

推荐答案

我不认为第一个函数实际上是错误的,尽管它表面上看起来可能是这样.您从 timeIntervalSince1970 接收的时间间隔 NOT 返回格林威治标准时间的时间间隔.相反,它返回从现在到 1970 年 1 月 1 日 00:00:00 GMT 之间的时间间隔.这可能看起来像是吹毛求疵,但很重要:间隔值只不过是自固定时间点以来的标量秒数.间隔本身不是格林威治标准时间,只有它的固定参考点是.

I don't think the first function is actually wrong, although it may look like it on the surface. The time interval you are receiving from timeIntervalSince1970 is NOT returning the time interval in GMT. Rather, it is returning the time interval between now and January 1, 1970 00:00:00 GMT. That might seem like a nitpick, but it is important: The interval values are nothing more than a scalar number of seconds since a fixed point in time. The interval itself is not in GMT, only its fixed reference point is.

我不知道 perl,但我快速搜索了本地时间的文档,它似乎需要任何时间并打印将标准日期类型转换为本地时间.这意味着您描述固定时间点的时间间隔将转换回该点的本地时间.当您从命令行显示它时,您将再次获得本地时间.因此,将绝对时间转换为您的当地时间是我希望看到的.

I don't know perl, but I did a quick search for documentation on local time and it appears to take any time and print convert a standard date type into local time. That means that your time interval describing a fixed point in time is converted back into your local time at that point. When you display it from your command line, you are getting the local time again. So seeing that absolute time translated to your local time is what I would expect to see.

根据您的服务期望接收 UTC 时间的准确程度,您的时间间隔值可能会正常工作.您是否有证据表明它不是基于终端检查以外的其他内容?

Depending on how exactly your service expects to receive UTC time, your time interval value is likely to be working just fine. Do you have evidence that it is not based on something other than your terminal check?

这篇关于NSDate dateWithTimeIntervalSince1970 不返回 GMT/UTC 时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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