将unix时间戳转换为clojure中的人类可读日期和时间

时间:2013-08-28 16:44:33

标签: unix clojure timestamp epoch

数据库查询以unix时间戳记返回日期和时间。我正在尝试使用clojure将其转换为人类可读时间,然后在日期和时间下将日期和时间拆分为两个单独的列。

1 个答案:

答案 0 :(得分:7)

查看clj-time

例如,以下是将(长)时间戳转换为DateTime的方法:

user=> (clj-time.coerce/from-long 893362442345)
#<DateTime 1998-04-23T20:14:02.345Z>

以下是按日期/时间部分“拆分”的方法:

user=> (unparse (formatters :date) (clj-time.coerce/from-long 893362442345))
"1998-04-23"

user=> (unparse (formatters :time) (clj-time.coerce/from-long 893362442345))
"20:14:02.345Z"

根据您需要格式化结果的方式,您可以从许多不同的内置格式化程序中进行选择:

user=> (show-formatters)
:basic-date                             20130828
:basic-date-time                        20130828T175957.850Z
:basic-date-time-no-ms                  20130828T175957Z
:basic-ordinal-date                     2013240
:basic-ordinal-date-time                2013240T175957.850Z
:basic-ordinal-date-time-no-ms          2013240T175957Z
:basic-t-time                           T175957.850Z
:basic-t-time-no-ms                     T175957Z
:basic-time                             175957.850Z
:basic-time-no-ms                       175957Z
:basic-week-date                        2013W353
:basic-week-date-time                   2013W353T175957.850Z
:basic-week-date-time-no-ms             2013W353T175957Z
:date                                   2013-08-28
:date-hour                              2013-08-28T17
:date-hour-minute                       2013-08-28T17:59
:date-hour-minute-second                2013-08-28T17:59:57
:date-hour-minute-second-fraction       2013-08-28T17:59:57.850
:date-hour-minute-second-ms             2013-08-28T17:59:57.850
:date-time                              2013-08-28T17:59:57.850Z
:date-time-no-ms                        2013-08-28T17:59:57Z
:hour                                   17
:hour-minute                            17:59
:hour-minute-second                     17:59:57
:hour-minute-second-fraction            17:59:57.850
:hour-minute-second-ms                  17:59:57.850
:mysql                                  2013-08-28 17:59:57
:ordinal-date                           2013-240
:ordinal-date-time                      2013-240T17:59:57.850Z
:ordinal-date-time-no-ms                2013-240T17:59:57Z
:rfc822                                 Wed, 28 Aug 2013 17:59:57 +0000
:t-time                                 T17:59:57.850Z
:t-time-no-ms                           T17:59:57Z
:time                                   17:59:57.850Z
:time-no-ms                             17:59:57Z
:week-date                              2013-W35-3
:week-date-time                         2013-W35-3T17:59:57.850Z
:week-date-time-no-ms                   2013-W35-3T17:59:57Z
:weekyear                               2013
:weekyear-week                          2013-W35
:weekyear-week-day                      2013-W35-3
:year                                   2013
:year-month                             2013-08
:year-month-day                         2013-08-28

或构建自定义的:

(def custom-formatter (formatter "yyyyMMdd"))