One day difference when persisting to database

81 views Asked by At

My db has a column dob like this:

|    dob     |
+------------+
| 1935-06-05 |

For TDD I test against an in-memory H2 database using korma and lobos:

(:use (lobos [migration :only [defmigration]] core schema config)
( :require [korma.core :as k] )

(defmigration create-table-readings
  (up [] (create
           (table :readings
                  (date :dob :not-null)
                  )))
  (down [] (drop
             (table :readings))))

(defn insert 
  [row]
  (k/insert readings (k/values row)))

I have a helper f-tion to convert to and from date strings:

 (:require 
    [clj-time.core :as t]
    [clj-time.format :as f]
    [clj-time.coerce :as c])

(def custom-formatter (f/formatter "yyyy-MM-dd"))

(defn from-sql-to-string
  "formats date from sql date to string"
  [sql-time]
  (f/unparse custom-formatter 
             (c/from-sql-date sql-time)))

(defn from-string-to-sql
  "formats date string and returns sql date"
  [string-time]
  (c/to-sql-date string-time))

yet when I test store and retrieve somehow I always get a one day difference between the dates:

(deftest database-insert-tests
  (testing "Testing simple insert"
           (let [test-row { :dob "1935-06-05" }]
             (database/insert test-row)
             (let [first-row (first (database/select-all))]
               (is (= test-row
                      (assoc first-row
                             :dob 
                             (t/from-sql-to-string (:dob first-row)))
                      ))))))

FAIL: actual: (not (= { :dob "1935-06-05" } { :dob "1935-06-04" }))

0

There are 0 answers