55[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-blue.svg )] ( https://opensource.org/licenses/MIT )
66<a target =" _blank " href =" https://www.paypal.me/GuepardoApps " title =" Donate using PayPal " ><img src =" https://img.shields.io/badge/paypal-donate-blue.svg " /></a >
77
8- [ ![ Build] ( https://img.shields.io/badge/build-success-green.svg )] ( https://github.com/TimeXt/TimeXt-Kotlin/blob/master/releases/lib-2018-10-20-1.aar )
9- [ ![ Version] ( https://img.shields.io/badge/version-v0.1.0.181020-blue.svg )] ( https://github.com/TimeXt/TimeXt-Kotlin/tree/master/releases/ )
8+ [ ![ Build] ( https://img.shields.io/badge/build-success-green.svg )] ( https://github.com/TimeXt/TimeXt-Kotlin/blob/master/releases/lib-2018-10-28-1.aar )
9+ [ ![ Version] ( https://img.shields.io/badge/version-v0.2.0.181028-blue.svg )] ( https://github.com/TimeXt/TimeXt-Kotlin/tree/master/releases/ )
10+ [ ![ CodeCoverage] ( https://img.shields.io/badge/codeCoverage-71-orange.svg )] ( https://github.com/TimeXt/TimeXt-Kotlin/tree/master/ )
1011
1112First of all many thanks to [ Kizitonwose] ( https://github.com/kizitonwose/Time ) for the original idea and already awesome library!
1213
13- This library shall help to reduce code like
14+ This library shall help to reduce code like...
1415
1516``` kotlin
1617val dayInMillis = 24 * 60 * 60 * 1000 // Represent a day in milliSeconds
18+
19+ ```
20+
21+ ... or allow code like
22+
23+ ``` kotlin
24+ val date1 = java.sql.Date (1540719288642 )
25+ val date2 = java.sql.Date (1540722888642 )
26+
27+ val difference = date2 - date1
28+
1729```
1830
1931## How to use
2032
2133### Basics
2234
2335``` kotlin
24- val oneWeek = 1 .weeks // Type is Interval<Week>
25- val threeDays = 3 .days // Type is Interval<Day>
26- val elevenHours = 11 .hours // Type is Interval<Hour>
27- val sixMinutes = 6 .minutes // Type is Interval<Minute>
28- val fiftySeconds = 50 .seconds // Type is Interval<Second>
29- val hundredMilliSeconds = 100 .milliSeconds // Type is Interval<MilliSecond>
30- val fiveMicroSeconds = 5 .microSeconds // Type is Interval<MicroSecond>
31- val oneNanoSecond = 1 .nanoSeconds // Type is Interval<NanoSecond>
32- val onePicoSecond = 1 .picoSeconds // Type is Interval<PicoSecond>
33-
34- val oneDayInMillis = 1 .days.inMilliSeconds // Converts one day into milliseconds
35- val twoWeeksInHours = 2 .weeks.inHours // Converts two weeks into hours
36-
37- val duration = 1 .days + 11 .hours + 35 .minutes + 15 .seconds - 250 .milliSeconds
36+ // Type is TimeXt
37+ val oneWeek = 1 .weeks
38+ val threeDays = 3 .days
39+ val elevenHours = 11 .hours
40+ val sixMinutes = 6 .minutes
41+ val fiftySeconds = 50 .seconds
42+ val hundredMilliseconds = 100 .milliseconds
43+
44+ val oneDayInMillis = 1 .days.inMilliSeconds // Returns one day in milliseconds
45+ val twoWeeksInHours = 2 .weeks.inHours // Returns two weeks in hours
46+
47+ // Converts the existing 3h-Class into a 180min-Class
48+ val threeHoursToMinutes = 3 .hours.toMinutes()
49+ // 3.hours === TimeXt(3, TimeXtUnit.hour)
50+ // 180.minutes === TimeXt(180, TimeXtUnit.minute)
51+
52+ val duration = 1 .days + 11 .hours + 35 .minutes + 15 .seconds - 250 .milliseconds
3853val multipliedDuration = 1.5 * duration
3954val dividedDuration = duration / 2
4055
4156val isLessTrue = 1 .days < 1 .weeks // True
4257val isLessFalse = 24 .hours < 360 .minutes // False
4358
4459val isBiggerTrue = 5 .minutes > 30 .seconds // True
45- val isBiggerFalse = 500 .nanoSeconds > 1 .minutes // False
60+ val isBiggerFalse = 500 .minutes > 5 .weeks // False
4661
4762```
4863
@@ -52,16 +67,19 @@ val isBiggerFalse = 500.nanoSeconds > 1.minutes // False
5267// Calendar
5368val inOneHour = Calendar .getInstance() + 1 .hours
5469val threeDaysAgo = Calendar .getInstance() - 3 .days
70+ val difference = calendar1 - calendar2 // you can perform minus on calendar and get a TimeXt-object
5571
5672// Sql Date
57- val sqlDate: java.sql.Date = java.sql.Date ()
58- sqlDate + 5 .minutes
59- sqlDate - 30 .seconds
73+ var sqlDate: java.sql.Date = java.sql.Date ()
74+ sqlDate = sqlDate + 5 .minutes
75+ sqlDate = sqlDate - 30 .seconds
76+ val difference = sqlDate1 - sqlDate2 // you can perform minus on java.sql.Date and get a TimeXt-object
6077
6178// Util Date
6279val utilDate: java.util.Date = java.util.Date ()
63- utilDate + 5 .minutes
64- utilDate - 30 .seconds
80+ utilDate = utilDate + 5 .minutes
81+ utilDate = utilDate - 30 .seconds
82+ val difference = utilDate1 - utilDate2 // you can perform minus on java.util.Date and get a TimeXt-object
6583
6684// Timer
6785val timer = Timer ()
@@ -70,7 +88,7 @@ timer.schedule(10.seconds) {
7088}
7189```
7290
73- The library also includes extensions for Android's Handler class, this is only available if you compile the "lib -android" module.
91+ The library also includes extensions for Android's Handler class, this is only available if you compile the "timext -android" module.
7492
7593``` kotlin
7694val handler = Handler ()
@@ -79,27 +97,29 @@ handler.postDelayed({
7997}, 2 .minutes)
8098```
8199
82- ### Custom IDateTimeUnit
100+ ### Custom TimeXtUnit
83101
84- If you would like to have your own date time unit, implement it as followed:
102+ If you would like to have your own timext unit, implement it as followed:
85103
86104``` kotlin
87- class Year : IDateTimeUnit {
88- // amount of seconds in one year
89- override val dateTimeIntervalRatio = 365.0 * 24.0 * 60.0 * 60.0
90- }
105+ val year: Double = 365 * 24 * 60 * 60 * 1e3
91106
92107// Add also some extensions:
93- val Number .years: Interval < Year >
94- get() = Interval (this )
108+ val Number .years: TimeXt
109+ get() = TimeXt (this .toDouble(), year )
95110
96- val Interval <IDateTimeUnit >.inYears: Interval <Year >
97- get() = converted()
111+ val TimeXt .inYears: TimeXt
112+ get() = this .value * this .unit / year
113+
114+ fun toYears (): TimeXt {
115+ return TimeXt (this .inYears, year)
116+ }
98117
99118// Use it like:
100- val threeYears = Interval < Year >( 3 )
119+ val threeYears = TimeXt ( 3 , year )
101120val oneYear = 1 .years
102121val daysInYear = 365 .days.inYears
122+ val yearsFromDays = 365 .days.toYears()
103123
104124```
105125
0 commit comments