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

Commit 5d70c28

Browse files
author
Jonas Schubert
committed
adds new long extensions to format long in human readable string
1 parent cd3205f commit 5d70c28

File tree

4 files changed

+169
-6
lines changed

4 files changed

+169
-6
lines changed

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
[![API](https://img.shields.io/badge/API-14+-blue.svg)](https://android-arsenal.com/api?level=14)
55
[![](https://jitpack.io/v/TimeXt/TimeXt-Kotlin.svg)](https://jitpack.io/#TimeXt/TimeXt-Kotlin)
66

7-
[![Version](https://img.shields.io/badge/version-v0.4.1-blue.svg)](https://github.com/TimeXt/TimeXt-Kotlin/releases/tag/v0.4.1)
7+
[![Version](https://img.shields.io/badge/version-v0.5.0-blue.svg)](https://github.com/TimeXt/TimeXt-Kotlin/releases/tag/0.5.0)
88
[![Build](https://img.shields.io/badge/build-success-green.svg)](timext)
9-
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-61-orange.svg)](timext)
9+
[![CodeCoverage](https://img.shields.io/badge/codeCoverage-64-orange.svg)](timext)
1010

1111
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
1212
[![Paypal](https://img.shields.io/badge/paypal-donate-blue.svg)](https://www.paypal.me/GuepardoApps)
@@ -103,6 +103,15 @@ handler.postDelayed({
103103
}, 2.minutes)
104104
```
105105

106+
Since version 0.5.0 TimeXt has new extensions for the long type to display this number value in a human readable string format.
107+
108+
```kotlin
109+
val readableStringFromMilliseconds = 34325055574.formatMilliseconds // 56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds
110+
val readableStringFromSeconds = 4350554L.formatSeconds // 7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds
111+
val readableStringFromMinutes = 432555L.formatMinutes // 42 weeks, 6 days, 9 hours, 15 minutes
112+
val readableStringFromHours = 4574L.formatHours // 27 weeks, 1 day, 14 hours
113+
```
114+
106115
### Custom TimeXtUnit
107116

108117
If you would like to have your own timext unit, implement it as followed:
@@ -147,15 +156,15 @@ Add the dependency to your `build.gradle`:
147156

148157
```groovy
149158
dependencies {
150-
implementation 'com.github.TimeXt.TimeXt-Kotlin:timext:v0.4.1'
159+
implementation 'com.github.TimeXt.TimeXt-Kotlin:timext:0.5.0'
151160
}
152161
```
153162

154163
- For **Android** projects:
155164

156165
```groovy
157166
dependencies {
158-
implementation 'com.github.TimeXt.TimeXt-Kotlin:timext-android:v0.4.1'
167+
implementation 'com.github.TimeXt.TimeXt-Kotlin:timext-android:0.5.0'
159168
}
160169
```
161170

@@ -166,3 +175,27 @@ dependencies {
166175
## License
167176

168177
TimeXt-Kotlin is distributed under the MIT license. [See LICENSE](LICENSE.md) for details.
178+
179+
```
180+
MIT License
181+
182+
Copyright (c) 2018 - 2019 GuepardoApps (Jonas Schubert)
183+
184+
Permission is hereby granted, free of charge, to any person obtaining a copy
185+
of this software and associated documentation files (the "Software"), to deal
186+
in the Software without restriction, including without limitation the rights
187+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
188+
copies of the Software, and to permit persons to whom the Software is
189+
furnished to do so, subject to the following conditions:
190+
191+
The above copyright notice and this permission notice shall be included in all
192+
copies or substantial portions of the Software.
193+
194+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
195+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
196+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
197+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
199+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
200+
SOFTWARE.
201+
```

timext-android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdkVersion 14
1414
targetSdkVersion 28
15-
versionCode 401
16-
versionName "0.4.1"
15+
versionCode 500
16+
versionName "0.5.0"
1717
}
1818

1919
buildTypes {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.guepardoapps.timext.kotlin.extensions
2+
3+
val Long.formatMilliseconds: String
4+
get() {
5+
val map: Map<String, Pair<Long, Int>> = mapOf(
6+
"week" to Pair(7 * 24 * 60 * 60 * 1000L, Int.MAX_VALUE),
7+
"day" to Pair(24 * 60 * 60 * 1000L, 7),
8+
"hour" to Pair(60 * 60 * 1000L, 24),
9+
"minute" to Pair(60 * 1000L, 60),
10+
"second" to Pair(1000L, 60),
11+
"millisecond" to Pair(1L, 1000)
12+
)
13+
14+
val stringArray = map
15+
.map { item ->
16+
if (((this / item.value.first) % item.value.second) > 0) "${((this / item.value.first) % item.value.second)} ${item.key}${if (((this / item.value.first) % item.value.second) > 1) "s" else ""}" else ""
17+
}
18+
.filter { x -> x.isNotEmpty() }
19+
20+
return if (stringArray.isNotEmpty()) stringArray.joinToString(", ") else "0 millisecond"
21+
}
22+
23+
val Long.formatSeconds: String
24+
get() {
25+
val map: Map<String, Pair<Long, Int>> = mapOf(
26+
"week" to Pair(7 * 24 * 60 * 60 * 1L, Int.MAX_VALUE),
27+
"day" to Pair(24 * 60 * 60 * 1L, 7),
28+
"hour" to Pair(60 * 60 * 1L, 24),
29+
"minute" to Pair(60 * 1L, 60),
30+
"second" to Pair(1L, 60)
31+
)
32+
33+
val stringArray = map
34+
.map { item ->
35+
if (((this / item.value.first) % item.value.second) > 0) "${((this / item.value.first) % item.value.second)} ${item.key}${if (((this / item.value.first) % item.value.second) > 1) "s" else ""}" else ""
36+
}
37+
.filter { x -> x.isNotEmpty() }
38+
39+
return if (stringArray.isNotEmpty()) stringArray.joinToString(", ") else (this * 1000).formatMilliseconds
40+
}
41+
42+
val Long.formatMinutes: String
43+
get() {
44+
val map: Map<String, Pair<Long, Int>> = mapOf(
45+
"week" to Pair(7 * 24 * 60 * 1L, Int.MAX_VALUE),
46+
"day" to Pair(24 * 60 * 1L, 7),
47+
"hour" to Pair(60 * 1L, 24),
48+
"minute" to Pair(1L, 60)
49+
)
50+
51+
val stringArray = map
52+
.map { item ->
53+
if (((this / item.value.first) % item.value.second) > 0) "${((this / item.value.first) % item.value.second)} ${item.key}${if (((this / item.value.first) % item.value.second) > 1) "s" else ""}" else ""
54+
}
55+
.filter { x -> x.isNotEmpty() }
56+
57+
return if (stringArray.isNotEmpty()) stringArray.joinToString(", ") else (this * 60).formatSeconds
58+
}
59+
60+
val Long.formatHours: String
61+
get() {
62+
val map: Map<String, Pair<Long, Int>> = mapOf(
63+
"week" to Pair(7 * 24 * 1L, Int.MAX_VALUE),
64+
"day" to Pair(24 * 1L, 7),
65+
"hour" to Pair(1L, 24)
66+
)
67+
68+
val stringArray = map
69+
.map { item ->
70+
if (((this / item.value.first) % item.value.second) > 0) "${((this / item.value.first) % item.value.second)} ${item.key}${if (((this / item.value.first) % item.value.second) > 1) "s" else ""}" else ""
71+
}
72+
.filter { x -> x.isNotEmpty() }
73+
74+
return if (stringArray.isNotEmpty()) stringArray.joinToString(", ") else (this * 60).formatMinutes
75+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.github.guepardoapps.timext.kotlin.extensions
2+
3+
import org.junit.Assert.*
4+
import org.junit.Test
5+
6+
class LongExtensionsTests {
7+
8+
@Test
9+
fun `formatMilliseconds should work as expected`() {
10+
// Arrange
11+
val expected = "56 weeks, 5 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds"
12+
13+
// Act
14+
val actual = 34325055574.formatMilliseconds
15+
16+
// Assert
17+
assertEquals(expected, actual)
18+
}
19+
20+
@Test
21+
fun `formatSeconds should work as expected`() {
22+
// Arrange
23+
val expected = "7 weeks, 1 day, 8 hours, 29 minutes, 14 seconds"
24+
25+
// Act
26+
val actual = 4350554L.formatSeconds
27+
28+
// Assert
29+
assertEquals(expected, actual)
30+
}
31+
32+
@Test
33+
fun `formatMinutes should work as expected`() {
34+
// Arrange
35+
val expected = "42 weeks, 6 days, 9 hours, 15 minutes"
36+
37+
// Act
38+
val actual = 432555L.formatMinutes
39+
40+
// Assert
41+
assertEquals(expected, actual)
42+
}
43+
44+
@Test
45+
fun `formatHours should work as expected`() {
46+
// Arrange
47+
val expected = "27 weeks, 1 day, 14 hours"
48+
49+
// Act
50+
val actual = 4574L.formatHours
51+
52+
// Assert
53+
assertEquals(expected, actual)
54+
}
55+
}

0 commit comments

Comments
 (0)