Latest 1.0.0
Homepage https://github.com/studio-rookery/TimeKit
License MIT
Platforms ios 10.0, osx 10.12, tvos 10.0, watchos 3.0
Authors

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

// 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
Swift 4.2+
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.0.0")

Latest podspec

{
    "name": "TimeKit",
    "version": "1.0.0",
    "summary": "Type safe, simple, light weight calendar components.",
    "homepage": "https://github.com/studio-rookery/TimeKit",
    "license": "MIT",
    "authors": {
        "masaki": "[email protected]"
    },
    "swift_version": "4.2",
    "platforms": {
        "ios": "10.0",
        "osx": "10.12",
        "tvos": "10.0",
        "watchos": "3.0"
    },
    "source": {
        "git": "https://github.com/studio-rookery/TimeKit.git",
        "tag": "1.0.0"
    },
    "source_files": "Sources/**/*.{swift,h,m}"
}

Pin It on Pinterest

Share This