我遇到了一个奇怪的错误,我无法弄清楚它为什么会发生。如果我调用我的原始函数,roundToMidnight()
则不会调用并且日期不会四舍五入。
我原来的功能,什么不起作用:
suspend operator fun invoke(reference: Reference) = reference.tagId
?.let { tagRepository.getTag(it) }
?.uploadDate ?: Date()
.apply { time += accountRepository.getAccount().first().defaultExpiryPeriod }
.roundToMidnight()
}
有什么作用:
suspend operator fun invoke(reference: Reference): Date {
val date = reference.tagId
?.let { tagRepository.getTag(it) }
?.uploadDate ?: Date()
.apply { time += accountRepository.getAccount().first().defaultExpiryPeriod }
return date.roundToMidnight()
}
roundToMidnight()
返回一个新的实例Date
fun Date.roundToMidnight(): Date {
val calendar = Calendar.getInstance()
calendar.time = this
calendar[Calendar.HOUR_OF_DAY] = 23
calendar[Calendar.MINUTE] = 59
calendar[Calendar.SECOND] = 59
calendar[Calendar.MILLISECOND] = 0
return Date(calendar.timeInMillis)
}
是什么导致这两种功能的差异?我会说它们完全一样,我看到自己在一个月内将无错误功能重构为原始功能,因为我忘记了这种情况。