pointyhat
pointyhat

Reputation: 596

Groovy way to obtain number of seconds in a period

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

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

Upvotes: 1

Views: 72

Answers (1)

doelleri
doelleri

Reputation: 19702

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.

Upvotes: 1

Related Questions