• Stars
    star
    1,070
  • Rank 41,514 (Top 0.9 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ•ฐ Type-safe time calculations in Swift

Time

This micro-library is made for you if:

  • You have ever written something like this:
let interval: TimeInterval = 10 * 60

To represent 10 minutes.

Usage

Showcase

import Time

let tenMinutes = 10.minutes
let afterTenMinutes = Date() + 10.minutes
let tenMinutesAndSome = 10.minutes + 15.seconds
let tenMinutesInSeconds = 10.minutes.inSeconds
if 10.minutes > 500.seconds {
    print("That's right")
}

Basics

Time is not just a bunch of Double conversion functions. The main advantage of it is that all time units are strongly-typed. So, for example:

let tenMinutes = 10.minutes

Here tenMinutes will actually be of type Interval<Minute> (not to be confused with Foundation's TimeInterval). There are seven time units available, from nanoseconds to days:

public extension Double {
    
    var seconds: Interval<Second> {
        return Interval<Second>(self)
    }
    
    var minutes: Interval<Minute> {
        return Interval<Minute>(self)
    }
    
    var milliseconds: Interval<Millisecond> {
        return Interval<Millisecond>(self)
    }
    
    var microseconds: Interval<Microsecond> {
        return Interval<Microsecond>(self)
    }
    
    var nanoseconds: Interval<Nanosecond> {
        return Interval<Nanosecond>(self)
    }
    
    var hours: Interval<Hour> {
        return Interval<Hour>(self)
    }
    
    var days: Interval<Day> {
        return Interval<Day>(self)
    }
    
}

Operations

You can perform all basic arithmetic operations on time intervals, even of different units:

let interval = 10.minutes + 15.seconds - 3.minutes + 2.hours // Interval<Minute>
let doubled = interval * 2

let seconds = 10.seconds + 3.minutes // Interval<Second>

You can also use these operations on Date:

let oneHourAfter = Date() + 1.hours

Conversions

Time intervals are easily convertible:

let twoMinutesInSeconds = 2.minutes.inSeconds // Interval<Second>

You can also convert intervals to Foundation's TimeInterval, if needed:

let timeInterval = 5.minutes.timeInterval

You can also use converted(to:) method:

let fiveSecondsInHours = 5.seconds.converted(to: Hour.self) // Interval<Hour>
// or
let fiveSecondsInHours: Interval<Hour> = 5.seconds.converted()

Although, in my opinion, you would rarely need to.

Comparison

You can compare different time units as well

50.minutes < 1.hour

Creating your own time units

If, for some reason, you need to create your own time unit, that's super easy to do:

public enum Week: TimeUnit {
    
    public static var toTimeIntervalRatio: Double {
        return 604800
    }
    
}

Now you can use it as any other time unit:

let fiveWeeks = Interval<Week>(5)

For the sake of convenience, don't forget to write those handy extensions:

public enum Week: TimeUnit {
    
    public static var toTimeIntervalRatio: Double {
        return 604800
    }
    
}

extension Interval {
    
    public var inWeeks: Interval<Week> {
        return converted()
    }
    
}

extension Double {
    
    public var weeks: Interval<Week> {
        return Interval<Week>(self)
    }
    
}

extension Int {
    
    public var weeks: Interval<Week> {
        return Interval<Week>(Double(self))
    }
    
}

Also

Also available:

  • Get conversion rate:
let conversionRate = Hour.conversionRate(to: Second.self) // 3600.0
  • GCD integration:
DispatchQueue.main.asyncAfter(after: 5.seconds) {
	// do stuff
}

Installation

Swift Package Manager

Starting with Xcode 11, Time is officially available only via Swift Package Manager.

In Xcode 11 or greater, in you project, select: File > Swift Packages > Add Package Dependency

In the search bar type

https://github.com/dreymonde/Time

Then proceed with installation.

If you can't find anything in the panel of the Swift Packages you probably haven't added yet your github account. You can do that under the Preferences panel of your Xcode, in the Accounts section.

For command-line based apps, you can just add this directly to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/dreymonde/Time", from: "1.1.0"),
]

Manual

Of course, you always have an option of just copying-and-pasting the code - Time is just two files, so feel free.

Deprecated dependency managers

Last Time version to support Carthage and Cocoapods is 1.0.1. Carthage and Cocoapods will no longer be officially supported.

Carthage:

github "dreymonde/Time" ~> 1.0.1

Cocoapods:

pod 'TimeIntervals', '~> 1.0.1'

More Repositories

1

AppFolder

๐Ÿ—‚ Never use NSSearchPathForDirectoriesInDomains again
Swift
938
star
2

Delegated

๐Ÿ‘ทโ€โ™€๏ธ Closure-based delegation without memory leaks
Swift
704
star
3

Shallows

๐Ÿ›ถ Your lightweight persistence toolbox
Swift
623
star
4

NiceNotifications

๐Ÿ”” Create rich local notifications experiences on iOS with incredible ease
Swift
259
star
5

Placeholders

๐Ÿ…ฟ๏ธ Define multiple placeholders for UITextField and animate their change
Swift
199
star
6

ScheduledNotificationsViewController

See all your scheduled local notifications in one place
Swift
171
star
7

TheGreatGame

๐Ÿ† Open-source first-class iOS & watchOS app dedicated to Womenโ€™s Euro 2017
Swift
135
star
8

DateBuilder

๐Ÿ“† Create dates and date components easily (e.g. "first Thursday of the next month")
Swift
131
star
9

TelegraphKit

๐Ÿ“œ The ultimate solution for showing ad hoc, server-editable web content (FAQs, Tutorials, Privacy Policy, etc.) in your iOS apps
Swift
52
star
10

Paperville

๐Ÿ™ Design a city in Swift code (๏ฃฟWWDC 2018 submission, ACCEPTED)
Swift
51
star
11

DonateToUkraine

๐Ÿ‡บ๐Ÿ‡ฆ Implement "donate to Ukraine" inside your app, with Apple Pay
Swift
25
star
12

Alba

๐ŸŽ™ Stateful event observing engine [DEPRECATED]
Swift
19
star
13

Avenues

๐ŸŒ… [WIP] Idiomatic image fetching and caching in Swift.
Swift
13
star
14

Subviews

๐Ÿงฉ @โ€‹Subview and other ways of making UIKit more fun to use
Swift
9
star
15

Timers

โฒ๏ธ Intuitive Swift timers with automatic memory management
Swift
6
star
16

SwiftyNURE

Swift framework for NURE API (CIST)
Swift
3
star
17

Operacjas

๐Ÿ›  [DEPRECATED] Unlocking the full glory of NSOperations
Swift
2
star
18

Swift-hints

1
star
19

DynamicInstance

Swift
1
star
20

Light

๐Ÿ•Š Super thin networking layer built on top of Shallows
Swift
1
star
21

Operations

[WIP] NSOperations for 2018
Swift
1
star
22

uahelp-js-scripts

JavaScript
1
star
23

Avenues-Shallows

Making caching even better
Swift
1
star
24

SofarKit

Access Sofar admin data with Swift [WIP]
Swift
1
star