{"id":17688867,"url":"https://github.com/kizitonwose/time","last_synced_at":"2025-04-12T22:37:04.334Z","repository":{"id":26390154,"uuid":"108419228","full_name":"kizitonwose/Time","owner":"kizitonwose","description":"Type-safe time calculations in Kotlin, powered by generics.","archived":false,"fork":false,"pushed_at":"2022-06-16T04:59:16.000Z","size":136,"stargazers_count":979,"open_issues_count":1,"forks_count":39,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-04T02:07:56.310Z","etag":null,"topics":["generics","interval","kotlin","time"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","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/kizitonwose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-26T14:00:56.000Z","updated_at":"2025-03-10T12:53:13.000Z","dependencies_parsed_at":"2022-09-15T23:21:17.625Z","dependency_job_id":null,"html_url":"https://github.com/kizitonwose/Time","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2FTime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2FTime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2FTime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kizitonwose%2FTime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kizitonwose","download_url":"https://codeload.github.com/kizitonwose/Time/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248643006,"owners_count":21138353,"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","interval","kotlin","time"],"created_at":"2024-10-24T11:45:46.675Z","updated_at":"2025-04-12T22:37:04.313Z","avatar_url":"https://github.com/kizitonwose.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Time\n\n[![JitPack](https://jitpack.io/v/kizitonwose/Time.svg)](https://jitpack.io/#kizitonwose/Time) \n[![Build Status](https://travis-ci.org/kizitonwose/Time.svg?branch=master)](https://travis-ci.org/kizitonwose/Time) \n[![Coverage](https://img.shields.io/codecov/c/github/kizitonwose/Time/master.svg)](https://codecov.io/gh/kizitonwose/Time) \n[![Codacy](https://api.codacy.com/project/badge/Grade/26b009748a0849f3973887fbbcd84900)](https://www.codacy.com/app/kizitonwose/Time) \n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/kizitonwose/Time/blob/master/LICENSE.md) \n\nThis library is made for you if you have ever written something like this: \n\n```kotlin\nval duration = 10 * 1000\n```\nto represent a duration of 10 seconds(in milliseconds) because most methods in Kotlin/Java take duration parameters in milliseconds.\n\n## Usage\n\n### Showcase\n\n```kotlin\nval tenSeconds = 10.seconds\nval fiveMinutes = 5.minutes\nval twoHours = 2.hours\nval threeDays = 3.days\nval tenMinutesFromNow = Calendar.getInstance() + 10.minutes\nval tenSecondsInMilliseconds = 10.seconds.inMilliseconds\n```\n\n### Basics\n\nThe main advantage of the library is that all time units are *strongly-typed*. So, for example:\n\n```kotlin\nval tenMinutes = 10.minutes\n```\n\nIn the example above,  `tenMinutes` will be of type `Interval\u003cMinute\u003e`. There are seven time units available, from nanoseconds to days:\n\n```kotlin\nval tenNanoseconds = 10.nanoseconds \n// type is Interval\u003cNanosecond\u003e\n```\n```kotlin\nval tenMicroseconds = 10.microseconds \n// type is Interval\u003cMicrosecond\u003e\n```\n```kotlin\nval tenMilliseconds = 10.milliseconds \n// type is Interval\u003cMillisecond\u003e\n```\n```kotlin\nval tenSeconds = 10.seconds \n// type is Interval\u003cSecond\u003e\n```\n```kotlin\nval tenMinutes = 10.minutes \n// type is Interval\u003cMinute\u003e\n```\n```kotlin\nval tenHours = 10.hours \n// type is Interval\u003cHour\u003e\n```\n```kotlin\nval tenDays = 10.days \n// type is Interval\u003cDay\u003e\n```\n\n### Operations\n\nYou can perform all basic arithmetic operations on time intervals, even of different units:\n\n```kotlin\nval duration = 10.minutes + 15.seconds - 3.minutes + 2.hours // Interval\u003cMinute\u003e\nval doubled = duration * 2\n\nval seconds = 10.seconds + 3.minutes // Interval\u003cSecond\u003e\n```\n\nYou can also use these operations with the `Calendar` class:\n\n```kotlin\nval twoHoursLater = Calendar.getInstance() + 2.hours\n```\n\n### Conversions\n\nTime intervals are easily convertible:\n\n```kotlin\nval twoMinutesInSeconds = 2.minutes.inSeconds // Interval\u003cSecond\u003e\nval fourDaysInHours = 4.days.inHours // Interval\u003cHour\u003e\n```\n\nYou can also use the `converted()` method, although you would rarely need to:\n\n```kotlin\nval tenMinutesInSeconds: Interval\u003cSecond\u003e = 10.minutes.converted()\n```\n\n### Comparison\n\nYou can compare different time units as well:\n\n```kotlin\n50.seconds \u003c 2.hours // true\n120.minutes == 2.hours // true\n100.milliseconds \u003e 2.seconds // false\n48.hours in 2.days // true\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```kotlin\nclass Week : TimeUnit {\n    // number of seconds in one week\n    override val timeIntervalRatio = 604800.0\n}\n```\n\nNow you can use it like any other time unit:\n\n```kotlin\nval fiveWeeks = Interval\u003cWeek\u003e(5)\n```\n\nFor the sake of convenience, don't forget to write those handy extensions:\n\n```kotlin\nclass Week : TimeUnit {\n    override val timeIntervalRatio = 604800.0\n}\n\nval Number.weeks: Interval\u003cWeek\u003e\n    get() = Interval(this)\n\nval Interval\u003cTimeUnit\u003e.inWeeks: Interval\u003cWeek\u003e\n    get() = converted()\n```\nNow you can write:\n\n```kotlin\nval fiveWeeks = 5.weeks // Interval\u003cWeek\u003e\n```\nYou can also easily convert to weeks:\n\n```kotlin\nval valueInWeeks = 14.days.inWeeks // Interval\u003cWeek\u003e\n```\n\n### Extras\n\nThe library includes some handy extensions for some classes:\n\n```kotlin\nval now = Calendar.getInstance() \nval sixHoursLater = now + 6.hours\nval fourDaysAgo = now - 4.days\n```\n\n```kotlin\nval timer = Timer()\ntimer.schedule(10.seconds) {\n    println(\"This block will be called in 10 seconds\")\n}\n```\n\nThe library also includes extensions for Android's Handler class, this is only available if you compile the \"time-android\" module.\n\n```kotlin\nval handler = Handler()\nhandler.postDelayed({\n    Log.i(\"TAG\", \"This will be printed to the Logcat in 2 minutes\")\n}, 2.minutes)\n```\nMore extensions will be added to the library in the future.\n\n### Conversion safety everywhere\n\nFor time-related methods in other third-party libraries in your project, if such methods are frequently used, it's best to write extention functions that let you use the time units in this libary in those methods. This is mostly just one line of code. \n\nIf such methods aren't frequently used, you can still benefit from the conversion safety that comes with this library.\n\nAn example method in a third-party library that does something after a delay period in milliseconds:\n\n```kotlin\nclass Person {\n    fun doSomething(delayMillis: Long) {\n        // method body\n    }\n}\n```\n\nTo call the method above with a value of 5 minutes, one would usually write:\n\n```kotlin\nval person = Person()\nperson.doSomething(5 * 60 * 1000)\n```\n\nThe above line can be written in a safer and clearer way using this library:\n\n```kotlin\nval person = Person()\nperson.doSomething(5.minutes.inMilliseconds.longValue)\n```\n\nIf the method is frequently used, you can write an extension function:\n\n```kotlin\nfun Person.doSomething(delay: Interval\u003cTimeUnit\u003e) {\n    doSomething(delay.inMilliseconds.longValue)\n}\n```\nNow you can write:\n\n```kotlin\nval person = Person()\nperson.doSomething(5.minutes)\n```\n\n## Installation\n\nAdd the JitPack repository to your `build.gradle`:\n\n```groovy\nallprojects {\n repositories {\n    maven { url \"https://jitpack.io\" }\n    }\n}\n```\n\nAdd the dependency to your `build.gradle`:\n\n- For **non-Android** projects:\n\n```groovy\ndependencies {\n    compile 'com.github.kizitonwose.time:time:\u003cversion\u003e'\n}\n```\n\n- For **Android** projects:\n\n```groovy\ndependencies {\n    compile 'com.github.kizitonwose.time:time-android:\u003cversion\u003e'\n}\n```\n\n## Contributing\nThe goal is for the library to be used wherever possible. If there are extension functions or features you think the library should have, feel free to add them and send a pull request or open an issue. Core Kotlin extensions belong in the \"[time][time-core-module-url]\" module while Android extensions belong in \"[time-android][time-android-module-url]\" module.\n\n\n## Inspiration\nTime was inspired by a Swift library of the same name - [Time][time-swift-url].\n\nThe API of Time(Kotlin) has been designed to be as close as possible to Time(Swift) for consistency and because the two languages have many similarities. Check out [Time(Swift)][time-swift-url] if you want a library with the same functionality in Swift.\n\n\n## License\nTime is distributed under the MIT license. [See LICENSE](https://github.com/kizitonwose/Time/blob/master/LICENSE.md) for details.\n\n\n[time-swift-url]: https://github.com/dreymonde/Time \n[time-core-module-url]: https://github.com/kizitonwose/Time/tree/master/time \n[time-android-module-url]: https://github.com/kizitonwose/Time/tree/master/time-android\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkizitonwose%2Ftime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkizitonwose%2Ftime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkizitonwose%2Ftime/lists"}