{"id":1062,"url":"https://github.com/dreymonde/Time","last_synced_at":"2025-08-06T13:32:14.207Z","repository":{"id":53263232,"uuid":"99199347","full_name":"dreymonde/Time","owner":"dreymonde","description":"🕰 Type-safe time calculations in Swift","archived":false,"fork":false,"pushed_at":"2022-05-06T09:03:47.000Z","size":44,"stargazers_count":1071,"open_issues_count":1,"forks_count":56,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-11-21T22:28:52.412Z","etag":null,"topics":["generics","ios","swift","time"],"latest_commit_sha":null,"homepage":"https://medium.com/anysuggestion","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dreymonde.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-03T06:36:40.000Z","updated_at":"2024-11-05T07:58:41.000Z","dependencies_parsed_at":"2022-08-21T05:20:35.395Z","dependency_job_id":null,"html_url":"https://github.com/dreymonde/Time","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreymonde%2FTime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreymonde%2FTime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreymonde%2FTime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dreymonde%2FTime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dreymonde","download_url":"https://codeload.github.com/dreymonde/Time/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228905458,"owners_count":17989771,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generics","ios","swift","time"],"created_at":"2024-01-05T20:15:38.102Z","updated_at":"2024-12-09T14:30:48.934Z","avatar_url":"https://github.com/dreymonde.png","language":"Swift","funding_links":[],"categories":["Date \u0026 Time","Libs","Swift","Date [🔝](#readme)"],"sub_categories":["Getting Started","Date","Other free courses","Linter"],"readme":"# Time\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdreymonde%2FTime%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/dreymonde/Time)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fdreymonde%2FTime%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/dreymonde/Time)\n\nThis micro-library is made for you if:\n\n- You have ever written something like this:\n\n```swift\nlet interval: TimeInterval = 10 * 60\n```\n\nTo represent 10 minutes.\n\n## Usage\n\n#### Showcase\n\n```swift\nimport Time\n\nlet tenMinutes = 10.minutes\nlet afterTenMinutes = Date() + 10.minutes\nlet tenMinutesAndSome = 10.minutes + 15.seconds\nlet tenMinutesInSeconds = 10.minutes.inSeconds\nif 10.minutes \u003e 500.seconds {\n    print(\"That's right\")\n}\n```\n\n#### Basics\n\n**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:\n\n```swift\nlet tenMinutes = 10.minutes\n```\n\nHere `tenMinutes` will actually be of type `Interval\u003cMinute\u003e` (not to be confused with **Foundation**'s `TimeInterval`). There are seven time units available, from nanoseconds to days:\n\n```swift\npublic extension Double {\n    \n    var seconds: Interval\u003cSecond\u003e {\n        return Interval\u003cSecond\u003e(self)\n    }\n    \n    var minutes: Interval\u003cMinute\u003e {\n        return Interval\u003cMinute\u003e(self)\n    }\n    \n    var milliseconds: Interval\u003cMillisecond\u003e {\n        return Interval\u003cMillisecond\u003e(self)\n    }\n    \n    var microseconds: Interval\u003cMicrosecond\u003e {\n        return Interval\u003cMicrosecond\u003e(self)\n    }\n    \n    var nanoseconds: Interval\u003cNanosecond\u003e {\n        return Interval\u003cNanosecond\u003e(self)\n    }\n    \n    var hours: Interval\u003cHour\u003e {\n        return Interval\u003cHour\u003e(self)\n    }\n    \n    var days: Interval\u003cDay\u003e {\n        return Interval\u003cDay\u003e(self)\n    }\n    \n}\n```\n\n#### Operations\n\nYou can perform all basic arithmetic operations on time intervals, even of different units:\n\n```swift\nlet interval = 10.minutes + 15.seconds - 3.minutes + 2.hours // Interval\u003cMinute\u003e\nlet doubled = interval * 2\n\nlet seconds = 10.seconds + 3.minutes // Interval\u003cSecond\u003e\n```\n\nYou can also use these operations on `Date`:\n\n```swift\nlet oneHourAfter = Date() + 1.hours\n```\n\n#### Conversions\n\nTime intervals are easily convertible:\n\n```swift\nlet twoMinutesInSeconds = 2.minutes.inSeconds // Interval\u003cSecond\u003e\n```\n\nYou can also convert intervals to **Foundation**'s `TimeInterval`, if needed:\n\n```swift\nlet timeInterval = 5.minutes.timeInterval\n```\n\nYou can also use `converted(to:)` method:\n\n```swift\nlet fiveSecondsInHours = 5.seconds.converted(to: Hour.self) // Interval\u003cHour\u003e\n// or\nlet fiveSecondsInHours: Interval\u003cHour\u003e = 5.seconds.converted()\n```\n\nAlthough, in my opinion, you would rarely need to.\n\n#### Comparison\n\nYou can compare different time units as well\n\n```swift\n50.minutes \u003c 1.hour\n```\n\n#### Creating your own time units\n\nIf, for some reason, you need to create your own time unit, that's super easy to do:\n\n```swift\npublic enum Week: TimeUnit {\n    \n    public static var toTimeIntervalRatio: Double {\n        return 604800\n    }\n    \n}\n```\n\nNow you can use it as any other time unit:\n\n```swift\nlet fiveWeeks = Interval\u003cWeek\u003e(5)\n```\n\nFor the sake of convenience, don't forget to write those handy extensions:\n\n\n```swift\npublic enum Week: TimeUnit {\n    \n    public static var toTimeIntervalRatio: Double {\n        return 604800\n    }\n    \n}\n\nextension Interval {\n    \n    public var inWeeks: Interval\u003cWeek\u003e {\n        return converted()\n    }\n    \n}\n\nextension Double {\n    \n    public var weeks: Interval\u003cWeek\u003e {\n        return Interval\u003cWeek\u003e(self)\n    }\n    \n}\n\nextension Int {\n    \n    public var weeks: Interval\u003cWeek\u003e {\n        return Interval\u003cWeek\u003e(Double(self))\n    }\n    \n}\n```\n\n#### Also\n\nAlso available:\n\n- Get conversion rate:\n\n```swift\nlet conversionRate = Hour.conversionRate(to: Second.self) // 3600.0\n```\n\n- GCD integration:\n\n```swift\nDispatchQueue.main.asyncAfter(after: 5.seconds) {\n\t// do stuff\n}\n```\n\n## Installation\n\n#### Swift Package Manager\n\nStarting with Xcode 11, **Time** is officially available *only* via Swift Package Manager.\n\nIn Xcode 11 or greater, in you project, select: `File \u003e Swift Packages \u003e Add Package Dependency`\n\nIn the search bar type\n\n```\nhttps://github.com/dreymonde/Time\n``` \n\nThen proceed with installation.\n\n\u003e If you can't find anything in the panel of the Swift Packages you probably haven't added yet your github account.\nYou can do that under the **Preferences** panel of your Xcode, in the **Accounts** section.\n\nFor command-line based apps, you can just add this directly to your **Package.swift** file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/dreymonde/Time\", from: \"1.1.0\"),\n]\n```\n\n#### Manual\n\nOf course, you always have an option of just copying-and-pasting the code - **Time** is just two files, so feel free.\n\n#### Deprecated dependency managers\n\nLast **Time** version to support [Carthage][carthage-url] and [Cocoapods][cocoapods-url] is **1.0.1**. Carthage and Cocoapods will no longer be officially supported.\n\nCarthage:\n\n```ruby\ngithub \"dreymonde/Time\" ~\u003e 1.0.1\n```\n\nCocoapods:\n\n```ruby\npod 'TimeIntervals', '~\u003e 1.0.1'\n```\n\n[carthage-url]: https://github.com/Carthage/Carthage\n[cocoapods-url]: https://github.com/CocoaPods/CocoaPods\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdreymonde%2FTime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdreymonde%2FTime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdreymonde%2FTime/lists"}