ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - dreymonde/Time: 🕰 Type-safe time calculations in Swift
🕰 Type-safe time calculations in Swift. Contribute to dreymonde/Time development by creating an account on GitHub.
Visit Site

GitHub - dreymonde/Time: 🕰 Type-safe time calculations in Swift

GitHub - dreymonde/Time: 🕰 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 Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory