Skip to content

CreateAPI/NaiveDate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NaiveDate

The standard Date type is excellent for working with timestamps and time zones (e.g. 2024-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 referred to as naive.

Usage

The NaiveDate library implements three types:

  • NaiveDate (e.g. 2024-09-29)
  • NaiveTime (e.g. 15:30:00)
  • NaiveDateTime (e.g. 2024-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("2024-10-01")
NaiveDate(year: 2024, month: 10, day: 1)

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

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

Format

NaiveDate supports Foundation.FormatStyle:

let dateTime = NaiveDateTime("2024-11-01T15:30:00")!
dateTime.formatted(date: .numeric, time: .standard)
// prints "11/1/2024, 3:30:00 PM"

In addition to the format style, you can use built-in NaiveDateFormatter directly.

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

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

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

Convert

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

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

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

// Creates `Date` in a calendar's time zone
// "2024-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 Platforms
NaiveDate 1.1 Swift 5.9 iOS 13, tvOS 13, watchOS 6, macOS 10.15
NaiveDate 1.0 Swift 5.3 iOS 11, tvOS 11, watchOS 4, macOS 10.13,

License

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