-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Datehumanizertests and implementation #1
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="1.0" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test/main/kotlin" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.gradle" /> | ||
<excludeFolder url="file://$MODULE_DIR$/build" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" exported="" name="Gradle: kotlin-stdlib-0.7.270" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: kotlin-runtime-0.7.270" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: spek-0.1.110" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: hamcrest-core-1.3" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: junit-4.11" level="project" /> | ||
</component> | ||
</module> | ||
|
||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module external.linked.project.path="E:\Users\christiaan\IdeaProjects\Lessthandot\Humanizer.jvm" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="1.0" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test/main/kotlin" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.gradle" /> | ||
<excludeFolder url="file://$MODULE_DIR$/build" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" exported="" name="Gradle: kotlin-stdlib-0.7.270" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: spek-0.1.118" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: kotlin-runtime-0.7.270" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: hamcrest-core-1.3" level="project" /> | ||
<orderEntry type="library" exported="" name="Gradle: junit-4.11" level="project" /> | ||
</component> | ||
</module> | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.humanizer.jvm | ||
|
||
import java.util.Date | ||
import java.util.concurrent.TimeUnit | ||
import org.humanizer.jvm.formatters.dateHumanizer.DefaultDateFormatter | ||
import java.util.GregorianCalendar | ||
import java.util.Calendar | ||
|
||
fun Date.humanize(toCompareAgainst: Date): String { | ||
val formatter = DefaultDateFormatter() | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.MILLISECONDS))<=500) | ||
return "now" | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.SECONDS)) < 60) | ||
return formatter.secondFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.SECONDS)) < 120) | ||
return formatter.minuteFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.MINUTES)) < 60) | ||
return formatter.minuteFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.MINUTES)) < 120) | ||
return formatter.hourFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.HOURS)) < 24) | ||
return formatter.hourFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.HOURS)) < 48) | ||
return formatter.dayFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.DAYS)) < 29) | ||
return formatter.dayFormatter(this,toCompareAgainst) | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.DAYS)) > 28 && Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.DAYS)) < 32) | ||
{ | ||
val cal = GregorianCalendar() | ||
cal.setTime(this) | ||
if(cal.getActualMaximum(Calendar.DAY_OF_MONTH) <= Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.DAYS))) | ||
return formatter.dayFormatter(this,toCompareAgainst) | ||
if(cal.getActualMaximum(Calendar.DAY_OF_MONTH) > 29) | ||
return formatter.monthFormatter(this,toCompareAgainst) | ||
} | ||
if(Math.abs(this.getDateDiff(toCompareAgainst, TimeUnit.DAYS)) < 345) | ||
return formatter.monthFormatter(this,toCompareAgainst) | ||
return formatter.yearFormatter(this,toCompareAgainst) | ||
} | ||
|
||
// based on this SO answer http://stackoverflow.com/a/10650881 | ||
fun Date.getDateDiff(date2:Date, timeUnit:TimeUnit): Long | ||
{ | ||
val diffInMillies = this.getTime() - date2.getTime() | ||
return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.humanizer.jvm.formatters.dateHumanizer | ||
|
||
import java.util.Date | ||
import org.humanizer.jvm.getDateDiff | ||
import java.util.concurrent.TimeUnit | ||
|
||
public class DefaultDateFormatter() { | ||
|
||
private var suffix: String = "ago" | ||
|
||
private fun makeSuffix(difference: Long) | ||
{ | ||
suffix = "ago" | ||
if (difference > 0) suffix = "from now" | ||
} | ||
|
||
fun secondFormatter(input: Date, toCompareAgainst: Date): String { | ||
var difference = input.getDateDiff(toCompareAgainst, TimeUnit.SECONDS) | ||
makeSuffix(difference) | ||
difference = Math.abs(difference) | ||
if (difference == 1.toLong()) { | ||
return "one second " + suffix | ||
} | ||
if (difference > 1.toLong() && difference < 60.toLong()) { | ||
return (difference).toString() + " seconds " + suffix | ||
} | ||
return "a minute " + suffix | ||
} | ||
|
||
fun minuteFormatter(input: Date,toCompareAgainst: Date): String { | ||
var difference = input.getDateDiff(toCompareAgainst, TimeUnit.MINUTES) | ||
makeSuffix(difference) | ||
difference = Math.abs(difference) | ||
if (difference == 1.toLong()) { | ||
return "a minute " + suffix | ||
} | ||
if (difference > 1.toLong() && difference < 45.toLong()) { | ||
return (difference).toString() + " minutes " + suffix | ||
} | ||
return "an hour " + suffix | ||
} | ||
|
||
fun hourFormatter(input: Date,toCompareAgainst: Date): String { | ||
var difference = input.getDateDiff(toCompareAgainst, TimeUnit.HOURS) | ||
makeSuffix(difference) | ||
difference = Math.abs(difference) | ||
if (difference == 1.toLong()) { | ||
return "an hour " + suffix | ||
} | ||
if (difference > 1.toLong() && difference < 24.toLong()) { | ||
return (difference).toString() + " hours " + suffix | ||
} | ||
return "a day " + suffix | ||
} | ||
|
||
fun dayFormatter(input: Date,toCompareAgainst: Date): String { | ||
var difference = input.getDateDiff(toCompareAgainst, TimeUnit.DAYS) | ||
makeSuffix(difference) | ||
if (difference == -1.toLong()) { | ||
return "yesterday" | ||
} | ||
if (difference == 1.toLong()) { | ||
return "tomorrow" | ||
} | ||
if (Math.abs(difference) > 1.toLong() && Math.abs(difference) < 31.toLong()) { | ||
return (Math.abs(difference)).toString() + " days " + suffix | ||
} | ||
return "one month " + suffix | ||
} | ||
|
||
fun monthFormatter(input: Date,toCompareAgainst: Date): String { | ||
var difference = getMonthsDifference(input, toCompareAgainst) | ||
makeSuffix(difference.toLong()) | ||
difference = Math.abs(difference) | ||
if (difference == 1) { | ||
return "one month " + suffix | ||
} | ||
if (Math.abs(difference) > 1.toLong() && Math.abs(difference) < 12.toLong()) { | ||
return (Math.abs(difference)).toString() + " months " + suffix | ||
} | ||
return "one year " + suffix | ||
} | ||
|
||
fun yearFormatter(input: Date,toCompareAgainst: Date): String { | ||
var difference = input.getYear() - toCompareAgainst.getYear() | ||
makeSuffix(difference.toLong()) | ||
difference = Math.abs(difference) | ||
if (difference == 1) { | ||
return "one year " + suffix | ||
} | ||
return (Math.abs(difference)).toString() + " years " + suffix | ||
} | ||
|
||
private fun getMonthsDifference(date1: Date, date2: Date) : Int | ||
{ | ||
val m1 = date1.getYear() * 12 + date1.getMonth() | ||
val m2 = date2.getYear() * 12 + date2.getMonth() | ||
return m1 - m2; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.humanizer.jvm.tests | ||
|
||
import org.junit.Test as Test | ||
import kotlin.test.assertEquals | ||
import java.util.ArrayList | ||
import java.util.HashMap | ||
import org.spek.* | ||
import org.humanizer.jvm.humanize | ||
import java.util.Calendar | ||
import java.util.Date | ||
import java.util.GregorianCalendar | ||
|
||
public class DateHumanizerTests(): Spek() { | ||
|
||
class ParamClass(val timeUnit: Int | ||
,val input: Int | ||
,val expected: String | ||
,val dateToUse: Date = GregorianCalendar(2014,Calendar.JANUARY, 5).getTime()){} | ||
{ | ||
|
||
val data = listOf( | ||
ParamClass(Calendar.SECOND, -1, "one second ago"), | ||
ParamClass(Calendar.SECOND, -10 , "10 seconds ago"), | ||
ParamClass(Calendar.SECOND, -59 , "59 seconds ago"), | ||
ParamClass(Calendar.SECOND, -60 , "a minute ago"), | ||
ParamClass(Calendar.SECOND, 1 , "one second from now"), | ||
ParamClass(Calendar.SECOND, 10 , "10 seconds from now"), | ||
ParamClass(Calendar.SECOND, 59 , "59 seconds from now"), | ||
ParamClass(Calendar.SECOND, 60 , "a minute from now"), | ||
ParamClass(Calendar.MINUTE,-1, "a minute ago"), | ||
ParamClass(Calendar.MINUTE,-10, "10 minutes ago"), | ||
ParamClass(Calendar.MINUTE,-44, "44 minutes ago"), | ||
ParamClass(Calendar.MINUTE,-60, "an hour ago"), | ||
ParamClass(Calendar.MINUTE,-119, "an hour ago"), | ||
ParamClass(Calendar.MINUTE,-120, "2 hours ago"), | ||
ParamClass(Calendar.MINUTE,1, "a minute from now"), | ||
ParamClass(Calendar.MINUTE,10, "10 minutes from now"), | ||
ParamClass(Calendar.MINUTE,44, "44 minutes from now"), | ||
ParamClass(Calendar.MINUTE,60, "an hour from now"), | ||
ParamClass(Calendar.MINUTE,119, "an hour from now"), | ||
ParamClass(Calendar.MINUTE,120, "2 hours from now"), | ||
ParamClass(Calendar.HOUR,-1, "an hour ago"), | ||
ParamClass(Calendar.HOUR,-10, "10 hours ago"), | ||
ParamClass(Calendar.HOUR,-23, "23 hours ago"), | ||
ParamClass(Calendar.HOUR,-24, "yesterday"), | ||
ParamClass(Calendar.HOUR,1, "an hour from now"), | ||
ParamClass(Calendar.HOUR,10, "10 hours from now"), | ||
ParamClass(Calendar.HOUR,23, "23 hours from now"), | ||
ParamClass(Calendar.HOUR,24, "tomorrow"), | ||
ParamClass(Calendar.DATE,-1, "yesterday"), | ||
ParamClass(Calendar.DATE,-10, "10 days ago"), | ||
ParamClass(Calendar.DATE,-28, "28 days ago"), | ||
ParamClass(Calendar.DATE,-32, "one month ago"), | ||
ParamClass(Calendar.DATE,1, "tomorrow"), | ||
ParamClass(Calendar.DATE,10, "10 days from now"), | ||
ParamClass(Calendar.DATE,28, "28 days from now"), | ||
ParamClass(Calendar.DATE,29, "one month from now",GregorianCalendar(2014,Calendar.FEBRUARY, 5).getTime()), | ||
ParamClass(Calendar.DATE,32, "one month from now"), | ||
ParamClass(Calendar.MONTH,-1, "one month ago"), | ||
ParamClass(Calendar.MONTH,-10, "10 months ago"), | ||
ParamClass(Calendar.MONTH,-11, "11 months ago"), | ||
ParamClass(Calendar.MONTH,-12, "one year ago"), | ||
ParamClass(Calendar.MONTH,1, "one month from now"), | ||
ParamClass(Calendar.MONTH,10, "10 months from now"), | ||
ParamClass(Calendar.MONTH,11, "11 months from now"), | ||
ParamClass(Calendar.MONTH,12, "one year from now"), | ||
ParamClass(Calendar.YEAR,-1, "one year ago"), | ||
ParamClass(Calendar.YEAR,-2, "2 years ago"), | ||
ParamClass(Calendar.YEAR,1, "one year from now"), | ||
ParamClass(Calendar.YEAR,2, "2 years from now"), | ||
ParamClass(Calendar.SECOND,0, "now"), | ||
ParamClass(Calendar.MINUTE,0, "now"), | ||
ParamClass(Calendar.HOUR,0, "now"), | ||
ParamClass(Calendar.DAY_OF_YEAR,0, "now"), | ||
ParamClass(Calendar.MONTH,0, "now"), | ||
ParamClass(Calendar.YEAR,0, "now") | ||
) | ||
|
||
givenData(data) { | ||
on("calling humanize with " + it.input.toString() + " for timeunit " + it.timeUnit, { | ||
val cal = GregorianCalendar() | ||
cal.setTime(it.dateToUse) | ||
cal.add(it.timeUnit, it.input) | ||
val actual = cal.getTime().humanize(it.dateToUse) | ||
it("should become ${it.expected}", { | ||
shouldEqual(it.expected, actual) | ||
}) | ||
}) | ||
} | ||
|
||
|
||
}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should change this to 59 to verify the boundary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep