-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#322: add afterQuarter() and afterDay() to Date class
- Loading branch information
Showing
2 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
pat-static/src/main/kotlin/com/rectanglel/patstatic/dates/DateExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.rectanglel.patstatic.dates | ||
|
||
import java.util.* | ||
|
||
/* | ||
* There's no notion of HTTP-caching schemes on the Port Authority server, so I had to create my own. | ||
* @author Jeremy Jao | ||
*/ | ||
|
||
private fun getQuarterOfMonth(month: Int) = when { | ||
month >= Calendar.JANUARY && month <= Calendar.MARCH -> 0 | ||
month >= Calendar.APRIL && month <= Calendar.JUNE -> 1 | ||
month >= Calendar.JULY && month <= Calendar.SEPTEMBER -> 2 | ||
else -> 3 | ||
} | ||
|
||
private val hoursInMilliseconds = 1000 * 60 * 60 * 60 | ||
|
||
/** | ||
* Check if the quarter of the current datetime of the device is greater than the date to compare. | ||
* @param other the date to compare (in the app, this is the modified date of the file saved on the device) | ||
* @return true if the date's quarter > comparedDate's quarter especially by year | ||
*/ | ||
fun Date.afterQuarter(other: Date) : Boolean { | ||
val currentCalendar = Calendar.getInstance(Locale.US) | ||
currentCalendar.time = this | ||
val currentYear = currentCalendar.get(Calendar.YEAR) | ||
|
||
val otherCalendar = Calendar.getInstance(Locale.US) | ||
otherCalendar.time = other | ||
val comparedYear = otherCalendar.get(Calendar.YEAR) | ||
|
||
|
||
if (currentYear != comparedYear) { | ||
return currentYear > comparedYear | ||
} | ||
|
||
val currentMonth = currentCalendar.get(Calendar.MONTH) | ||
val currentQuarter = getQuarterOfMonth(currentMonth) | ||
val comparedMonth = otherCalendar.get(Calendar.MONTH) | ||
val comparedQuarter = getQuarterOfMonth(comparedMonth) | ||
|
||
|
||
|
||
return currentQuarter > comparedQuarter | ||
} | ||
|
||
/** | ||
* Check if the current date is 1 day after the other date. | ||
* @param other the date to test (usually the modified date of the cached data) | ||
* @return whether or not the current date is 24 hours ahead of the other date. | ||
*/ | ||
fun Date.afterDay(other: Date) : Boolean { | ||
// get the current date as a `Calendar` | ||
val currentCalendar = Calendar.getInstance() | ||
currentCalendar.time = this | ||
|
||
// get the date to compare, subtract the day by one | ||
val otherCalendar = Calendar.getInstance() | ||
otherCalendar.time = other | ||
otherCalendar.add(Calendar.DAY_OF_MONTH, 1) | ||
// otherCalendar.add(Calendar.MILLISECOND, 1) | ||
|
||
return (currentCalendar.timeInMillis - otherCalendar.timeInMillis) >= 0 | ||
} |
165 changes: 165 additions & 0 deletions
165
pat-static/src/test/kotlin/com/rectanglel/patstatic/dates/DateExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package com.rectanglel.patstatic.dates | ||
|
||
import org.junit.Assert | ||
import org.junit.Test | ||
import java.util.* | ||
|
||
@Suppress("RedundantVisibilityModifier") | ||
public class DateExtensionsTest { | ||
|
||
private fun createCalendar(year: Int, month: Int, day: Int, hour: Int) : Date { | ||
val calendar = Calendar.getInstance() | ||
calendar.set(year, month, day, hour, 0, 0) | ||
calendar.set(Calendar.MILLISECOND, 0) | ||
return calendar.time | ||
} | ||
|
||
private fun createDate(year: Int, month: Int, day: Int, hour: Int) : Date = createCalendar(year, month, day, hour) | ||
|
||
/** | ||
* "Current date of time" for unit testing. Logic to do things against... | ||
* | ||
* Take note that the `Date` class works in a 0 scale, so that means January = 0, 1st day of the month = 0 | ||
* | ||
* @return 2018-08-30T15:00 as a `Date` | ||
*/ | ||
private fun currentDate() : Date { | ||
return createDate( | ||
year = 2018, | ||
month = 7, | ||
day = 30, | ||
hour = 0 | ||
) | ||
} | ||
|
||
private val pastOneDayTestData = listOf( | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2018, | ||
month = 7, | ||
day = 29, | ||
hour = 12 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = false | ||
), | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2018, | ||
month = 7, | ||
day = 29, | ||
hour = 23 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = false | ||
), | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2018, | ||
month = 7, | ||
day = 28, | ||
hour = 0 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = true | ||
), | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2018, | ||
month = 7, | ||
day = 29, | ||
hour = 0 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = true | ||
), | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2018, | ||
month = 8, | ||
day = 1, | ||
hour = 1 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = false | ||
), | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2017, | ||
month = 1, | ||
day = 1, | ||
hour = 0 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = true | ||
), | ||
PastOneDayDatum( | ||
dateToTest = createDate( | ||
year = 2017, | ||
month = 1, | ||
day = 1, | ||
hour = 0 | ||
), | ||
currentDate = currentDate(), | ||
isPastOneDay = true | ||
), | ||
PastOneDayDatum( | ||
dateToTest = currentDate(), | ||
currentDate = currentDate(), | ||
isPastOneDay = false | ||
) | ||
) | ||
|
||
@Test | ||
public fun `test afterDay - shouldAllPass`() { | ||
pastOneDayTestData.forEach { | ||
Assert.assertEquals(it.isPastOneDay, it.currentDate.afterDay(it.dateToTest)) | ||
} | ||
} | ||
|
||
|
||
private val firstQuarter = createCalendar(year = 2018, month = 0, day = 1, hour = 0) | ||
private val secondQuarter = createCalendar(year = 2018, month = 3, day = 1, hour = 0) | ||
private val thirdQuarter = createCalendar(year = 2018, month = 6, day = 1, hour = 0) | ||
private val fourthQuarter = createCalendar(year = 2018, month = 9, day = 1, hour = 0) | ||
private val fifthQuarter = createCalendar(year = 2019, month = 0, day = 1, hour = 0) | ||
// | ||
private val quarterData = listOf( | ||
QuarterDatum(dateToTest = firstQuarter, currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = firstQuarter, currentDate = firstQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = secondQuarter, currentDate = thirdQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = secondQuarter, currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = thirdQuarter, currentDate = fourthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = thirdQuarter, currentDate = thirdQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = fourthQuarter, currentDate = fifthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = fourthQuarter, currentDate = fourthQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = fourthQuarter, currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 0, day = 2, hour = 5), currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 1, day = 2, hour = 5), currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 2, day = 2, hour = 5), currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 3, day = 2, hour = 5), currentDate = secondQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 3, day = 2, hour = 5), currentDate = thirdQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 4, day = 2, hour = 5), currentDate = thirdQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 5, day = 2, hour = 5), currentDate = thirdQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 6, day = 2, hour = 5), currentDate = thirdQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 6, day = 2, hour = 5), currentDate = fourthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 7, day = 2, hour = 5), currentDate = fourthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 8, day = 2, hour = 5), currentDate = fourthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 9, day = 2, hour = 5), currentDate = fourthQuarter, isCurrentDatePassedComparedDateQuarter = false), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 9, day = 2, hour = 5), currentDate = fifthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 10, day = 2, hour = 5), currentDate = fifthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 11, day = 2, hour = 5), currentDate = fifthQuarter, isCurrentDatePassedComparedDateQuarter = true), | ||
QuarterDatum(dateToTest = createCalendar(year = 2018, month = 12, day = 2, hour = 5), currentDate = fifthQuarter, isCurrentDatePassedComparedDateQuarter = false) | ||
|
||
) | ||
|
||
@Test | ||
public fun `test isCurrentDatePastComparedDateQuarter - shouldAllPass`() { | ||
quarterData.forEach { | ||
Assert.assertEquals(it.isCurrentDatePassedComparedDateQuarter, it.currentDate.afterQuarter(it.dateToTest)) | ||
} | ||
} | ||
} | ||
|
||
private data class PastOneDayDatum(val dateToTest : Date, val currentDate: Date, val isPastOneDay: Boolean) | ||
private data class QuarterDatum(val dateToTest: Date, val currentDate : Date, val isCurrentDatePassedComparedDateQuarter: Boolean) |