Domanda

The database query is returning the date and time in a unix timestamp. I am trying to use clojure to convert this into human readable time and then split the date and time into two separate columns under date and time.

È stato utile?

Soluzione

Take a look at clj-time

For example here is how you would convert a (long) timestamp to DateTime:

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

Here is how to "split" it by date/time portions:

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"

Depending on how you need the result to be formatted, you can choose from many different built in formatters:

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

or built a custom one:

(def custom-formatter (formatter "yyyyMMdd"))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top