简体   繁体   English

iPad Retina模拟器为NSDateFormatter提供了不同于其他所有输出的输出

[英]iPad Retina Simulator Gives Different Output Than All Others for NSDateFormatter

I've run this exact same code on all the other iPad simulators & iPhone and it works fine. 我已经在所有其他iPad模拟器和iPhone上运行了完全相同的代码,并且工作正常。 However, when I run on the iPad Retina emulator, it gives a different response 但是,当我在iPad Retina模拟器上运行时,它会给出不同的响应

               NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
               [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss.SSS"];
               NSDate *dateFromString = [[NSDate alloc] init];
               NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
               [dateFormatter setTimeZone:timeZone];
               dateFromString = [dateFormatter dateFromString:savedTime];
               NSTimeInterval timeInMiliseconds = [dateFromString timeIntervalSince1970]*1000;
               NSInteger time = timeInMiliseconds;

               NSString *strTimeStamp = [NSString stringWithFormat:@"%ld",(long)time];
               NSLog(@"The Date is = %@",savedTime);
               NSLog(@"The Timestamp is = %@",strTimeStamp);

This then returns: The Date is = 2016/03/03 20:55:39.613 The Timestamp is = -2147483648 然后返回:日期为= 2016/03/03 20:55:39.613时间戳为= -2147483648

Where as if this were on any other simulator it would be correct and return: The Date is = 2016/03/03 20:55:39.613 The Timestamp is = 1457038539613 好像在其他任何模拟器上一样,它将正确并返回:日期为= 2016/03/03 20:55:39.613时间戳为= 1457038539613

NSinteger isn't big enough to hold your value on 32-bit systems. NSinteger不足以在32位系统上保持您的价值。 Use a data type that will hold 64-bits regardless of platform. 无论平台如何,都使用可容纳64位的数据类型。 Or just use the NSTimeInterval . 或者只是使用NSTimeInterval

NSString *strTimeStamp = [NSString stringWithFormat:@"%.0f", timeInMilliseconds];

On a 32 bit device, NSInteger is 32 bits, and you can see that 1457038539613 doesn't fit into 32 bits. 在32位设备上,NSInteger是32位,您会看到1457038539613不适合32位。

I would really, really recommend that you don't try to convert NSTimeInterval into some other format. 我真的,真的建议您不要尝试将NSTimeInterval转换为其他格式。 Just leave NSTimeInterval as a double counting seconds. 只需将NSTimeInterval保留为重复计数秒即可。 Using the type NSTimeInterval for a number that means milliseconds is close to criminal. 使用类型为NSTimeInterval的数字表示毫秒接近犯罪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM