TimeKit

1.2.1

Type safe, simple, lightweight date components.
studio-rookery/TimeKit

What's New

1.2.1

2019-08-20T14:13:39Z
Xcode Swift
10.3 5.0

Fixed

  • Fix time gap in Auckland(#20)
  • Use swift tool chain version 5.0

TimeKit

Build Status codecov SwiftPM Carthage compatible Version Platform

TimeKit is a type safe, simple, lightweight date components.

let calendar = Calendar.utc

// 1970/01/01(thu) 00:00 +00:00
let date = Date(timeIntervalSince1970: 0)

// TimeKit provides date components: `Day`, `Month`, `Year`, `Weekday`.
// These are compatible with `Date`.
let day = calendar.day(for: date)
let month = calendar.month(for: date)
let year = calendar.year(for: date)
let weekday = day.weekday

Day, Month, Year, Weekday are independent of a particular calendar or time zone. To represent these components to a user, you must interpret them in the context of a Calendar.

let inUTC = Calendar.utc.startDate(of: day)           // 1970/01/01(thu) 00:00 +00:00
let inTokyo = Calendar.tokyo.startDate(of: day)       // 1970/01/01(thu) 09:00 +00:00
let inHonolulu = Calendar.honolulu.startDate(of: day) // 1969/12/31(wed) 14:00 +00:00

Usage

Convert Date to Day, Month, Year

let calendar = Calendar.utc
let date = Date(timeIntervalSince1970: 0)

let day = calendar.day(for: date)
let month = calendar.month(for: date)
let year = calendar.year(for: date)

The date components are independent of a particular calendar or time zone. You need a Calendar to convert them from Date.

Convert Day, Month, Year to Date

let startOfDay = calendar.start(of: day)
let startOfMonth = calendar.start(of: month)
let startOfYear = calendar.start(of: year)

For the same reason as above, you need a Calendar to convert to Date from the date components.

Manipulation

// A day after / before `today`
let tomorrow = day.next
let yesterday = day.previous

// Addition and subtraction
let threeDaysLater = day + 3.day
let fourMonthsAgo = month - 4.month
let fiveYearsLater = year + 5.year

// Obtain the date components range
let days = 42.days(from: today)     // ["1970/01/01", "1970/01/02", "1970/01/03", ...]
let months = 12.months(from: month) // ["1970/01", "1970/02", "1970/03", ...]
let years = 3.years(from: year)     // ["1970", "1971", "1972"]

Date Formatting

let dateFormatter = ...

let dayString = dateFormatter.string(from: day)
print(dayString) // => "1970/01/01(thu)"

let day = dateFormatter.day(from: "1970/01/01(thu)")
print(day) // => "1970/01/01(thu) in gregorian calendar"

Requirements

Name Version
Xcode 10.2+
Swift 5.0+
iOS 10.0+
macOS 10.12+
tvOS 10.0+
watchOS 3.0+

Installation

CocoaPods

pod 'TimeKit'

Carthage

github "studio-rookery/TimeKit"

Swift Package Manager

.package(url: "https://github.com/studio-rookery/TimeKit", from: "1.2.0")

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Mar 17 2024 05:45:22 GMT-0900 (Hawaii-Aleutian Daylight Time)