khronos

Ce37cf75fa85b89a33916545978c64de?s=47 @hotchemi
March 23, 2016

 khronos

Introduction of khronos

Ce37cf75fa85b89a33916545978c64de?s=128

@hotchemi

March 23, 2016
Tweet

Transcript

  1. khronos Shintaro Katafuchi

  2. • Shintaro Katafuchi • @hotchemi • Using Kotlin for •

    PermissionsDispatcher • Spring boot
  3. khronos github.com/hotchemi/khronos

  4. • An intuitive Date extensions in Kotlin • Inspired by

    • ActiveSupport (Ruby) • Timepiece (Swift)
  5. val  calendar  =  Calendar.getInstance()
 calendar.add(Calendar.WEEK_OF_MONTH,  1)
 val  nextWeek  =  calendar.time

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

  7. 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
  8. val  firstCommitDate  =  Dates.of(   year=  2016,   month=  2,

      day=  26,   hour=  18,   minute=  58,   second=  31) specify date components (khronos)
  9. 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
  10. val  newYearDay  =  Dates.now.beginningOfYear beginning of this year (khronos)

  11. 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
  12. Dates.today  in  1.day.ago..1.day.since compare dates (khronos)

  13. None
  14. • How it works 1. Extensions 2. Operator overloading 3.

    Ranges
  15. 1  //  kotlin.Int   1.day  //  khronos.Duration   1.day.ago  //

     java.util.Date Extensions
  16. //  IntExtensions.kt
 val  Int.day:  Duration
        get()  =

     Duration(unit  =  Calendar.DATE,  value  =  this)
 
 val  Int.days:  Duration
        get()  =  day Extensions
  17. //  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
  18. //  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
  19. //  Compile  error!!  how  to  do  that?   fun  Date.Companion.of(year:

     Int  =  0,  month:  Int  =  0):  Date  {} Extensions
  20. Operator overloading Dates.today  +  1.week  

  21. 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
  22. //  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
  23. Dates.today  in  1.day.ago..1.day.since Ranges

  24. //  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
  25. Ranges • Other stuff • *Progression • downTo() • reversed()

    • step()
  26. dependencies  {
        compile  'com.github.hotchemi:khronos:0.1.0'
 }   Install