-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [ANDROAPP-6305] Take into account invalid date formats
- Loading branch information
1 parent
7c0b2bd
commit d404e3d
Showing
4 changed files
with
87 additions
and
39 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
...ommonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/DateTimeUtils.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,43 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal | ||
|
||
import org.hisp.dhis.mobile.ui.designsystem.component.SelectableDates | ||
import org.hisp.dhis.mobile.ui.designsystem.component.parseDate | ||
import org.hisp.dhis.mobile.ui.designsystem.component.parseStringDateToMillis | ||
import java.text.ParseException | ||
import java.text.SimpleDateFormat | ||
import java.util.Calendar | ||
|
||
internal fun dateIsInRange(date: Long, allowedDates: SelectableDates, format: String = "ddMMyyyy"): Boolean { | ||
return ( | ||
date >= parseStringDateToMillis(allowedDates.initialDate, format) && | ||
date <= parseStringDateToMillis(allowedDates.endDate, format) | ||
) | ||
} | ||
|
||
internal fun yearIsInRange(date: String, pattern: String, yearRange: IntRange): Boolean { | ||
val cal = Calendar.getInstance() | ||
return date.parseDate(pattern)?.let { | ||
cal.time = it | ||
yearRange.contains(cal.get(Calendar.YEAR)) | ||
} ?: false | ||
} | ||
|
||
internal fun isValidHourFormat(timeString: String): Boolean { | ||
val hourRange = IntRange(0, 24) | ||
val minuteRange = IntRange(0, 60) | ||
|
||
return timeString.length == 4 && hourRange.contains(timeString.substring(0, 2).toInt()) && | ||
minuteRange.contains(timeString.substring(2, 4).toInt()) | ||
} | ||
|
||
internal fun isValidDate(text: String): Boolean { | ||
if (text.length != 8) return false | ||
val format = SimpleDateFormat("ddMMyyyy") | ||
format.isLenient = false | ||
return try { | ||
format.parse(text) | ||
true | ||
} catch (e: ParseException) { | ||
false | ||
} | ||
} |
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