Database Reference
In-Depth Information
Parsing dates and times
One dificult issue when normalizing and cleaning up data is how to deal with time. People
enter dates and times in a bewildering variety of formats; some of them are ambiguous, and
some of them are vague. However, we have to do our best to interpret them and normalize
them into a standard format.
In this recipe, we'll deine a function that attempts to parse a date into a standard string
format. We'll use the clj-time Clojure library, which is a wrapper around the Joda Java
library ( http://joda-time.sourceforge.net/ ).
Getting ready
First, we need to declare our dependencies in the Leiningen project.clj ile:
(defproject cleaning-data "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.6.0"]
[clj-time "0.9.0-beta1"]])
Then, we need to load these dependencies into our script or REPL. We'll exclude second
from clj-time to keep it from clashing with clojure.core/second :
(use '[clj-time.core :exclude (extend second)]
'[clj-time.format])
How to do it…
In order to solve this problem, we'll specify a sequence of date/time formats and walk
through them. The irst that doesn't throw an exception will be the one that we'll use.
1.
Here's a list of formats that you can try:
(def ^:dynamic *default-formats*
[:date
:date-hour-minute
:date-hour-minute-second
:date-hour-minute-second-ms
:date-time
:date-time-no-ms
:rfc822
"YYYY-MM-dd HH:mm"
"YYYY-MM-dd HH:mm:ss"
"dd/MM/YYYY"
"YYYY/MM/dd"
"d MMM YYYY"])
 
Search WWH ::




Custom Search