It’s not as easy as I initially thought to calculate the exact age of my son when the days fly by quickly and the time interval is usually measured in weeks or (less often) months, or hardly thus far – years. That’s why I thought about setting up a Clojure project with lein‘s help to develop an application (a script, shall I say?) to calculate it for me. It’s been on my TODO list for far too long.
Lein is pretty easy to start with – just type in lein new with the name of your project and you’re done.
jacek:~/oss $ lein new maksym-age-clojure Created new project in: /Users/jacek/oss/maksym-age-clojure Look over project.clj and start coding in maksym_age_clojure/core.clj jacek:~/oss $ cd maksym-age-clojure
After all the book readings, I learnt that when you design an application in Clojure, use Java classes and libraries as much as possible (but not overly much!). I remembered when Stuart Halloway mentioned a Clojure library for Joda Time during his talk about Clojure – Clojure in the Field (at 00:36:35) so I googled it to see if there’s a more recent version.
There is one – Sean Corfield’s clj-time – a date and time library for Clojure, wrapping the Joda Time library. It seemed a perfect fit for my needs.
I vi’ed project.clj and added the library.
jacek:~/oss/maksym-age-clojure
$ cat project.clj
(defproject maksym-age-clojure "1.0.0-SNAPSHOT"
:description "Calculate Maksym's age in Clojure using Joda Time library"
:dependencies [[org.clojure/clojure "1.3.0"]
clj-time "0.3.5"])
Note: I don’t know how to tell WordPress to display :dependencies correctly so it contains clj-time “0.3.5″ in the brackets. Anyone?
I ran lein deps and switched to Clojure’s REPL with lein repl. No overly complicated stuff – quickly and easily.
jacek:~/oss/maksym-age-clojure $ lein deps Downloading: clj-time/clj-time/0.3.5/clj-time-0.3.5.pom from repository central at http://repo1.maven.org/maven2 ... Copying 3 files to /Users/jacek/oss/maksym-age-clojure/lib jacek:~/oss/maksym-age-clojure $ lein repl REPL started; server listening on localhost port 46468 user=>
Reading the readme of the clj-time project is almost like being guided by a mentor who knows what you’re up to if you’re in the room (i.e. on the project’s website). It’s concise and exactly to the point. No troubles along the way. It is during such moments when I wonder how people can claim programming is a huge undertaking?!
I moved along importing the clj-time.core (the use function) and the other functions which I doubt anyone would find hard to comprehend.
user=> (use 'clj-time.core) WARNING: extend already refers to: #'clojure.core/extend in namespace: user, being replaced by: #'clj-time.core/extend nil user=> ; maksym was born on 10/03/2011 at 17:23 CET user=> (def maksym-birth-date-time (date-time 2011 10 03 17 23)) #'user/maksym-birth-date-time user=> (in-months (interval maksym-birth-date-time (now))) 4 user=> (in-weeks (interval maksym-birth-date-time (now))) 18
I’ll leave it as an exercise to you to have it as a standalone script in the project. I seem to have lost interest in it in its current form. Sorry. I’ll ditch the project and create another one for more general age calculations that would help me to remember my entire family’s birthdays. That’s the plan.
I’d like to thank Stuart and Sean for the enormous oversimplification of the development efforts of mine – Stuart planted the idea of using the library Sean currently maintains. Not much was left to me! :)

As far as I remember clj-time wraps Joda-Time. And Joda has nice PeriodFormater for printing intervals like “3 months and 3 days”. Not sure if that’s wrapped in clj-time, but you can always use it directly.
http://joda-time.sourceforge.net/apidocs/org/joda/time/format/PeriodFormatterBuilder.html
Thanks Konrad. It’s gonna be the topic of the upcoming blog entry.
Pingback: clj-time and Joda Time to pretty-print my son’s age in Clojure | Japila :: verba docent, exempla trahunt