<commit_before>//
//  NSDate+ValueType.swift
//  NetworkStack
//
//  Created by <NAME> on 2/24/16.
//  Copyright © 2016 OC Tanner. All rights reserved.
//

import Foundation
import Marshal

extension NSDate: ValueType {
    
    public static func value(object: Any) throws -> NSDate {
        // handle date strings
        if let dateString = object as? String {
            guard let date = NSDate.fromISO8601String(dateString) else {
                throw Marshal.Error.TypeMismatch(expected: "ISO8601 date string", actual: dateString)
            }
            return date
        }
        // handle NSDate objects
        else if let date = object as? NSDate {
            return date
        }
        else {
            throw Marshal.Error.TypeMismatch(expected: String.self, actual: object.dynamicType)
        }
    }
}

public extension NSDate {
    
    static private let ISO8601MillisecondFormatter:NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
        let tz = NSTimeZone(abbreviation:"GMT")
        formatter.timeZone = tz
        return formatter
    }()
    
    static private let ISO8601SecondFormatter:NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
        let tz = NSTimeZone(abbreviation:"GMT")
        formatter.timeZone = tz
        return formatter
    }()
    
    static private let formatters = [ISO8601MillisecondFormatter, ISO8601SecondFormatter]
    
    static func fromISO8601String(dateString:String) -> NSDate? {
        for formatter in formatters {
            if let date = formatter.dateFromString(dateString) {
                return date
            }
        }
        return .None
    }
}<commit_msg>Make sure ISO8601YearMonthDayFormatter is included<commit_after>//
//  NSDate+ValueType.swift
//  NetworkStack
//
//  Created by <NAME> on 2/24/16.
//  Copyright © 2016 OC Tanner. All rights reserved.
//

import Foundation
import Marshal

extension NSDate: ValueType {
    
    public static func value(object: Any) throws -> NSDate {
        // handle date strings
        if let dateString = object as? String {
            guard let date = NSDate.fromISO8601String(dateString) else {
                throw Marshal.Error.TypeMismatch(expected: "ISO8601 date string", actual: dateString)
            }
            return date
        }
            // handle NSDate objects
        else if let date = object as? NSDate {
            return date
        }
        else {
            throw Marshal.Error.TypeMismatch(expected: String.self, actual: object.dynamicType)
        }
    }
}

public extension NSDate {
    
    static private let ISO8601MillisecondFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        let tz = NSTimeZone(abbreviation:"GMT")
        formatter.timeZone = tz
        return formatter
    }()
    
    static private let ISO8601SecondFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        let tz = NSTimeZone(abbreviation:"GMT")
        formatter.timeZone = tz
        return formatter
    }()
    
    static private let ISO8601YearMonthDayFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    static private let formatters = [ISO8601MillisecondFormatter, ISO8601SecondFormatter, ISO8601YearMonthDayFormatter]
    
    static func fromISO8601String(dateString:String) -> NSDate? {
        for formatter in formatters {
            if let date = formatter.dateFromString(dateString) {
                return date
            }
        }
        return .None
    }
}
{'fraction_non_alphanumeric': 0.08402489626556017, 'fraction_numerical': 0.022302904564315353, 'mean_word_length': 2.179719703215169, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2213369', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1096, 'n_words': 275}