Skip to content

Instantly share code, notes, and snippets.

@ysasaki
Created June 23, 2015 14:24
Show Gist options
  • Save ysasaki/a04dc3e19a319d596fc7 to your computer and use it in GitHub Desktop.
Save ysasaki/a04dc3e19a319d596fc7 to your computer and use it in GitHub Desktop.
utc->jst using transform
(ns test.database
(:require [korma.core :as k]
[korma.db :refer [mysql defdb]]
[test.config :as config]
[clj-time.core :as t]
[clj-time.coerce :as c]))
(def default-time-zone-id (t/time-zone-for-id "Asia/Tokyo"))
(def utc->jst-filter-keys [:created_at :updated_at])
(defn utc->jst [timestamp]
(let [datetime (c/from-long timestamp)]
(t/to-time-zone datetime default-time-zone-id)))
(defn utc->jst-filter [v]
(let [keys (keys (select-keys v utc->jst-filter-keys))]
(reduce #(assoc %1 %2 (utc->jst (%1 %2))) v keys)))
(defdb database
(mysql config/db-spec))
(k/defentity users
(k/transform utc->jst-filter))
(ns test.test.database
(:require [clojure.test :refer :all]
[test.database :as db]
[clj-time.core :as t]
[clj-time.coerce :as c]))
(def utc-time #inst "2015-06-18T07:46:58.000000000-00:00")
(def local-time
(t/to-time-zone (c/from-long utc-time) db/default-time-zone-id))
(deftest filter-keys
(is
(= [:created_at :updated_at]
db/utc->jst-filter-keys)))
(deftest utc->jst
(is
(= local-time
(db/utc->jst #inst "2015-06-18T07:46:58.000000000-00:00"))))
(deftest utc->jst-filter
(is
(=
{:id 1
:created_at local-time
:updated_at local-time}
(db/utc->jst-filter {:id 1
:created_at utc-time
:updated_at utc-time}))))
;(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment