我正在比较两个文件(一个本地文件和一个 Amazon S3 服务器上的文件)的上次修改日期。我正在使用 AWS IOS SDK 框架,可以成功请求并接收来自 S3 服务器的响应,但我无法理解返回的 s3 日期的格式。
在我的本地计算机上,lastModified 的日期格式为“2011-07-21 18:43:15 -0400”,而对于驻留在 S3 服务器上的文件,其日期格式为“2011-10-15T16:25:49.000Z”。
我的本地信息是使用以下方式获取的:
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *attr = [fm attributesOfItemAtPath:filePath error:nil];
NSDate *localDate = [attr objectForKey:NSFileModificationDate];
而我的 S3 信息是使用以下方式获取的
for (S3ObjectSummary *object in [listObjectsResult objectSummaries]) {
NSDate *s3date = [object lastModified];
}
有谁知道我是否可以将 S3 文件的日期转换为可用于比较这两个日期的格式:
NSTimeInterval deltaSeconds = [s3Date timeIntervalSinceDate: localDate];
或者我在这里做错了什么?现在我的程序崩溃了,
[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x200351360.
可能是因为 s3 日期格式格式不正确。我对使用 AWS S3 SDK 还很陌生,因此非常感谢所有帮助。如果有人还知道这个框架的一些很好的教程(除了它附带的演示代码),那就太好了。干杯,特隆德
I am comparing the date when a file was last modified for two files, one local and one on Amazon S3 server. I am using the AWS IOS SDK framework and can successfully request and receive response from the S3 server but I have trouble understanding the format of the returned s3 date.
On my local machine the date format for lastModified is "2011-07-21 18:43:15 -0400" while for the file residing on the S3 server it is "2011-10-15T16:25:49.000Z".
My local info is obtained using:
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *attr = [fm attributesOfItemAtPath:filePath error:nil];
NSDate *localDate = [attr objectForKey:NSFileModificationDate];
while my S3 info is obtained using
for (S3ObjectSummary *object in [listObjectsResult objectSummaries]) {
NSDate *s3date = [object lastModified];
}
Does anyone know if I can convert the date for the S3 file to a format that I can use to compare these two dates using:
NSTimeInterval deltaSeconds = [s3Date timeIntervalSinceDate: localDate];
or am I doing something wrong here? Right now my program crashes with
[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x200351360.
probably because the s3 date format is not in proper format. I am quite new to using the AWS S3 SDK so all help is greatly appreciated. If anyone also knows of some good tutorials for this framework (apart from the demo code that comes with it), that would be great. Cheers, Trond
发布评论
评论(3)
在我看来,
[object lastModified]
只是返回一个NSString
而不是NSDate
对象,如 文档。在这种情况下,可以使用
NSDateFormatter
从字符串创建NSDate
对象:日期格式指南 有很多方便的示例。您可能需要稍微调整格式字符串,因为我还没有测试过它。
It looks to me as
[object lastModified]
simply returns aNSString
and not anNSDate
object, as stated in the documentation.NSDateFormatter
can be used in this case to create aNSDate
object from the string:The Date Formatting guide has lots of handy examples. You may need to tweak the format string slightly as i have not tested it.
给定的答案有一些问题 - 似乎有某种重复的“ss”+“mm”内容,导致字符串无法正确翻译。此外,由于字符串中的时区以 UTC 形式给出,因此您必须为格式化程序设置时区。这是我使用的代码(对我有用),我将其转换为一个类别,并且默认使用之前提供的字符串,以防适合我的代码不适合您:
Had some trouble with the given answer - seems that there is some sort of duplicated 'ss'+'mm' stuff in there that prevents the string from correctly translating. Additionally, because the time zone in the string is given as UTC, you have to set the time zone for the formatter. Here is the code I used (that worked for me), I turned it into a category, and default to using the previously provided string in the event the one that works for me doesn't work for you:
看起来这也有效:
Looks like this also works: