Skip to content

Instantly share code, notes, and snippets.

@shohiebsense
Created December 14, 2022 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shohiebsense/cb04f07fa229c58267fc5d697afce034 to your computer and use it in GitHub Desktop.
Save shohiebsense/cb04f07fa229c58267fc5d697afce034 to your computer and use it in GitHub Desktop.
Also note that the cache is using the same simpledateformat, ended up as a string
private const val LAST_REQUEST_TIME_MILLISECONDS = "last_request_time_milliseconds"
private val getDateFormat = SimpleDateFormat("dd-hh-mm-ss")
private fun isRapidRequest(lastRequestDateTime: String): Boolean {
if (lastRequestDateTime.isBlank()) return false
val date = getDateFormat.format(Date())
val dateTimeIntArrayList = date.split("-")
val lastDatetimeIntArrayList = lastRequestDateTime.split("-")
val currentDay = dateTimeIntArrayList[0].toInt()
val lastRequestTimeInDay = dateTimeIntArrayList[0].toInt()
if (currentDay != lastRequestTimeInDay) {
return false
}
val currentHour = dateTimeIntArrayList[1].toInt()
val lastrequestTimeInHour = lastDatetimeIntArrayList[1].toInt()
if ((currentHour - lastrequestTimeInHour).absoluteValue > 1) {
return false
}
val currentMinute = dateTimeIntArrayList[2].toInt()
val lastrequestTimeInMinute = lastDatetimeIntArrayList[2].toInt()
return (currentMinute - lastrequestTimeInMinute)
.absoluteValue < BuildConfig.ACCESS_INTERVAL_LIMIT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment