scaladatetimejodatimenscala-time

How does DateTime.now.hour(0) get a new DateTime in nscala-time?


I use nsacala-time package from https://github.com/nscala-time/nscala-time.

import com.github.nscala_time.time.Imports._
DateTime.now.hour(0)
res0: org.joda.time.DateTime = 2015-06-11T22:33:52.266+08:00

will get a new DateTime object. But in the source,https://github.com/nscala-time/nscala-time/blob/master/src/main/scala/com/github/nscala_time/time/RichDateTime.scala ,it does not have a hour function with signature (Int). It seems that withHour(hour: Int) do the exact work.


Solution

  • Implicit conversions everywhere (a.k.a Black Magic, a.k.a why everyone hates Scala)!!!

    1. hour returns a org.joda.time.DateTime.Property.

    2. Import._ imports the RichDateTimeProperty

    3. RichDateTimeProperty has an apply method defined and by default in Scala doing something() if something is not a method gets translated to something.apply(), so finally this method is invoked via 2 implicit conversions:

      def apply(value: Int): DateTime = underlying.setCopy(value)