Datetime Clojure:now和a记录SQL日期时间之间的时差
试图找出获取当前时间和mySQL记录的日期时间之间差异的最佳方法 下面是我的代码片段Datetime Clojure:now和a记录SQL日期时间之间的时差,datetime,clojure,Datetime,Clojure,试图找出获取当前时间和mySQL记录的日期时间之间差异的最佳方法 下面是我的代码片段 (ns clojure.example.hello (:require [clj-time.coerce :as coerce] [clj-time.local :as l])) (let [timenow (l/local-now) ;; #object[org.joda.time.DateTime 0x333e8ce5 2017-02-23T21:44:11.022+11:00
(ns clojure.example.hello
(:require [clj-time.coerce :as coerce]
[clj-time.local :as l]))
(let [timenow (l/local-now) ;; #object[org.joda.time.DateTime 0x333e8ce5 2017-02-23T21:44:11.022+11:00]
last-update (gettimelogged :timestamp)
;; returns SQL datetime which is 2017-02-23 19:20:15,
;; but gets converted into #inst "2017-02-23T08:20:15.000000000-00:00"]
(println (timenow - lastupdate)))
在timenow和上次更新之间运行比较的最佳方法是什么
如果需要,更新时间戳的最佳方法是什么?假设我想将它延迟加载到clojure中,比如,{:NAME“Jim”:AGE“24”:CHECKDATE“NOW()”}假设您有一个即时值(以
#inst…
开头的值),您可以使用clojure中的inst ms
函数将其转换为毫秒,然后与当前时间(毫秒)进行比较
(let [db-ms (inst-ms #inst"2017-02-23T08:20:15.000000000-00:00")
now-ms (System/currentTimeMillis)]
(println "Difference is" (- now-ms db-ms) "ms"))
如果您没有使用clojure 1.9,您可以在指令上调用.getTime
:
(.getTime #inst"2017")
要使用MySQL插入当前时间,您有几个选项,包括使用内置的NOW()
函数,您可以使用honeysql访问该函数
但是,对于纯基于jbdc的方法,请使用clj-time.concure的帮助,正如您已经使用的:
(defn get-sql-now []
(-> (System/currentTimeMillis)
coerce/from-long
coerce/to-sql-time))
(j/insert! mysql-db "joshtable" {:name "Jim"
:age 24
:checkdate (get-sql-now)})
以秒为单位的差异?太太ns?使用inst-ms(让[zyx(inst-ms#inst“2017-02-23T08:20:15.000000000-00:00”))(println-zyx)时,秒数可能不同将是最好的问题。我得到编译器异常java.lang.RuntimeException:无法解析symbol:inst ms在此上下文中编译。我正在使用Clojure 1.8.0,是否应该使用其他版本?@Stixxxx我刚刚更新了答案。调用(.getTime#inst“2017”)
。