stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.72k stars 1.57k forks source link

Error while using DateTime Extension #313

Closed onlyway closed 7 years ago

onlyway commented 8 years ago

Hello I'm trying to use your DateTime extension for Swift2 project, while I'm pasting this code:

extension NSDate: Value {
    class var declaredDatatype: String {
        return String.declaredDatatype
    }
    class func fromDatatypeValue(stringValue: String) -> NSDate {
        return SQLDateFormatter.dateFromString(stringValue)!
    }
    var datatypeValue: String {
        return SQLDateFormatter.stringFromDate(self)
    }
}

let SQLDateFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    return formatter
}()

I've got such error Redundant conformance of "NSDate" to protocol 'Value' SQLite.swift

cjwirth commented 8 years ago

You don't need to paste that in, there is already an extension included that does that. You should be able to just use NSDate already.

I came here to post an issue quite similar --> except that I don't want the extension in there. It's not the date format that I need to save in, so it's kind of annoying that it's included by default.

If this extension was deleted, then I could make a new subclass of NSDate for each different date format I wanted to save.

e.g.

private let ShortDateFormatter = NSDateFormatter(format: "yyyyMMdd")
class ShortDate: NSDate, Value {

    public class var declaredDatatype: String {
        return String.declaredDatatype
    }

    public class func fromDatatypeValue(stringValue: String) -> NSDate {
        return ShortDateFormatter.dateFromString(stringValue)!
    }

    public var datatypeValue: String {
        return ShortDateFormatter.stringFromDate(self)
    }

}
cjwirth commented 8 years ago

On second thought that might not be very useful. I am fetching and loading some data from a server, and it comes in yyyyMMdd format. That doesn't necessarily mean I have to save it in that format though. Using this library, I'd still have to fetch a range using the NSDate anyway...

Still something to keep in mind. Also, I'm not sure if a global public variable named simply dateFormatter is a very good idea :full_moon_with_face:

helbertgs commented 8 years ago

​Caesar,

try this

//

// NSDate.swift

// Spartan

//

// Created by Helbert Gomes on 06/01/16.

// Copyright © 2016 Mobilus. All rights reserved.

//

import Foundation

extension NSDate

{

func ToString() -> String

{

let formatter = NSDateFormatter()

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

return formatter.stringFromDate(self)

}

func ToString(format : String) -> String

{

let formatter = NSDateFormatter()

formatter.dateFormat = format

return formatter.stringFromDate(self)

} }​

Helbert Gomes Game Developer Cel: +55 (31) 9929 - 5833 helbertgs@gmail.com http://gmail.com/

"O sucesso é a soma de pequenos esforços - repetidos dia sim, e no outro dia também." (Robert Collier)

2016-01-08 10:10 GMT-02:00 Caesar Wirth notifications@github.com:

You don't need to paste that in, there is already an extension included that does that. You should be able to just use NSDate already.

I came here to post an issue quite similar --> except that I don't want the extension in there. It's not the date format that I need to save in, so it's kind of annoying that it's included by default.

If this extension was deleted, then I could make a new subclass of NSDate for each different date format I wanted to save.

e.g.

private let ShortDateFormatter = NSDateFormatter(format: "yyyyMMdd") class ShortDate: NSDate, Value {

public class var declaredDatatype: String {
    return String.declaredDatatype
}

public class func fromDatatypeValue(stringValue: String) -> NSDate {
    return ShortDateFormatter.dateFromString(stringValue)!
}

public var datatypeValue: String {
    return ShortDateFormatter.stringFromDate(self)
}

}

— Reply to this email directly or view it on GitHub https://github.com/stephencelis/SQLite.swift/issues/313#issuecomment-169982235 .

cjwirth commented 8 years ago

Thanks, Helbert. I actually don't have any problems getting a string from a date though!

I'm more curious about how to filter by range (e.g. all records from day 1 to day 5). I ended up making a wrapper that converts all the dates being saved to the beginning of the day of that date. I don't need the actual time of day in this case. Otherwise, dates saved in and then taken out will not be equal (the time interval from 1970 is ever so slightly off), so date == date2 will fail, even though it is the same.

It's late on Friday night, so there are probably better solutions that my brain isn't coming up with right now!

jberkel commented 7 years ago

The extension is now include by default, closing