简体   繁体   English

Clojure中的Unix时间戳

[英]Unix timestamp in Clojure

I need to pass in two unix timestamps into a query to pull back data between two dates. 我需要将两个unix时间戳传递给查询以在两个日期之间提取数据。

if the script is run today, the 17th of September and needs to get the data for the previous day, the unix timestamps will need to be 00:00:00 of the 16th to 00:00:00 of the 17th. 如果脚本今天运行,9月17日需要获取前一天的数据,则unix时间戳将需要是17日16:00到00:00:00的00:00:00。

these timestamps will need to be automatically stored in variables dateFrom and dateTo, so that they can be passed into the query. 这些时间戳需要自动存储在变量dateFrom和dateTo中,以便将它们传递给查询。

Thanks in advance for any help. 在此先感谢您的帮助。

You can use clj-time : 你可以使用clj-time

(require '(clj-time [core :as time] [coerce :as tc]))

;; not timezone-aware
(time/today)
;= #<LocalDate 2013-09-17>

;; UTC
(time/today-at-midnight)
;= #<DateMidnight 2013-09-17T00:00:00.000Z>

;; timestamp at midnight
(tc/to-long (time/today))
;= 1379376000000

(tc/minus (time/today-at-midnight) (time/days 1))
;= #<DateMidnight 2013-09-16T00:00:00.000Z>

And so forth. 等等。

If you need to convert to java.sql.Timestamp rather than long , there's clj-time.coerce/to-sql-time for that ( java.sql.Timestamp s are printed using #inst literals in Clojure 1.5.1): 如果你需要转换为java.sql.Timestamp而不是long ,那就是clj-time.coerce/to-sql-time (在Clojure 1.5.1中使用#inst文字打印java.sql.Timestamp ):

(tc/to-sql-time (time/today))
;= #inst "2013-09-17T00:00:00.000000000-00:00"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM