mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Merge pull request #1425 from eduebernal/freq-display-normal
fix marker scaling in frequency display
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
package org.isoron.uhabits.core.utils
|
||||
|
||||
import org.isoron.uhabits.core.models.Timestamp
|
||||
import java.time.YearMonth
|
||||
import java.util.Calendar
|
||||
import java.util.Calendar.DAY_OF_MONTH
|
||||
import java.util.Calendar.DAY_OF_WEEK
|
||||
@@ -178,6 +179,26 @@ abstract class DateUtils {
|
||||
return getWeekdayNames(GregorianCalendar.SHORT, firstWeekday)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a vector of Int representing the frequency of each weekday in a given month.
|
||||
*
|
||||
* @param startOfMonth a Timestamp representing the beginning of the month.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getWeekdaysInMonth(startOfMonth: Timestamp): Array<Int> {
|
||||
val month = startOfMonth.toCalendar()[Calendar.MONTH] + 1
|
||||
val year = startOfMonth.toCalendar()[Calendar.YEAR]
|
||||
val weekday = startOfMonth.weekday
|
||||
val monthLength = YearMonth.of(year, month).lengthOfMonth()
|
||||
|
||||
val freq = Array(7) { 0 }
|
||||
for (day in weekday until weekday + monthLength) {
|
||||
freq[day % 7] += 1
|
||||
}
|
||||
|
||||
return freq
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getToday(): Timestamp = Timestamp(getStartOfToday())
|
||||
|
||||
|
||||
@@ -118,6 +118,31 @@ class DateUtilsTest : BaseUnitTest() {
|
||||
assertThat(arrayOf("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"), equalTo(longWeekdayNames))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getWeekdaysInMonth() {
|
||||
val february = GregorianCalendar(2018, Calendar.FEBRUARY, 1)
|
||||
val leapFebruary = GregorianCalendar(2020, Calendar.FEBRUARY, 1)
|
||||
val month = GregorianCalendar(2020, Calendar.APRIL, 1)
|
||||
val longMonth = GregorianCalendar(2020, Calendar.AUGUST, 1)
|
||||
|
||||
assertThat(
|
||||
arrayOf(4, 4, 4, 4, 4, 4, 4),
|
||||
equalTo(DateUtils.getWeekdaysInMonth(Timestamp(february)))
|
||||
)
|
||||
assertThat(
|
||||
arrayOf(5, 4, 4, 4, 4, 4, 4),
|
||||
equalTo(DateUtils.getWeekdaysInMonth(Timestamp(leapFebruary)))
|
||||
)
|
||||
assertThat(
|
||||
arrayOf(4, 4, 4, 4, 5, 5, 4),
|
||||
equalTo(DateUtils.getWeekdaysInMonth(Timestamp(month)))
|
||||
)
|
||||
assertThat(
|
||||
arrayOf(5, 5, 5, 4, 4, 4, 4),
|
||||
equalTo(DateUtils.getWeekdaysInMonth(Timestamp(longMonth)))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetToday() {
|
||||
setFixedLocalTime(FIXED_LOCAL_TIME)
|
||||
|
||||
Reference in New Issue
Block a user