Clojure’s lein and clj-time to calculate my son’s age

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.

$ 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
$ 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.

$ 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.

$ lein deps
Downloading: clj-time/clj-time/0.3.5/clj-time-0.3.5.pom from repository central at
Copying 3 files to /Users/jacek/oss/maksym-age-clojure/lib
$ lein repl
REPL started; server listening on localhost port 46468

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
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=> (in-months (interval maksym-birth-date-time (now)))
user=> (in-weeks (interval maksym-birth-date-time (now)))

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! :)

Be Sociable, Share!
This entry was posted in Languages.

3 Responses to Clojure’s lein and clj-time to calculate my son’s age

  1. Konrad Garus says:

    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.

  2. Thanks Konrad. It’s gonna be the topic of the upcoming blog entry.

  3. Pingback: clj-time and Joda Time to pretty-print my son’s age in Clojure | Japila :: verba docent, exempla trahunt

Leave a Reply

%d bloggers like this: