<commit_before>//
//  Date.swift
//  MySQL
//
//  Created by ito on 12/16/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import CoreFoundation

public struct SQLDate {
    let absoluteTime: CFAbsoluteTime
    let timeZone: CFTimeZoneRef
    
    init(absoluteTime: CFAbsoluteTime, timeZone: CFTimeZoneRef) {
        self.absoluteTime = absoluteTime
        self.timeZone = timeZone
    }
    
    init(sqlDate: String, timeZone: CFTimeZoneRef) throws {
        self.timeZone = timeZone
        
        if let year = Int32(sqlDate) where sqlDate.characters.count == 4 {
            let date = CFGregorianDate(year: year, month: 1, day: 1, hour: 0, minute: 0, second: 0)
            self.absoluteTime = CFGregorianDateGetAbsoluteTime(date, timeZone)
            return
        }
        let chars:[Character] = Array(sqlDate.characters)
        if let year = Int32(String(chars[0...3])),
            let month = Int8(String(chars[5...6])),
            let day = Int8(String(chars[8...9])),
            let hour = Int8(String(chars[11...12])),
            let minute = Int8(String(chars[14...15])),
            let second = Int8(String(chars[17...18])) where chars.count == 19 {
            let date = CFGregorianDate(year: year, month: month, day: day, hour: hour, minute: minute, second: Double(second))
                if CFGregorianDateIsValid(date, CFGregorianUnitFlags.AllUnits.rawValue) == false {
                    //throw QueryError.InvalidSQLDate(sqlDate)
                }
            self.absoluteTime = CFGregorianDateGetAbsoluteTime(date, timeZone)
            return
        }
        throw QueryError.InvalidSQLDate(sqlDate)
    }
    
    func padNum(num: Int32, digits: Int = 2) -> String {
        return padNum(Int(num), digits: digits)
    }
    func padNum(num: Int8, digits: Int = 2) -> String {
        return padNum(Int(num), digits: digits)
    }
    
    func padNum(num: Int, digits: Int = 2) -> String {
        var str = String(num)
        if num < 0 {
            return str
        }
        while str.characters.count < digits {
            str = "0" + str
        }
        return str
    }
}

extension SQLDate: QueryParameter {
    public func escapedValue() -> String {
        
        let cal = CFAbsoluteTimeGetGregorianDate(absoluteTime, timeZone)<commit_msg>fix crash bug in SQLDate related string bounds<commit_after>//
//  Date.swift
//  MySQL
//
//  Created by ito on 12/16/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import CoreFoundation

public struct SQLDate {
    let absoluteTime: CFAbsoluteTime
    let timeZone: CFTimeZoneRef
    
    init(absoluteTime: CFAbsoluteTime, timeZone: CFTimeZoneRef) {
        self.absoluteTime = absoluteTime
        self.timeZone = timeZone
    }
    
    init(sqlDate: String, timeZone: CFTimeZoneRef) throws {
        self.timeZone = timeZone
        
        switch sqlDate.characters.count {
        case 4:
            if let year = Int32(sqlDate) {
                let date = CFGregorianDate(year: year, month: 1, day: 1, hour: 0, minute: 0, second: 0)
                self.absoluteTime = CFGregorianDateGetAbsoluteTime(date, timeZone)
                return
            }
        case 19:
            let chars:[Character] = Array(sqlDate.characters)
            if let year = Int32(String(chars[0...3])),
                let month = Int8(String(chars[5...6])),
                let day = Int8(String(chars[8...9])),
                let hour = Int8(String(chars[11...12])),
                let minute = Int8(String(chars[14...15])),
                let second = Int8(String(chars[17...18])) {
                    let date = CFGregorianDate(year: year, month: month, day: day, hour: hour, minute: minute, second: Double(second))
                    if CFGregorianDateIsValid(date, CFGregorianUnitFlags.AllUnits.rawValue) == false {
                        //throw QueryError.InvalidSQLDate(sqlDate)
                    }
                    self.absoluteTime = CFGregorianDateGetAbsoluteTime(date, timeZone)
                    return
            }
        default: break
        }
        
        throw QueryError.InvalidSQLDate(sqlDate)
    }
    
    func padNum(num: Int32, digits: Int = 2) -> String {
        return padNum(Int(num), digits: digits)
    }
    func padNum(num: Int8, digits: Int = 2) -> String {
        return padNum(Int(num), digits: digits)
    }
    
    func padNum(num: Int, digits: Int = 2) -> String {
        var str = String(num)
        if num < 0 {
            return str
        }
        while str.characters.count < digits {
            str = "0" + str
        }
        return str
    }
}

extension SQLDate: QueryParameter {
    public func escapedValue() -> String {
        
        let cal = CFAbsoluteTimeGetGregorianDate(absoluteTime, timeZone)
{'fraction_non_alphanumeric': 0.10870470637234486, 'fraction_numerical': 0.022074135776759683, 'mean_word_length': 1.9502457002457003, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '611024', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1396, 'n_words': 354}