Skip to content

Commit 3862529

Browse files
committed
Add README.md
1 parent e5baf40 commit 3862529

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

README.md

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
# Time
2+
3+
This library is made for you if you have ever written something like this:
4+
5+
```kotlin
6+
val duration = 10 * 1000
7+
```
8+
to represent a duration of 10 seconds(in milliseconds) because most methods in Kotlin/Java take duration parameters in milliseconds.
9+
10+
## Usage
11+
12+
### Showcase
13+
14+
```kotlin
15+
val tenSeconds = 10.seconds
16+
val fiveMinutes = 5.minutes
17+
val twoHours = 2.hours
18+
val threeDays = 3.days
19+
val tenMinutesFromNow = Calender.getInstance() + 10.minutes
20+
val tenSecondsInMilliseconds = 10.seconds.inMilliseconds
21+
```
22+
23+
### Basics
24+
25+
The main advantage of the library is that all time units are *strongly-typed*. So, for example:
26+
27+
```kotlin
28+
val tenMinutes = 10.minutes
29+
```
30+
31+
In the example above, `tenMinutes` will be of type `Interval<Minute>`. There are seven time units available, from nanoseconds to days:
32+
33+
```kotlin
34+
val tenNanoseconds = 10.nanoseconds
35+
// type is Interval<Nanosecond>
36+
```
37+
```kotlin
38+
val tenMicroseconds = 10.microseconds
39+
// type is Interval<Microsecond>
40+
```
41+
```kotlin
42+
val tenMilliseconds = 10.milliseconds
43+
// type is Interval<Millisecond>
44+
```
45+
```kotlin
46+
val tenSeconds = 10.seconds
47+
// type is Interval<Second>
48+
```
49+
```kotlin
50+
val tenMinutes = 10.minutes
51+
// type is Interval<Minute>
52+
```
53+
```kotlin
54+
val tenHours = 10.hours
55+
// type is Interval<Hour>
56+
```
57+
```kotlin
58+
val tenDays = 10.days
59+
// type is Interval<Day>
60+
```
61+
62+
### Operations
63+
64+
You can perform all basic arithmetic operations on time intervals, even of different units:
65+
66+
```kotlin
67+
val duration = 10.minutes + 15.seconds - 3.minutes + 2.hours // Interval<Minute>
68+
val doubled = duration * 2
69+
70+
val seconds = 10.seconds + 3.minutes // Interval<Second>
71+
```
72+
73+
You can also use these operations on `Calender`:
74+
75+
```kotlin
76+
val twoHoursLater = Calendar.getInstance() + 2.hours
77+
```
78+
79+
### Conversions
80+
81+
Time intervals are easily convertible:
82+
83+
```kotlin
84+
val twoMinutesInSeconds = 2.minutes.inSeconds // Interval<Second>
85+
val fourDaysInHours = 4.days.inHours // Interval<Hour>
86+
```
87+
88+
You can also use the `converted()` method, although you would rarely need to:
89+
90+
```kotlin
91+
val tenMinutesInSeconds: Interval<Second> = 10.minutes.converted()
92+
```
93+
94+
### Comparison
95+
96+
You can compare different time units as well:
97+
98+
```kotlin
99+
50.seconds < 2.hours // true
100+
120.minutes == 2.hours // true
101+
100.milliseconds > 2.seconds // false
102+
48.hours in 2.days // true
103+
```
104+
105+
### Creating your own time units
106+
107+
If, for some reason, you need to create your own time unit, that's super easy to do:
108+
109+
```kotlin
110+
class Week : TimeUnit {
111+
// number of seconds in one week
112+
override val timeIntervalRatio = 604800.0
113+
}
114+
```
115+
116+
Now you can use it like any other time unit:
117+
118+
```kotlin
119+
val fiveWeeks = Interval<Week>(5)
120+
```
121+
122+
For the sake of convenience, don't forget to write those handy extensions:
123+
124+
```kotlin
125+
class Week : TimeUnit {
126+
override val timeIntervalRatio = 604800.0
127+
}
128+
129+
val Number.weeks: Interval<Week>
130+
get() = Interval(this.toDouble())
131+
132+
val Interval<TimeUnit>.inWeeks: Interval<Week>
133+
get() = converted()
134+
```
135+
Now you can write:
136+
137+
```kotlin
138+
val fiveWeeks = 5.weeks // Interval<Week>
139+
```
140+
You can also easily convert to weeks:
141+
142+
```kotlin
143+
val valueInWeeks = 14.days.inWeeks // Interval<Week>
144+
```
145+
146+
### Extras
147+
148+
The library includes some handy extensions for some classes:
149+
150+
```kotlin
151+
val now = Calendar.getInstance()
152+
val sixHoursLater = now + 6.hours
153+
val fourDaysAgo = now - 4.days
154+
```
155+
156+
```kotlin
157+
val timer = Timer()
158+
timer.schedule(10.seconds) {
159+
println("This block will be called in 10 seconds")
160+
}
161+
```
162+
163+
The library also includes extensions for Android's Handler class, this is only available if you compile the "time-android" module.
164+
165+
```kotlin
166+
val handler = Handler()
167+
handler.postDelayed({
168+
Log.i("TAG", "This will be printed to the Logcat in 2 minutes")
169+
}, 2.minutes)
170+
```
171+
More extensions will be added to the library in the future.
172+
173+
### Conversion safety everywhere
174+
175+
For 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.
176+
177+
If such methods aren't frequently used, you can still benefit from the conversion safety that comes with this library.
178+
179+
An example method in a third-party library that does something after a delay period in milliseconds:
180+
181+
```kotlin
182+
class Person {
183+
fun doSomething(delayMillis: Long) {
184+
// method body
185+
}
186+
}
187+
```
188+
189+
To call the method above with a value of 5 minutes, one would usually write:
190+
191+
```kotlin
192+
val person = Person()
193+
person.doSomething(5 * 60 * 1000)
194+
```
195+
196+
The above line can be written in a safer and clearer way using this library:
197+
198+
```kotlin
199+
val person = Person()
200+
person.doSomething(5.minutes.inMilliseconds.longValue)
201+
```
202+
203+
If the method is frequently used, you can write an extension function:
204+
205+
```kotlin
206+
fun Person.doSomething(delay: Interval<TimeUnit>) {
207+
doSomething(delay.inMilliseconds.longValue)
208+
}
209+
```
210+
Now you can write:
211+
212+
```kotlin
213+
val person = Person()
214+
person.doSomething(5.minutes)
215+
```
216+
217+
## Installation
218+
219+
Add the JitPack repository to your `build.gradle`:
220+
221+
```groovy
222+
allprojects {
223+
repositories {
224+
maven { url "https://jitpack.io" }
225+
}
226+
}
227+
```
228+
229+
Add the dependency to your `build.gradle`:
230+
231+
- For **non-Android** projects:
232+
233+
```groovy
234+
dependencies {
235+
compile 'com.github.kizitonwose.time:time:1.0.0'
236+
}
237+
```
238+
239+
- For **Android** projects:
240+
241+
```groovy
242+
dependencies {
243+
compile 'com.github.kizitonwose.time:time-android:1.0.0'
244+
}
245+
```
246+
247+
## Contributing
248+
The 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]" modue while Android extensions belong in "[time-android][time-android-module-url]" module.
249+
250+
251+
## Inspiration
252+
Time was inspired by a Swift library of the same name - [Time][time-swift-url].
253+
254+
The 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.
255+
256+
257+
## License
258+
Time is distributed under the MIT license. [See LICENSE](https://github.com/kizitonwose/Time/blob/master/LICENSE.md) for details.
259+
260+
261+
[time-swift-url]: https://github.com/dreymonde/Time
262+
[time-core-module-url]: https://github.com/kizitonwose/Time/tree/master/time
263+
[time-android-module-url]: https://github.com/kizitonwose/Time/tree/master/time-android

0 commit comments

Comments
 (0)