Slide 1

Slide 1 text

khronos Shintaro Katafuchi

Slide 2

Slide 2 text

• Shintaro Katafuchi • @hotchemi • Using Kotlin for • PermissionsDispatcher • Spring boot

Slide 3

Slide 3 text

khronos github.com/hotchemi/khronos

Slide 4

Slide 4 text

• An intuitive Date extensions in Kotlin • Inspired by • ActiveSupport (Ruby) • Timepiece (Swift)

Slide 5

Slide 5 text

val  calendar  =  Calendar.getInstance()
 calendar.add(Calendar.WEEK_OF_MONTH,  1)
 val  nextWeek  =  calendar.time   1 week after

Slide 6

Slide 6 text

Dates.today  +  1.week   1 week after (khronos)

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

val  firstCommitDate  =  Dates.of(   year=  2016,   month=  2,   day=  26,   hour=  18,   minute=  58,   second=  31) specify date components (khronos)

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

val  newYearDay  =  Dates.now.beginningOfYear beginning of this year (khronos)

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Dates.today  in  1.day.ago..1.day.since compare dates (khronos)

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

• How it works 1. Extensions 2. Operator overloading 3. Ranges

Slide 15

Slide 15 text

1  //  kotlin.Int   1.day  //  khronos.Duration   1.day.ago  //  java.util.Date Extensions

Slide 16

Slide 16 text

//  IntExtensions.kt
 val  Int.day:  Duration
        get()  =  Duration(unit  =  Calendar.DATE,  value  =  this)
 
 val  Int.days:  Duration
        get()  =  day Extensions

Slide 17

Slide 17 text

//  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

Slide 18

Slide 18 text

//  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

Slide 19

Slide 19 text

//  Compile  error!!  how  to  do  that?   fun  Date.Companion.of(year:  Int  =  0,  month:  Int  =  0):  Date  {} Extensions

Slide 20

Slide 20 text

Operator overloading Dates.today  +  1.week  

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

//  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

Slide 23

Slide 23 text

Dates.today  in  1.day.ago..1.day.since Ranges

Slide 24

Slide 24 text

//  DateExtensions.kt
 operator  fun  Date.rangeTo(other:  Date)  =  DateRange(this,  other)   class  DateRange(override  val  start:  Date,  override  val  endInclusive:  Date):   ClosedRange  {
        override  fun  contains(value:  Date)  =  start  <  value  &&  value  <  endInclusive
 } Ranges

Slide 25

Slide 25 text

Ranges • Other stuff • *Progression • downTo() • reversed() • step()

Slide 26

Slide 26 text

dependencies  {
        compile  'com.github.hotchemi:khronos:0.1.0'
 }   Install