Upgrade to Pro — share decks privately, control downloads, hide ads and more …

khronos

@hotchemi
March 23, 2016

 khronos

Introduction of khronos

@hotchemi

March 23, 2016
Tweet

More Decks by @hotchemi

Other Decks in Programming

Transcript

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

    • ActiveSupport (Ruby) • Timepiece (Swift)
  2. 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
  3. val  firstCommitDate  =  Dates.of(   year=  2016,   month=  2,

      day=  26,   hour=  18,   minute=  58,   second=  31) specify date components (khronos)
  4. 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
  5. 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
  6. //  IntExtensions.kt
 val  Int.day:  Duration
        get()  =

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

     Int  =  0,  month:  Int  =  0):  Date  {} Extensions
  10. 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
  11. //  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
  12. //  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