Skip to content
Permalink
Browse files

Use unix timestamps for sqlite datetime columns

  • Loading branch information...
swlkr committed Mar 12, 2019
1 parent 8c05c57 commit a61a2219250875320e4d4d4f2f0201aaff2bd0d9
Showing with 40 additions and 10 deletions.
  1. +6 −3 src/coast.clj
  2. +2 −3 src/coast/db/migrations.clj
  3. +4 −4 src/coast/db/sql.clj
  4. +28 −0 src/coast/time2.clj
@@ -5,7 +5,7 @@
[coast.db.connection]
[coast.theta]
[coast.env]
[coast.time]
[coast.time2]
[coast.components]
[coast.responses]
[coast.utils]
@@ -82,8 +82,11 @@
uuid
intern-var]

[coast.time
now]
[coast.time2
now
datetime
instant
strftime]

[hiccup2.core
raw
@@ -1,14 +1,13 @@
(ns coast.db.migrations
(:require [coast.utils :as utils]
[clojure.edn :as edn]
[clojure.string :as string]
[coast.db.connection :refer [spec]]))

(def rollback? (atom false))
(def vectors (atom []))

(def sql {"sqlite" {:timestamp "timestamp"
:now "current_timestamp"
(def sql {"sqlite" {:timestamp "integer"
:now "(strftime('%s', 'now'))"
:pk "integer primary key"}
"postgres" {:timestamp "timestamptz"
:now "now()"
@@ -1,9 +1,9 @@
(ns coast.db.sql
(:require [clojure.string :as string]
[clojure.walk :as walk]
[coast.utils :as utils])
(:refer-clojure :exclude [update])
(:import (java.time LocalDateTime)))
[coast.utils :as utils]
[coast.time2 :as time2])
(:refer-clojure :exclude [update]))


(def ops #{:select :from :update :set :insert
@@ -143,7 +143,7 @@


(defn update-set [v]
(let [v (conj v [:updated-at (LocalDateTime/now)])
(let [v (conj v [:updated-at (time2/now)])
args (filter #(not= "id" (-> % first name)) v)]
{:update-set (str "set " (->> (map (fn [[k _]] (str (-> k name utils/snake-case) " = ?")) args)
(distinct)
@@ -0,0 +1,28 @@
(ns coast.time2
(:import (java.time ZoneOffset ZonedDateTime ZoneId Instant)
(java.time.format DateTimeFormatter)))


(defn now []
(.getEpochSecond (Instant/now)))


(defn instant [seconds]
(Instant/ofEpochSecond seconds))


(defn datetime
([seconds zone]
(let [zoneId (if (string? zone)
(ZoneId/of zone)
ZoneOffset/UTC)]
(ZonedDateTime/ofInstant
(instant seconds)
zoneId)))
([seconds]
(datetime seconds nil)))


(defn strftime [d pattern]
(let [formatter (DateTimeFormatter/ofPattern pattern)]
(.format formatter d)))

0 comments on commit a61a221

Please sign in to comment.
You can’t perform that action at this time.