How to compare file lastModifiedDate between iPad and Web?

Discussion in 'iOS Programming' started by mikezang, Aug 29, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I use code as below to compare file's last modified date between iPad and Internet, but the date is GMT format on Web, and date format is local on iPad, how can I compare them?
    Code:
    NSString *url = [NSString stringWithFormat:@"http://protra.sourceforge.jp/data/index.txt.lzh"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    	
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    	
    if ([response respondsToSelector:@selector(allHeaderFields)])	{
    	NSDictionary *metaData = [response allHeaderFields];
    	NSString *lastModifiedString = [metaData objectForKey:@"Last-Modified"];
    		
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	NSString *indexFile = [self pathInDocumentsFolder:kIndexFile];
    	NSDate *lastDate = [[fileManager attributesOfItemAtPath:indexFile error:nil] fileModificationDate];
    	NSString *lastDateString = [lastDate description];
    		
    	if ([lastDateString compare:lastModifiedString]) {
    		return YES;
    	}
    	else {
    		return NO;
    	}
    }
    
     

    Attached Files:

  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    http://developer.apple.com/iphone/l...SDateFormatter_Class/Reference/Reference.html

    Instances of NSDateFormatter create string representations of NSDate (and NSCalendarDate) objects, and convert textual representations of dates and times into NSDate objects. You can express the representation of dates and times flexibly using pre-set format styles or custom format strings.

    http://developer.apple.com/iphone/l...a/Conceptual/DatesAndTimes/DatesAndTimes.html


    The idea of comparing the dates as strings won't work, unless you use a format that produces strings with the proper ordering. Otherwise you'll be comparing month-names as significant text ("February" before "January"), locale conventions for placement of year month day will be misinterpreted, and other completely wrong results. Timezones would be the least of the problems.
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Maybe you are right, I just thought if both date string in format like "YYYY-MM-DD HH:MM:SS ±HHMM" and with the same Time Zone, the comparing is no problem...

    But it is safety to use date comparing, I am going to convert date string to date and compare them.

    Thanks for your suggestion.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    That could work if you have control over the remote server's Last-Modified header. But you don't.

    HTTP has several acceptable header formats, and you can't control what the server uses. For example, look at the samples of Last-Modified headers in the specification for HTTP 1.1 (RFC 2616):
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

    Then look at the acceptable forms for an HTTP-date:
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Well, like you said "HTTP has several acceptable header formats", so how can I convert it to NSDate?

    For example, I got date string by
    Code:
    NSString *lastModifiedString = [metaData objectForKey:@"Last-Modified"];
    
    What can I do next? is this ok?
    Code:
    NSDate lastModifiedDate = [NSDateFormatter dateFromString:lastModifiedString];
    
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Do you know what format it's in? If not, then that should probably be step 1.

    If you do know what format the header is in, then you still have to apply the NSDateFormatter correctly.

    For example, you either didn't read the NSDateFormatter reference doc I linked to, or you didn't understand what you read. Otherwise you would have seen that dateFromString: is an instance method, not a class method. And then you would have been able to answer this question yourself:
    If you don't already know the answer, then you should try it and see what happens.


    The short answer in all of this is, "There is no short answer." You'll have to study how the class works, look at the data you get from servers, read the RFC about different date formats, and then design your code and make it work. If you're looking for a ready-to-paste fully coded handout, you'll have to look elsewhere.

    In fact, I suggest you do look elsewhere. There are lots of other resources on the web, and they aren't difficult to find. You already know what class names are relevant, what protocol is relevant (HTTP), and what header-name is relevant.
     
  7. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    At the moment, I can't try it because I am not at home.

    I know the last modified format is in "Sun, 29 Aug 2010 06:12:03 GMT".
     

Share This Page