Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dreymonde/Time
🕰 Type-safe time calculations in Swift
https://github.com/dreymonde/Time
generics ios swift time
Last synced: about 1 month ago
JSON representation
🕰 Type-safe time calculations in Swift
- Host: GitHub
- URL: https://github.com/dreymonde/Time
- Owner: dreymonde
- License: mit
- Created: 2017-08-03T06:36:40.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-05-06T09:03:47.000Z (over 2 years ago)
- Last Synced: 2024-11-21T22:28:52.412Z (about 2 months ago)
- Topics: generics, ios, swift, time
- Language: Swift
- Homepage: https://medium.com/anysuggestion
- Size: 43 KB
- Stars: 1,071
- Watchers: 17
- Forks: 56
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - Time - Type-safe time calculations in Swift, powered by generics. (Date & Time / Getting Started)
- awesome-swift - Time - Type-safe time calculations, powered by generics. (Libs / Date)
- awesome-swift - Time - Type-safe time calculations, powered by generics. (Libs / Date)
- awesome-ios-star - Time - Type-safe time calculations in Swift, powered by generics. (Date & Time / Getting Started)
- fucking-awesome-swift - Time - Type-safe time calculations, powered by generics. (Libs / Date)
- fucking-awesome-ios - Time - Type-safe time calculations in Swift, powered by generics. (Date & Time / Getting Started)
- fucking-awesome-ios - Time - Type-safe time calculations in Swift, powered by generics. (Date & Time / Getting Started)
- awesome-swift - Time - Type-safe time calculations in Swift ` 📝 2 years ago ` (Date [🔝](#readme))
README
# Time
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdreymonde%2FTime%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/dreymonde/Time)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdreymonde%2FTime%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/dreymonde/Time)This micro-library is made for you if:
- You have ever written something like this:
```swift
let interval: TimeInterval = 10 * 60
```To represent 10 minutes.
## Usage
#### Showcase
```swift
import Timelet 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:
```swift
let tenMinutes = 10.minutes
```Here `tenMinutes` will actually be of type `Interval` (not to be confused with **Foundation**'s `TimeInterval`). There are seven time units available, from nanoseconds to days:
```swift
public extension Double {
var seconds: Interval {
return Interval(self)
}
var minutes: Interval {
return Interval(self)
}
var milliseconds: Interval {
return Interval(self)
}
var microseconds: Interval {
return Interval(self)
}
var nanoseconds: Interval {
return Interval(self)
}
var hours: Interval {
return Interval(self)
}
var days: Interval {
return Interval(self)
}
}
```#### Operations
You can perform all basic arithmetic operations on time intervals, even of different units:
```swift
let interval = 10.minutes + 15.seconds - 3.minutes + 2.hours // Interval
let doubled = interval * 2let seconds = 10.seconds + 3.minutes // Interval
```You can also use these operations on `Date`:
```swift
let oneHourAfter = Date() + 1.hours
```#### Conversions
Time intervals are easily convertible:
```swift
let twoMinutesInSeconds = 2.minutes.inSeconds // Interval
```You can also convert intervals to **Foundation**'s `TimeInterval`, if needed:
```swift
let timeInterval = 5.minutes.timeInterval
```You can also use `converted(to:)` method:
```swift
let fiveSecondsInHours = 5.seconds.converted(to: Hour.self) // Interval
// or
let fiveSecondsInHours: Interval = 5.seconds.converted()
```Although, in my opinion, you would rarely need to.
#### Comparison
You can compare different time units as well
```swift
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:
```swift
public enum Week: TimeUnit {
public static var toTimeIntervalRatio: Double {
return 604800
}
}
```Now you can use it as any other time unit:
```swift
let fiveWeeks = Interval(5)
```For the sake of convenience, don't forget to write those handy extensions:
```swift
public enum Week: TimeUnit {
public static var toTimeIntervalRatio: Double {
return 604800
}
}extension Interval {
public var inWeeks: Interval {
return converted()
}
}extension Double {
public var weeks: Interval {
return Interval(self)
}
}extension Int {
public var weeks: Interval {
return Interval(Double(self))
}
}
```#### Also
Also available:
- Get conversion rate:
```swift
let conversionRate = Hour.conversionRate(to: Second.self) // 3600.0
```- GCD integration:
```swift
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:
```swift
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][carthage-url] and [Cocoapods][cocoapods-url] is **1.0.1**. Carthage and Cocoapods will no longer be officially supported.
Carthage:
```ruby
github "dreymonde/Time" ~> 1.0.1
```Cocoapods:
```ruby
pod 'TimeIntervals', '~> 1.0.1'
```[carthage-url]: https://github.com/Carthage/Carthage
[cocoapods-url]: https://github.com/CocoaPods/CocoaPods