Skip to content

Dates #58

@robertmryan

Description

@robertmryan

In your documentation, you suggest the following NSDateFormatter:

let SQLDateFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    formatter.timeZone = NSTimeZone(abbreviation: "UTC")
    return formatter
}()

I'd make a few suggestions:

  • I'd suggest you also set the locale of the NSDateFormatter to NSLocale(localeIdentifier: "en_US_POSIX") in order to ensure that you don't have problems with users/developers using non-Gregorian calendars. See Technical Q&A 1480.

  • I'd also suggest including the time zone in the string. If you want to handle it like NSLog, you could write it with nice human readable +0000. Or if you want to conform to ISO8601/RFC3339 standards, you'd put a T between the date and the time and append a Z at the end to designate that it's "Zulu".

    Personally, I'd also have the default behavior to include milliseconds (SSS in the format string, too), and perhaps adopt the ISO 8601 style, e.g. yyyy-MM-dd'T'HH:mm:ss.SSS'Z').

  • It's up to you, but rather than proper date handling being a comment in the documentation, and leaving it to the developer to implement it correctly (and, as evidenced by Stack Overflow, most of them won't), I might suggest incorporating it right into the class, so that, by default, the app developer has a good solid handling of NSDate parameters. Maybe make the NSDateFormatter property that the library uses a read/write property that the developer can override with whatever they want. But default it to some well-established best practice, perhaps something like what we've outlined above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions