1
use(TimeCategory) {(1.year + 2.months + 3.hours).toMilliseconds() }

Any suggestions on how to get that in seconds (maintaining the level of grooviness)?

3
  • You wouldn't want to just divide it?
    – doelleri
    Commented Nov 14, 2017 at 0:22
  • @doelleri: I seek to maintain the same readability of the above snippet. No arithmetic.
    – pointyhat
    Commented Nov 14, 2017 at 0:26
  • 1
    Those + operators don't count? ;-)
    – doelleri
    Commented Nov 14, 2017 at 0:29

1 Answer 1

1

You can add a closure to the groovy.time.TimeDatumDependentDuration class to hide the conversion to seconds:

TimeDatumDependentDuration.metaClass.toSeconds = { delegate.toMilliseconds() / 1000 }

and then:

use(TimeCategory) { (1.year + 2.months + 3.hours).toSeconds() }

will return seconds.

Not the answer you're looking for? Browse other questions tagged or ask your own question.