IOS 6 NSDateFormatter

2019-01-15 16:25发布

问题:

Please help me with the dateformatter on IOS6, please see the code below

NSString stringDate = @"12/31/9999"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"MM/dd/yyyy"]; 
NSDate *dateCheck = [dateFormatter dateFromString:stringDate]; 
NSLog(@"Date = %@", dateCheck);

Output is

Date = 1999-12-31 08:00:00 +0000

This was the output when converting the string date to date 12/31/9999.

From the previous version of IOS6 the output is

Date = 9999-12-31 08:00:00 +0000 // Correct

回答1:

I made a fix for this for my company's enterprise applications.

It should fix this issue for date formatters using a known format string (like the ones we use to parse dates from our sqlite database).

However, it will not fix:

  1. NSDateFormatters that have isLenient set to true.
  2. NSDateFormatters that use a style, instead of a format string, for parsing.

It does not seem to cause negative side effects on iOS 5 or 5.1. I have not tested anything earlier than that. However, I do mess with the internals of NSDateFormatter a bit, so this may not pass the App Store submission process. However, if you write programs under the Enterprise program (or just use ad hoc deployment), this shouldn't be a problem. Also, it will try to get out of the way if you have isLenient on, but there are no guarantees that you won't run into any issues.

I would like to stress that this is a Temporary Solution. I have not tested this in all possible situations, so you should implement this at your own risk.

I created the following category:

NSDateFormatter+HotFix.h

#import <Foundation/Foundation.h>

@interface NSDateFormatter (HotFix)

- (NSDate*)dateFromString:(NSString *)string;

@end

NSDateFormatter+HotFix.m

#import "NSDateFormatter+HotFix.h"
#import <objc/runtime.h>

@implementation NSDateFormatter (HotFix)

- (NSDate*)dateFromString:(NSString *)string
{
    if (!string) return nil;

    //HACK: Use the original implementation
    void* baseFormatter = nil;
    object_getInstanceVariable(self, "_formatter", &baseFormatter);
    if (!baseFormatter) return nil;

    //Use the underlying CFDateFormatter to parse the string
    CFDateRef rawDate = CFDateFormatterCreateDateFromString(kCFAllocatorDefault, (CFDateFormatterRef)baseFormatter, (CFStringRef)string, NULL);
    NSDate* source = (NSDate*)rawDate;

    //We do not support lenient parsing of dates (or styles), period.
    if (source && !self.isLenient && self.dateStyle == NSDateFormatterNoStyle && self.timeStyle == NSDateFormatterNoStyle)
    {
        //If it worked, then find out if the format string included a year (any cluster of 1 to 5 y characters)
        NSString* format = [self dateFormat];
        NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"y{1,5}" options:NSRegularExpressionCaseInsensitive error:NULL];
        NSArray* matches = [regex matchesInString:format options:0 range:NSMakeRange(0, [format length])];

        if ([matches count] > 0)
        {
            for (NSTextCheckingResult* result in matches)
            {
                //Check for the y grouping being contained within quotes.  If so, ignore it
                if (result.range.location > 0 && result.range.location + result.range.length < [format length] - 1)
                {
                    if ([format characterAtIndex:result.range.location - 1] == '\'' && 
                        [format characterAtIndex:result.range.location + result.range.length + 1] == '\'') continue;
                }

                NSString* possibleYearString = [string substringWithRange:result.range];
                NSInteger possibleYear = [possibleYearString integerValue];

                if (possibleYear > 3500)
                {
                    NSCalendar* calendar = [NSCalendar currentCalendar];
                    NSDateComponents* dateComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:source];
                    dateComp.year = possibleYear;

                    return [calendar dateFromComponents:dateComp];
                }
            }
        }
    }

    return [source autorelease];
}

@end

It will replace the existing dateFromString method of NSDateFormatter. It works by trying to parse the string normally, then checking to see if the formatString has a set of year formatting characters inside it. If it does, it manually pulls the year out and checks if it is greater than 3500. Finally, if this is the case, it rewrites the output to have the correctly parsed year.

Simply include it in your project and it will take effect. You do not need to import the header into every file that uses a NSDateFormatter, just having the .m compiled in will modify the class. If you have any other categories that change dateFromString: then the effects of this class cannot be defined.

I hope this helps.