Share
Embed
Transcript
26 slides
-
• Shintaro Katafuchi • @hotchemi • Using Kotlin for •
PermissionsDispatcher • Spring boot -
• An intuitive Date extensions in Kotlin • Inspired by
• ActiveSupport (Ruby) • Timepiece (Swift) -
val calendar = Calendar.getInstance() calendar.time = Date() calendar.set(Calendar.YEAR, year) calendar.set(Calendar.MONTH,
month) calendar.set(Calendar.DATE, day) calendar.set(Calendar.HOUR, hour) calendar.set(Calendar.MINUTE, minute) calendar.set(Calendar.SECOND, second) calendar.time specify date components -
val firstCommitDate = Dates.of( year= 2016, month= 2,
day= 26, hour= 18, minute= 58, second= 31) specify date components (khronos) -
val calendar = Calendar.getInstance() calendar.time = Date() calendar.set(Calendar.YEAR, 2016) calendar.set(Calendar.MONTH,
1) calendar.set(Calendar.DATE, 1) calendar.set(Calendar.HOUR, 0) calendar.set(Calendar.MINUTE, 0) calendar.set(Calendar.SECOND, 0) calendar.time beginning of this year -
val today = DateFormat.getDateInstance().parse("2016/03/23") val yesterday = DateFormat.getDateInstance().parse("2016/03/22")
val tomorrow = DateFormat.getDateInstance().parse("2016/03/24") today.compareTo(yesterday) < 0 && today.compareTo(tomorrow) > 0 compare dates -
1 // kotlin.Int 1.day // khronos.Duration 1.day.ago //
java.util.Date Extensions -
// IntExtensions.kt val Int.day: Duration get() =
Duration(unit = Calendar.DATE, value = this) val Int.days: Duration get() = day Extensions -
// Duration.kt val ago = calculate(from = Date(), value =
-‐value) val since = calculate(from = Date(), value = value) private fun calculate(from: Date, value: Int): Date { calendar.time = from calendar.add(unit, value) return calendar.time } Extensions -
// DateExtensions.kt fun Date.with(year: Int = 0, month: Int
= 0, day: Int = 0, hour: Int = 0, minute: Int = 0, second: Int = 0): Date { calendar.time = this if (year > 0) calendar.set(Calendar.YEAR, year) if (month > 0) calendar.set(Calendar.MONTH, month -‐ 1) if (day > 0) calendar.set(Calendar.DATE, day) if (hour > 0) calendar.set(Calendar.HOUR, hour) if (minute > 0) calendar.set(Calendar.MINUTE, minute) if (second > 0) calendar.set(Calendar.SECOND, second) return calendar.time } Extensions -
// Compile error!! how to do that? fun Date.Companion.of(year:
Int = 0, month: Int = 0): Date {} Extensions -
Operator overloading Expression Translated to a + b a.plus(b) a
- b a.minus(b) a * b a.times(b) a / b a.div(b) a % b a.mod(b) a..b a.rangeTo(b) a in b b.contains(a) a > b a.compareTo(b) > 0 a < b a.compareTo(b) < 0 -
// DateExtensions.kt operator fun Date.plus(duration: Duration): Date {
calendar.time = this calendar.add(duration.unit, duration.value) return calendar.time } operator fun Date.minus(duration: Duration): Date { calendar.time = this calendar.add(duration.unit, -‐duration.value) return calendar.time } Operator overloading -
// DateExtensions.kt operator fun Date.rangeTo(other: Date) = DateRange(this, other)
class DateRange(override val start: Date, override val endInclusive: Date): ClosedRange<Date> { override fun contains(value: Date) = start < value && value < endInclusive } Ranges