Giter Site home page Giter Site logo

naivedate's Introduction

NaiveDate

Native Date type is great for working with time zones (e.g. 2017-09-29T15:00:00+0300), but there are scenarios where you don't know or care about the time zone. These types of dates are often called naive.

Usage

The NaiveDate library implements three types:

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

They all implement Equatable, Comparable, LosslessStringConvertible, and Codable protocols. Naive date types can also be converted to Date, and DateComponents.

Create

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

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

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

NaiveDateTime("2021-10-01T15:30")
NaiveDateTime(
    date: NaiveDate(year: 2021, 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("2021-11-01")!
NaiveDateFormatter(dateStyle: .short).string(from: date)
// prints "Nov 1, 2021"

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

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

Convert

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

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

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

// Creates `Date` in a calendar's time zone
// "2021-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.

Minimum Requirements

NaiveDate Swift Xcode Platforms
NaiveDate 1.0 Swift 5.3 Xcode 12.0 iOS 11.0 / watchOS 4.0 / macOS 10.13 / tvOS 11.0
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.

naivedate's People

Contributors

kean avatar liamnichols avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.