Latest 0.4
Homepage https://github.com/kean/NaiveDate
License MIT
Platforms ios 10.0, watchos 3.0, osx 10.12, tvos 10.0

Date type is great for working with time zones (e.g. 2017-09-29T15:00:00+0300), however, there are scenarios in which naive dates and times are desirable.

Usage

The library implements three types:

  • NaiveDate (e.g. 2017-09-29)
  • NaiveTime (e.g. 15:30:00)
  • NaiveDateTime (e.g. 2017-09-29T15:30:00 – no time zone and no offset).

Each of them implements Equatable, Comparable, LosslessStringConvertible, Codable protocols. Naive types can also be converted to Date, and DateComponents.

Create

Naive dates and times can be created from a string (using a predefined format), using Decodable, or with a memberwise initializer:

NaiveDate("2017-10-01")
NaiveDate(year: 2017, month: 10, day: 1)

NaiveTime("15:30:00")
NaiveTime(hour: 15, minute: 30, second: 0)

NaiveDateTime("2017-10-01T15:30")
NaiveDateTime(
    date: NaiveDate(year: 2017, month: 10, day: 1),
    time: NaiveTime(hour: 15, minute: 30, second: 0)
)

Format

Format dates without having to worry about time zones:

let date = NaiveDate("2017-11-01")!
NaiveDateFormatter(dateStyle: .short).string(from: date)
// prints "Nov 1, 2017"

let time = NaiveTime("15:00")!
NaiveDateFormatter(timeStyle: .short).string(from: time)
// prints "3:00 PM"

let dateTime = NaiveDateTime("2017-11-01T15:30:00")!
NaiveDateFormatter(dateStyle: .short, timeStyle: .short).string(from: dateTime)
// prints "Nov 1, 2017 at 3:30 PM"

Convert

When you do need time zones, convert NaiveDate to Date:

let date = NaiveDate(year: 2017, month: 10, day: 1)

// Creates `Date` in a calendar's time zone
// "2017-10-01T00:00:00+0300" if user is in MSK
Calendar.current.date(from: date)
let dateTime = NaiveDateTime(
    date: NaiveDate(year: 2017, month: 10, day: 1),
    time: NaiveTime(hour: 15, minute: 30, second: 0)
)

// Creates `Date` in a calendar's time zone
// "2017-10-01T15:30:00+0300" if user is in MSK
Calendar.current.date(from: dateTime)

Important! The naive types are called this way because they don’t have a time zone associated with them. This means the date may not actually exist in some areas in the world, even though they are “valid”. For example, when daylight saving changes are applied the clock typically moves forward or backward by one hour. This means certain dates never occur or may occur more than once. If you need to do any precise manipulations with time, always use native Date and Calendar.

Requirements

NaiveDate Swift Xcode Platforms
NaiveDate 0.4 Swift 4.2 – 5.0 Xcode 10.1 – 10.2 iOS 10.0 / watchOS 3.0 / macOS 10.12 / tvOS 10.0
NaiveDate 0.3 Swift 4.0 – 4.2 Xcode 9.2 – 10.1 iOS 10.0 / watchOS 3.0 / macOS 10.12 / tvOS 10.0

License

NaiveDate is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "NaiveDate",
    "version": "0.4",
    "summary": "Naive date and time types",
    "homepage": "https://github.com/kean/NaiveDate",
    "license": "MIT",
    "authors": "Alexander Grebenyuk",
    "social_media_url": "https://twitter.com/a_grebenyuk",
    "source": {
        "git": "https://github.com/kean/NaiveDate.git",
        "tag": "0.4"
    },
    "platforms": {
        "ios": "10.0",
        "watchos": "3.0",
        "osx": "10.12",
        "tvos": "10.0"
    },
    "source_files": "Sources/**/*"
}

Pin It on Pinterest

Share This