【问题标题】:Unix timestamp in ClojureClojure 中的 Unix 时间戳
【发布时间】:2023-04-03 18:40:02
【问题描述】:

我需要将两个 unix 时间戳传递到查询中以拉回两个日期之间的数据。

如果脚本在今天(9 月 17 日)运行并且需要获取前一天的数据,则 unix 时间戳需要为 16 日 00:00:00 到 17 日 00:00:00。

这些时间戳需要自动存储在变量 dateFrom 和 dateTo 中,以便它们可以传递到查询中。

提前感谢您的帮助。

【问题讨论】:

    标签: unix datetime clojure timestamp


    【解决方案1】:

    你可以使用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>
    

    等等。

    如果您需要转换为 java.sql.Timestamp 而不是 long,则可以使用 clj-time.coerce/to-sql-timejava.sql.Timestamps 在 Clojure 1.5.1 中使用 #inst 文字打印):

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

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 2013-03-23
      • 2021-02-12
      • 1970-01-01
      • 2011-04-11
      • 2011-10-01
      • 2013-03-11
      • 2013-08-31
      • 2018-12-29
      相关资源
      最近更新 更多