Skip to content

Instantly share code, notes, and snippets.

@shinseitaro
Last active December 18, 2015 20:48
Show Gist options
  • Save shinseitaro/5842663 to your computer and use it in GitHub Desktop.
Save shinseitaro/5842663 to your computer and use it in GitHub Desktop.
clj-time のマイマイメモ。ハマったものを書いていきます。
;:dependencies [[org.clojure/clojure "1.5.1"]
; [clj-time "0.5.0"]]
(ns foo.core
(:require
[clj-time.core :as t]
[clj-time.format :as f]
[clj-time.local :as l]
[clj-time.coerce :as c]))
(t/date-time 2013 6 23)
; #<DateTime 2013-06-23T00:00:00.000Z>
(c/from-sql-date #inst "2013-06-23T00:00:00")
; #<DateTime 2013-06-23T00:00:00.000Z>
(c/from-string "2013-06-23")
; #<DateTime 2013-06-23T00:00:00.000Z>
(c/from-long 1371945600000)
; #<DateTime 2013-06-23T00:00:00.000Z>
(f/parse (f/formatter "yyyyMMdd") "20120623")
; #<DateTime 2012-06-23T00:00:00.000Z>
(l/to-local-date-time (t/date-time 2013 6 23))
; #<DateTime 2013-06-23T00:00:00.000+09:00>
(l/to-local-date-time
(f/parse (f/formatter "yyyyMMdd") "20120623"))
; #<DateTime 2012-06-23T00:00:00.000+09:00>
(l/to-local-date-time
(f/parse (f/formatter "yyyy-MM-dd hh:mm:ss") "2012-06-23 10:00:01"))
; #<DateTime 2012-06-23T10:00:01.000+09:00>
;これは全部 "2013-06-23"を返します
(l/format-local-time #inst "2013-06-23T00:00:00" :year-month-day)
(l/format-local-time (t/date-time 2013 6 23) :year-month-day)
(l/format-local-time "2013-06-23" :year-month-day)
;;formatters の中身を見ます
f/formatters
;; {:basic-week-date-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@122556f>, :t-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1171820>, :basic-week-date-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1310c9f>, :basic-date-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@188f17b>, :date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@133a69e>, :date-hour-minute-second-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1ffd9f1>, :time-element-parser #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@3d97af>, :weekyear-week #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1b18d5e>, :weekyear-week-day #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1b82279>, :basic-date-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1df3aeb>, :basic-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@95268b>, :date-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1590cd>, :hour-minute-second-fraction #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1d582c6>, :basic-ordinal-date-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@7c992e>, :ordinal-date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@2cc550>, :basic-week-date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@14e41bf>, :date-element-parser #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@be9067>, :weekyear #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@fa6afc>, :date-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@85e6b1>, :basic-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@ec8446>, :date-opt-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1e02ee9>, :year #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@127e9e9>, :week-date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@16df6f5>, :ordinal-date-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@7053a9>, :hour #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@8485b4>, :week-date-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@65f2d7>, :time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@12a2a7d>, :year-month-day #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@14ba039>, :date-hour-minute-second-fraction #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@420b3>, :basic-date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@fed51d>, :basic-t-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@59657a>, :date-time-parser #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1156949>, :rfc822 #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@b36c7e>, :local-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@197dc26>, :ordinal-date-time-no-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@f645a8>, :hour-minute-second-ms #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@5733c2>, :t-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1070ffc>, :basic-ordinal-date-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@aa9f42>, :hour-minute #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1bdce67>, :local-date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@39de86>, :date-parser #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@140308e>, :local-date-opt-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@133cdbd>, :basic-t-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@84038>, :time-parser #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1b028db>, :time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@4a659b>, :date-hour-minute-second #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@54bbe1>, :date-hour #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@fae43c>, :basic-ordinal-date #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@19e5c2a>, :year-month #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@1b4f342>, :date-hour-minute #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@cead66>, :hour-minute-second #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@da3b58>, :week-date-time #<DateTimeFormatter org.joda.time.format.DateTimeFormatter@16e4a57>}
;;むりくりformattersでフォーマットしたデータを出してみます
(map #(vector % (l/format-local-time "2013-06-23" %)) (keys f/formatters))
;; 同じものを default-time-zone で出してみます
(map #(vector % (l/format-local-time "2013-06-23" (f/formatters (t/default-time-zone) %))) (keys f/formatters))
(f/unparse (f/formatter (t/default-time-zone) "YYYY-MM-dd" "YYYY/MM/dd") (t/date-time 2013 6 23))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment