iOS - 在 mySQL (apache) 和 iOS (NSDate) 之间正确格式化时间戳 [英] iOS - Proper formatting of timestamps between mySQL (apache) and iOS (NSDate)

查看:50
本文介绍了iOS - 在 mySQL (apache) 和 iOS (NSDate) 之间正确格式化时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将时间戳 (yyyy-MM-dd HH:mm:ss) 从 mySQL(在 apache 上运行)传递到 iOS.目前,mysql 设置为此时区:

I am trying to pass a timestamp (yyyy-MM-dd HH:mm:ss) from mySQL (running on apache) to iOS. Currently, mysql is set to this timezone:

default-time-zone='+00:00'

当我将此时间戳传递给 iOS 时,我使用此代码将字符串转换为 NSDate:

When I pass this timestamp down to iOS, I use this code to convert the string to an NSDate:

-(NSDate *)formatDateWithString:(NSString *)dateString{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSDate* date = [dateFormatter dateFromString:dateString];
     NSLog(@"Formatted Timestamp: %@ - Original Timestamp: %@", date,dateString);
     return date;
}

这是 2013-04-17 16:33:56 的 mySQL 时间戳的输出:

This is the output for a mySQL timestamp of 2013-04-17 16:33:56:

Formatted Timestamp: 2013-04-17 23:33:56 +0000 - Original Timestamp: 2013-04-17 16:33:56

为什么 iOS 增加了 7 个小时?(仅供参考 - 我位于加利福尼亚州旧金山,所以我确定这与我的时区是 PDT 有关.只是不确定为什么当我没有指定它时它会被转换成那样).

Why is iOS adding 7 hours? (FYI - I am located in San Francisco, Ca so I am sure it has something to do with my timezone being PDT. Just not sure why it is being converted that way when I don't specify it to be).

在我的应用中尽可能使用最通用"的时间戳很重要,因为我的用户可能遍布世界各地,并且不想为大量转换大惊小怪.我希望能够在 mySQL 中存储时间戳,然后只比较存储的服务器时间戳和当前服务器时间戳之间的差异.如果可能的话,我宁愿不必在每次需要进行比较时都使用 Web 请求来获取服务器时间".

It is important to use the most "universal" timestamp possible in my app as I may have users all over the world and don't want to fuss with a lot of conversions. I want to be able to store the timestamps in mySQL, then just compare the differences between the stored server timestamp and the current server timestamp. I would prefer to not have to use a web request to get the "server time" everytime I need to do a comparison if possible.

更新

通过将这行代码添加到我的 dateFormatter 中,这两个时间现在似乎正确匹配了:

By adding this line of code to my dateFormatter, it seems that the two times are now matching correctly:

[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

但是,任何人都可以验证它始终会继续匹配,而不管一年中的什么时候?(即夏令时等)

However, can anyone verify that it will always continue to match, regardless of the time of year? (i.e. Daylight Savings time, etc.)

推荐答案

NSDateFormatter 默认设置为 localTimeZone.由于您的 dateString 没有时区信息,因此它计算了与 localTimeZone 的时间.如果您知道 dateString 的时区是 UTC,那么您需要将其设置为 dateFormatter.

By default NSDateFormatter is set to localTimeZone. Since your dateString didn't had timezone information, it calculated the time wrt to localTimeZone. If you know that the timezone of your dateString is UTC then you need to set that to dateFormatter.

NSDate 表示绝对时间,表示可以与 timeZones 不同,但它是一个唯一值,不会受到夏令时的影响.

NSDate represents absolute time, the representation can vary wrt timeZones but it is a unique value and it will not be affected by daylight savings.

-(NSDate *)formatDateWithString:(NSString *)dateString{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
     NSDate* date = [dateFormatter dateFromString:dateString];
     NSLog(@"Formatted Timestamp: %@ - Original Timestamp: %@", date,dateString);
     return date;
}

这篇关于iOS - 在 mySQL (apache) 和 iOS (NSDate) 之间正确格式化时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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