gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:29:37 28 4
gpt4 key购买 nike

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

最佳答案

看看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"))

关于unix - 在 clojure 中将 unix 时间戳转换为人类可读的日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493716/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com