Skip to content
This repository was archived by the owner on Jan 22, 2023. It is now read-only.

Commit 69d0990

Browse files
author
Jonas Schubert
committed
removed year, fixed date extensions
1 parent 73c56c6 commit 69d0990

File tree

10 files changed

+131
-78
lines changed

10 files changed

+131
-78
lines changed

README.md

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,59 @@
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/develop/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/develop/releases/)
8+
[![Build](https://img.shields.io/badge/build-success-green.svg)](https://github.com/TimeXt/TimeXt-Kotlin/blob/develop/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/develop/releases/)
10+
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-71-orange.svg)](https://github.com/TimeXt/TimeXt-Kotlin/tree/develop/)
1011

1112
First 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
1617
val 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
3853
val multipliedDuration = 1.5 * duration
3954
val dividedDuration = duration / 2
4055

4156
val isLessTrue = 1.days < 1.weeks // True
4257
val isLessFalse = 24.hours < 360.minutes // False
4358

4459
val 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
5368
val inOneHour = Calendar.getInstance() + 1.hours
5469
val 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
6279
val 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
6785
val 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
7694
val 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)
101120
val oneYear = 1.years
102121
val daysInYear = 365.days.inYears
122+
val yearsFromDays = 365.days.toYears()
103123

104124
```
105125

timext-android/timext-android.iml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,30 @@
109109
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
110110
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
111111
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/aapt_friendly_merged_manifests" />
112+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotation_processor_list" />
113+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations_typedef_file" />
114+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations_zip" />
112115
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check-manifest" />
116+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/consumer_proguard_file" />
113117
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
118+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javac" />
119+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
120+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_and_local_jars_jni" />
121+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/library_assets" />
114122
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint_jar" />
115123
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/merged_manifests" />
124+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/packaged-classes" />
116125
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/packaged_res" />
117126
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/public_res" />
118127
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
119128
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
129+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shader_assets" />
130+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
120131
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
132+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
133+
<excludeFolder url="file://$MODULE_DIR$/build/kotlin" />
121134
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
135+
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
122136
</content>
123137
<orderEntry type="jdk" jdkName="Android API 28 Platform" jdkType="Android SDK" />
124138
<orderEntry type="sourceFolder" forTests="false" />

timext/src/main/kotlin/com/github/guepardoapps/timext/kotlin/TimeXt.kt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package com.github.guepardoapps.timext.kotlin
22

33
class TimeXt(var value: Double, var unit: Double) {
44

5-
val inYears: Double
6-
get() = this.value * this.unit / TimeXtUnit.year
7-
85
val inWeeks: Double
96
get() = this.value * this.unit / TimeXtUnit.week
107

@@ -23,6 +20,30 @@ class TimeXt(var value: Double, var unit: Double) {
2320
val inMilliseconds: Double
2421
get() = this.value * this.unit / TimeXtUnit.millisecond
2522

23+
fun toWeeks(): TimeXt {
24+
return TimeXt(this.inWeeks, TimeXtUnit.week)
25+
}
26+
27+
fun toDays(): TimeXt {
28+
return TimeXt(this.inDays, TimeXtUnit.day)
29+
}
30+
31+
fun toHours(): TimeXt {
32+
return TimeXt(this.inHours, TimeXtUnit.hour)
33+
}
34+
35+
fun toMinutes(): TimeXt {
36+
return TimeXt(this.inMinutes, TimeXtUnit.minute)
37+
}
38+
39+
fun toSeconds(): TimeXt {
40+
return TimeXt(this.inSeconds, TimeXtUnit.second)
41+
}
42+
43+
fun toMilliseconds(): TimeXt {
44+
return TimeXt(this.inMilliseconds, TimeXtUnit.millisecond)
45+
}
46+
2647
operator fun plus(timeXt: TimeXt): TimeXt {
2748
val newValue = ((this.inMilliseconds + timeXt.inMilliseconds) / this.unit) * TimeXtUnit.millisecond
2849
return TimeXt(newValue, this.unit)

timext/src/main/kotlin/com/github/guepardoapps/timext/kotlin/TimeXtUnit.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.github.guepardoapps.timext.kotlin
22

33
object TimeXtUnit {
4-
val year: Double = 365 * 24 * 60 * 60 * 1e3
5-
64
val week: Double = 7 * 24 * 60 * 60 * 1e3
75

86
val day: Double = 24 * 60 * 60 * 1e3

timext/src/main/kotlin/com/github/guepardoapps/timext/kotlin/extensions/CalendarExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.util.*
66

77
operator fun Calendar.plus(timeXt: TimeXt): Calendar {
88
val newCalendar = clone() as Calendar
9-
newCalendar.add(Calendar.MILLISECOND, timeXt.inMilliseconds.toInt())
9+
newCalendar.timeInMillis += timeXt.inMilliseconds.toLong()
1010
return newCalendar
1111
}
1212

timext/src/main/kotlin/com/github/guepardoapps/timext/kotlin/extensions/DateExtensions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import com.github.guepardoapps.timext.kotlin.TimeXt
44
import com.github.guepardoapps.timext.kotlin.TimeXtUnit
55

66
operator fun java.sql.Date.plus(timeXt: TimeXt): java.sql.Date {
7-
this.time + timeXt.inMilliseconds.toLong()
7+
this.time += timeXt.inMilliseconds.toLong()
88
return this
99
}
1010

1111
operator fun java.sql.Date.minus(timeXt: TimeXt): java.sql.Date {
12-
this + (timeXt * -1)
12+
this.time -= timeXt.inMilliseconds.toLong()
1313
return this
1414
}
1515

@@ -19,12 +19,12 @@ operator fun java.sql.Date.minus(date: java.sql.Date): TimeXt {
1919
}
2020

2121
operator fun java.util.Date.plus(timeXt: TimeXt): java.util.Date {
22-
this.time + timeXt.inMilliseconds.toLong()
22+
this.time += timeXt.inMilliseconds.toLong()
2323
return this
2424
}
2525

2626
operator fun java.util.Date.minus(timeXt: TimeXt): java.util.Date {
27-
this + (timeXt * -1)
27+
this.time -= timeXt.inMilliseconds.toLong()
2828
return this
2929
}
3030

timext/src/main/kotlin/com/github/guepardoapps/timext/kotlin/extensions/NumberExtensions.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package com.github.guepardoapps.timext.kotlin.extensions
33
import com.github.guepardoapps.timext.kotlin.TimeXt
44
import com.github.guepardoapps.timext.kotlin.TimeXtUnit
55

6-
val Number.years: TimeXt
7-
get() = TimeXt(this.toDouble(), TimeXtUnit.year)
8-
96
val Number.weeks: TimeXt
107
get() = TimeXt(this.toDouble(), TimeXtUnit.week)
118

timext/src/test/kotlin/com/github/guepardoapps/timext/kotlin/TimeXtTests.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ class TimeXtTests {
88

99
@Test
1010
fun `All comparisons should work as expected`() {
11-
assertEquals(2.years, 730.days)
12-
assertTrue(3.years > 1094.days)
13-
assertTrue(4.years < 1461.days)
14-
assertNotEquals(2.years, 729.days)
15-
1611
assertEquals(2.weeks, 14.days)
1712
assertTrue(3.weeks > 20.days)
1813
assertTrue(4.weeks < 29.days)
@@ -41,7 +36,6 @@ class TimeXtTests {
4136

4237
@Test
4338
fun `All conversions should work as expected`() {
44-
assertTrue(365.days.inYears == 1.0)
4539
assertTrue(4.weeks.inDays == 28.0)
4640
assertTrue(7.days.inWeeks == 1.0)
4741
assertTrue(2.5.days.inHours == 60.0)
@@ -54,9 +48,22 @@ class TimeXtTests {
5448
assertTrue(5.75.seconds.inMilliseconds == 5750.0)
5549
}
5650

51+
@Test
52+
fun `All casts should work as expected`() {
53+
assertTrue(4.weeks.toDays() == 28.days)
54+
assertTrue(7.days.toWeeks() == 1.weeks)
55+
assertTrue(2.5.days.toHours() == 60.hours)
56+
assertTrue(5.25.hours.toMinutes() == 315.minutes)
57+
assertTrue(72.hours.toDays() == 3.days)
58+
assertTrue(360.minutes.toHours() == 6.hours)
59+
assertTrue(15.5.minutes.toSeconds() == 930.seconds)
60+
assertTrue(7200.seconds.toHours() == 2.hours)
61+
assertTrue(10800.seconds.toDays() == 0.125.days)
62+
assertTrue(5.75.seconds.toMilliseconds() == 5750.milliseconds)
63+
}
64+
5765
@Test
5866
fun `Plus operator should work as expected`() {
59-
assertTrue((1.years + 365.days) == 2.years)
6067
assertTrue((2.weeks + 7.days) == 3.weeks)
6168
assertTrue((2.5.days + 12.hours) == 3.days)
6269
assertTrue((2.25.hours + 45.minutes) == 3.hours)
@@ -66,7 +73,6 @@ class TimeXtTests {
6673

6774
@Test
6875
fun `Minus operator should work as expected`() {
69-
assertTrue((2.years - 365.days) == 1.years)
7076
assertTrue((2.weeks - 7.days) == 1.weeks)
7177
assertTrue((2.5.days - 12.hours) == 2.days)
7278
assertTrue((2.25.hours - 15.minutes) == 2.hours)
@@ -76,7 +82,6 @@ class TimeXtTests {
7682

7783
@Test
7884
fun `Multiplication operator should work as expected`() {
79-
assertTrue((2.years * 2) == 4.years)
8085
assertTrue((2.weeks * 2) == 4.weeks)
8186
assertTrue((2.5.days * 3) == 7.5.days)
8287
assertTrue((2.25.hours * 4) == 9.hours)
@@ -87,7 +92,6 @@ class TimeXtTests {
8792

8893
@Test
8994
fun `Division operator should work as expected`() {
90-
assertTrue((2.years / 2) == 1.years)
9195
assertTrue((2.weeks / 2) == 1.weeks)
9296
assertTrue((1.5.days / 3) == 0.5.days)
9397
assertTrue((4.hours / 4) == 1.hours)
@@ -98,9 +102,6 @@ class TimeXtTests {
98102

99103
@Test
100104
fun `Increment operator should work as expected`() {
101-
var actualYears = 2.years
102-
assertTrue(++actualYears == 3.years)
103-
104105
var actualWeeks = 2.weeks
105106
assertTrue(++actualWeeks == 3.weeks)
106107

@@ -122,9 +123,6 @@ class TimeXtTests {
122123

123124
@Test
124125
fun `Decrement operator should work as expected`() {
125-
var actualYears = 2.years
126-
assertTrue(--actualYears == 1.years)
127-
128126
var actualWeeks = 2.weeks
129127
assertTrue(--actualWeeks == 1.weeks)
130128

@@ -156,4 +154,9 @@ class TimeXtTests {
156154
assertTrue(55.milliseconds in 60.milliseconds)
157155
assertFalse(60.milliseconds in 55.milliseconds)
158156
}
157+
158+
@Test
159+
fun `HashCode should work as expected`() {
160+
assertEquals(1078689792, 55.milliseconds.hashCode())
161+
}
159162
}

0 commit comments

Comments
 (0)