Update upstream source from tag 'upstream/0.15.2'
Update to upstream version '0.15.2'
with Debian dir 2931db14a75f939a2405c41506f4982ab802e07a
Louis-Philippe VĂ©ronneau
3 years ago
0 | ## Changes Between 0.15.1 and 0.15.2 | |
1 | ||
2 | * Add type hints to reduce reflection warnings for users (#268, #269, @vemv) | |
3 | * Fix `overlaps?` with four arguments (#264). | |
4 | * Improve tests and documentation (#258, #259, #260, @bhaskarsaraogi) | |
5 | ||
6 | ## Changes Between 0.15.0 and 0.15.1 | |
7 | ||
8 | * Add `adjust` for intervals, so they can be "moved" backwards and forwards by one or more periods (#257). | |
9 | ||
10 | ## Changes Between 0.14.5 and 0.15.0 | |
11 | ||
12 | * Add tagged literal support (in `clj-time.coerce`) (#255, @jconti). `DateTime` can be converted `to-edn` and can be printed as `#clj-time/date-time "..."`, and `data_readers.clj` provides the conversion from that back to `DateTime`. | |
13 | ||
14 | ## Changes Between 0.14.4 and 0.14.5 | |
15 | ||
16 | * Update Joda Time to 2.10 (#254, @novalis). | |
17 | * Update various dev/test dependencies; clean up `spec_test.clj` to avoid Clojure refer warnings. | |
18 | * Drop `oraclejdk7` from test matrix (we still support `openjdk7`); add `openjdk8`; test Clojure 1.10 on Java 8+ only (#253). | |
19 | * Switch to versions.deps.co for tracking outdated dependencies (#252, @v-kolesnikov). | |
20 | ||
21 | ## Changes Between 0.14.3 and 0.14.4 | |
22 | ||
23 | * On Clojure 1.9 and later, `ReadableInstant` now extends to `Inst`, providing `inst?` and `inst-ms` on most Joda Time types (#248, #249) | |
24 | ||
25 | ## Changes Between 0.14.2 and 0.14.3 | |
26 | ||
27 | * Added deprecation notice and recommendation to move to Java Time (and [clojure.java-time](https://github.com/dm3/clojure.java-time)). | |
28 | * Clarified behavior of functions when passed `nil` (due to how Joda Time handles `null` objects). | |
29 | * Update to Joda Time 2.9.9. | |
30 | * Drop Clojure 1.6.0 support. | |
31 | * Refactor code to be more idiomatic (mostly `when` instead of `if`). | |
32 | ||
33 | * Fully-qualify function return type hints to avoid requiring imports on use (#241). | |
34 | ||
35 | ## Changes Between 0.14.1 and 0.14.2 | |
36 | ||
37 | * Fully-qualify function return type hints to avoid requiring imports on use (#241). | |
38 | ||
39 | ## Changes Between 0.14.0 and 0.14.1 | |
40 | ||
41 | * Switches `clojure.spec` to `clojure.spec.alpha` to work with latest Clojure 1.9 builds. | |
42 | * Adds `week-year` to go with `week-number-of-year` (#239, #240). | |
43 | * Adds function return type hints across the board (#226). | |
44 | ||
0 | 45 | ## Changes Between 0.13.0 and 0.14.0 |
1 | 46 | |
2 | 47 | * Add `from-epoch`. |
0 | # `clj-time` <a href="http://travis-ci.org/#!/clj-time/clj-time/builds"><img src="https://secure.travis-ci.org/clj-time/clj-time.png" /></a> [![Dependency Status](https://www.versioneye.com/clojure/clj-time:clj-time/badge.png)](https://www.versioneye.com/clojure/clj-time:clj-time) [![Join the chat at https://gitter.im/clj-time/clj-time](https://badges.gitter.im/clj-time/clj-time.svg)](https://gitter.im/clj-time/clj-time?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | |
1 | ||
0 | [![Build Status](https://travis-ci.org/clj-time/clj-time.svg?branch=master)](https://travis-ci.org/clj-time/clj-time) | |
1 | [![Dependencies Status](https://versions.deps.co/clj-time/clj-time/status.svg)](https://versions.deps.co/clj-time/clj-time) | |
2 | [![Downloads](https://versions.deps.co/clj-time/clj-time/downloads.svg)](https://versions.deps.co/clj-time/clj-time) | |
3 | [![Join the chat at https://gitter.im/clj-time/clj-time](https://badges.gitter.im/clj-time/clj-time.svg)](https://gitter.im/clj-time/clj-time?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | |
4 | ||
5 | # clj-time | |
2 | 6 | |
3 | 7 | A date and time library for Clojure, wrapping the [Joda Time](http://www.joda.org/joda-time/) library. |
8 | ||
9 | ## Project Status | |
10 | ||
11 | **The Joda Time website says:** | |
12 | ||
13 | > Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project. | |
14 | ||
15 | **If you are using Java 8 or later, consider using the built-in Java Time instead of Joda Time -- and look at [clojure.java-time](https://github.com/dm3/clojure.java-time) if you want a Clojure wrapper for that, or [cljc.java-time](https://github.com/henryw374/cljc.java-time) for a thin Clojure(Script) wrapper. See [Converting from Joda Time to java.time](http://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html) for more details about the similarities and differences between the two libraries.** | |
4 | 16 | |
5 | 17 | ## Artifacts |
6 | 18 | |
19 | 31 | |
20 | 32 | With Leiningen: |
21 | 33 | |
22 | ``` clj | |
23 | [clj-time "0.14.0"] | |
34 | ``` | |
35 | [clj-time "0.15.2"] | |
24 | 36 | ``` |
25 | 37 | |
26 | 38 | With Maven: |
29 | 41 | <dependency> |
30 | 42 | <groupId>clj-time</groupId> |
31 | 43 | <artifactId>clj-time</artifactId> |
32 | <version>0.14.0</version> | |
44 | <version>0.15.2</version> | |
33 | 45 | </dependency> |
34 | 46 | ``` |
35 | 47 | |
36 | 48 | ## Bugs and Enhancements |
37 | 49 | |
38 | Please open issues against the [official clj-time repo on Github](https://github.com/clj-time/clj-time/issues). | |
50 | Please open issues against the [official clj-time repo on Github](https://github.com/clj-time/clj-time/issues). `clj-time` is a very thin wrapper around Joda Time. That means that if Joda Time has a "peculiar behavior", it's likely to be surfaced directly in `clj-time` as well. A good example of this is `clj-time.format/unparse` which simply calls Joda Time's `.print` method -- and if the date passed in happens to be `nil`, you silently get back the _current date/time_ (many people would expect an exception!). | |
39 | 51 | |
40 | 52 | ## Mailing List |
41 | 53 | |
137 | 149 | => #<DateTime 1986-12-05T00:00:00.000Z> |
138 | 150 | ``` |
139 | 151 | |
152 | You can perform also `minus` operations | |
153 | ```clj | |
154 | (t/minus (t/date-time 1986 10 14 6) (t/hours 2)) | |
155 | => #<DateTime 1986-10-14T04:00:00.000Z> | |
156 | ``` | |
157 | ||
158 | ||
140 | 159 | An `Interval` is used to represent the span of time between two |
141 | 160 | `DateTime` instances. Construct one using `interval`, then query them |
142 | 161 | using `within?`, `overlaps?`, and `abuts?` |
231 | 250 | => "2012-02-01" |
232 | 251 | ``` |
233 | 252 | |
253 | Note: Joda Time's `.print` method accepts a null date/time object and substitutes the current date/time, so `(f/unparse my-fmt nil)` will not throw an exception -- it will just silently return the current date/time! | |
254 | ||
234 | 255 | ### clj-time.coerce |
235 | 256 | |
236 | 257 | The namespace `clj-time.coerce` contains utility functions for |
237 | 258 | coercing Joda `DateTime` instances to and from various other types: |
238 | 259 | |
239 | 260 | |
240 | ``` clj | |
261 | ```clojure | |
241 | 262 | (require '[clj-time.coerce :as c]) |
242 | 263 | ``` |
243 | 264 | |
265 | 286 | `java.sql.Timestamp` (`to-sql-time` and `from-sql-time`) and several |
266 | 287 | other types. |
267 | 288 | |
289 | To support serialization to the ubiquitous | |
290 | [EDN format](https://github.com/edn-format/edn), | |
291 | `pr`, `prn` etc. will serialize Joda `DateTime` in a tagged-literal format, | |
292 | that `clojure.edn/read` will deserialize. There is a `data_readers.clj` | |
293 | file, or if not loaded a `data-readers` var to use with `clojure.edn`. | |
294 | ||
295 | ```clojure | |
296 | (pr-str (t/date-time 1998 4 25)) | |
297 | => "#clj-time/date-time \"1998-04-25T00:00:00.000Z\"" | |
298 | ``` | |
299 | ||
300 | ```clojure | |
301 | (require '[clojure.edn :as edn]) | |
302 | => nil | |
303 | ``` | |
304 | ||
305 | ```clojure | |
306 | (def x (edn/read-string {:readers c/data-readers} | |
307 | (pr-str (t/date-time 1998 4 25)))) | |
308 | ``` | |
309 | ||
310 | ```clojure | |
311 | (type x) | |
312 | => org.joda.time.DateTime | |
313 | ``` | |
314 | ||
315 | ```clj | |
316 | x | |
317 | => #clj-time/date-time "1998-04-25T00:00:00.000Z" | |
318 | ``` | |
319 | ||
320 | ||
268 | 321 | ### clj-time.local |
269 | 322 | |
270 | 323 | The namespace `clj-time.local` contains functions for working with |
335 | 388 | (require '[clj-time.core :as t]) |
336 | 389 | (require '[clj-time.predicates :as pr]) |
337 | 390 | ``` |
338 | ``` clojure | |
391 | ```clojure | |
339 | 392 | (pr/monday? (t/date-time 1999 9 9)) |
340 | 393 | => false |
341 | 394 | |
384 | 437 | |
385 | 438 | Running the tests: |
386 | 439 | |
387 | $ rm -f test/readme.clj && lein test-all && lein test-readme | |
440 | $ rm -f test/readme.clj && lein test-all | |
388 | 441 | |
389 | 442 | (assumes Leiningen 2.x) |
390 | 443 |
0 | (defproject clj-time/clj-time "0.14.0" | |
0 | (def java7? (.startsWith (System/getProperty "java.version") "1.7")) | |
1 | ||
2 | (defproject clj-time/clj-time "0.15.2" | |
1 | 3 | :description "A date and time library for Clojure, wrapping Joda Time." |
2 | 4 | :url "https://github.com/clj-time/clj-time" |
3 | 5 | :mailing-list {:name "clj-time mailing list" |
6 | 8 | :license {:name "MIT License" |
7 | 9 | :url "http://www.opensource.org/licenses/mit-license.php" |
8 | 10 | :distribution :repo} |
9 | :dependencies [[joda-time "2.9.7"] | |
10 | [org.clojure/clojure "1.8.0" :scope "provided"]] | |
11 | :dependencies [[joda-time "2.10"] | |
12 | [org.clojure/clojure "1.10.0" :scope "provided"]] | |
11 | 13 | :min-lein-version "2.0.0" |
12 | 14 | :global-vars {*warn-on-reflection* true} |
13 | :profiles {:dev {:dependencies [[org.clojure/java.jdbc "0.6.1"]] | |
14 | :plugins [[codox "0.8.10"]]} | |
15 | :midje {:dependencies [[midje "1.9.0-alpha5"]] | |
15 | :profiles {:dev {:dependencies [[org.clojure/java.jdbc "0.7.9"]] | |
16 | :plugins [[codox "0.10.6"]]} | |
17 | :midje {:dependencies [[midje "1.9.8"]] | |
16 | 18 | :plugins [[lein-midje "3.2.1"] |
17 | 19 | [midje-readme "1.0.9"]] |
18 | 20 | :midje-readme {:require "[clj-time.core :as t] [clj-time.predicates :as pr] [clj-time.format :as f] [clj-time.coerce :as c]"}} |
19 | :1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} | |
20 | 21 | :1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]} |
22 | :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]} | |
23 | :1.9 {:dependencies [[org.clojure/clojure "1.9.0"] | |
24 | [org.clojure/test.check "0.10.0-alpha4"]] | |
25 | :test-paths ["test" "test_spec"]} | |
26 | :1.10 {:dependencies [[org.clojure/clojure "1.10.0"] | |
27 | [org.clojure/test.check "0.10.0-alpha4"]] | |
28 | :test-paths ["test" "test_spec"]} | |
21 | 29 | :master {:repositories [["snapshots" "https://oss.sonatype.org/content/repositories/snapshots/"]] |
22 | :dependencies [[org.clojure/clojure "1.9.0-master-SNAPSHOT"]]} | |
23 | :spec {:dependencies [[org.clojure/clojure "1.9.0-alpha17"] | |
24 | [org.clojure/test.check "0.9.0"]] | |
25 | :test-paths ["test" "test_clj_1.9"]}} | |
30 | :dependencies [[org.clojure/clojure "1.11.0-master-SNAPSHOT"] | |
31 | [org.clojure/test.check "0.10.0-alpha4"]] | |
32 | :test-paths ["test" "test_spec"]}} | |
26 | 33 | |
27 | :aliases {"test-all" ["with-profile" "dev,master,default,midje:dev,default,midje:dev,1.6,midje:dev,1.7,midje" "test"]}) | |
34 | :aliases {"test-all" ["with-profile" | |
35 | ~(str (when-not java7? | |
36 | ;; 1.10+ requires Java 8+ | |
37 | (str "dev,master,midje:" ; 1.11 with spec | |
38 | "dev,1.10,midje:" ; 1.10 with spec | |
39 | "dev,default,midje:")) ; 1.10 without spec | |
40 | "dev,1.9,midje:" ; 1.9 with spec | |
41 | "dev,1.8,midje:" ; 1.8 is supported too | |
42 | "dev,1.7,midje") ; 1.7 is earliest we support | |
43 | "test"]}) |
9 | 9 | (:refer-clojure :exclude [extend second]) |
10 | 10 | (:require [clj-time.core :refer :all] |
11 | 11 | [clj-time.format :as time-fmt]) |
12 | (:import [java.sql Timestamp] | |
12 | (:import [java.io Writer] | |
13 | [java.sql Timestamp] | |
13 | 14 | [java.util Date] |
14 | 15 | [org.joda.time DateTime DateTimeZone DateMidnight YearMonth |
15 | 16 | LocalDate LocalDateTime])) |
16 | 17 | |
17 | 18 | (defprotocol ICoerce |
18 | (^org.joda.time.DateTime | |
19 | to-date-time [obj] "Convert `obj` to a Joda DateTime instance.")) | |
19 | (to-date-time ^org.joda.time.DateTime [obj] "Convert `obj` to a Joda DateTime instance.")) | |
20 | 20 | |
21 | 21 | (defn from-long |
22 | 22 | "Returns a DateTime instance in the UTC time zone corresponding to the given |
23 | 23 | number of milliseconds after the Unix epoch." |
24 | ^org.joda.time.DateTime | |
24 | 25 | [^Long millis] |
25 | 26 | (DateTime. millis ^DateTimeZone utc)) |
26 | ||
27 | 27 | |
28 | 28 | (defn from-epoch |
29 | 29 | "Returns a DateTime instance in the UTC time zone |
30 | 30 | from given Unix epoch." |
31 | ^org.joda.time.DateTime | |
31 | 32 | [^Long epoch] |
32 | 33 | (from-long (* epoch 1000))) |
33 | ||
34 | 34 | |
35 | 35 | (defn from-string |
36 | 36 | "return DateTime instance from string using |
37 | 37 | formatters in clj-time.format, returning first |
38 | 38 | which parses" |
39 | ^org.joda.time.DateTime | |
39 | 40 | [^String s] |
40 | 41 | (time-fmt/parse s)) |
41 | 42 | |
43 | (def data-readers | |
44 | "tagged literal support if loader does not find \"data_readers.clj\"" | |
45 | {'clj-time/date-time from-string}) | |
46 | ||
42 | 47 | (defn from-date |
43 | 48 | "Returns a DateTime instance in the UTC time zone corresponding to the given |
44 | 49 | Java Date object." |
45 | [^Date date] | |
50 | ^org.joda.time.DateTime | |
51 | [^java.util.Date date] | |
46 | 52 | (when date |
47 | 53 | (from-long (.getTime date)))) |
48 | 54 | |
49 | 55 | (defn from-sql-date |
50 | 56 | "Returns a DateTime instance in the UTC time zone corresponding to the given |
51 | 57 | java.sql.Date object." |
58 | ^org.joda.time.DateTime | |
52 | 59 | [^java.sql.Date sql-date] |
53 | 60 | (when sql-date |
54 | 61 | (from-long (.getTime sql-date)))) |
56 | 63 | (defn from-sql-time |
57 | 64 | "Returns a DateTime instance in the UTC time zone corresponding to the given |
58 | 65 | java.sql.Timestamp object." |
66 | ^org.joda.time.DateTime | |
59 | 67 | [^java.sql.Timestamp sql-time] |
60 | 68 | (when sql-time |
61 | 69 | (from-long (.getTime sql-time)))) |
62 | 70 | |
63 | 71 | (defn to-long |
64 | 72 | "Convert `obj` to the number of milliseconds after the Unix epoch." |
65 | [obj] | |
66 | (if-let [dt (to-date-time obj)] | |
73 | ^Long | |
74 | [obj] | |
75 | (when-let [dt (to-date-time obj)] | |
67 | 76 | (.getMillis dt))) |
68 | 77 | |
69 | 78 | (defn to-epoch |
70 | 79 | "Convert `obj` to Unix epoch." |
71 | [obj] | |
72 | (let [millis (to-long obj)] | |
73 | (and millis (quot millis 1000)))) | |
80 | ^Long | |
81 | [obj] | |
82 | (when-let [millis (to-long obj)] | |
83 | (quot millis 1000))) | |
74 | 84 | |
75 | 85 | (defn to-date |
76 | 86 | "Convert `obj` to a Java Date instance." |
77 | [obj] | |
78 | (if-let [dt (to-date-time obj)] | |
87 | ^java.util.Date | |
88 | [obj] | |
89 | (when-let [dt (to-date-time obj)] | |
79 | 90 | (Date. (.getMillis dt)))) |
80 | 91 | |
81 | 92 | (defn to-sql-date |
82 | 93 | "Convert `obj` to a java.sql.Date instance." |
83 | [obj] | |
84 | (if-let [dt (to-date-time obj)] | |
94 | ^java.sql.Date | |
95 | [obj] | |
96 | (when-let [dt (to-date-time obj)] | |
85 | 97 | (java.sql.Date. (.getMillis dt)))) |
86 | 98 | |
87 | 99 | (defn to-sql-time |
88 | 100 | "Convert `obj` to a java.sql.Timestamp instance." |
89 | [obj] | |
90 | (if-let [dt (to-date-time obj)] | |
101 | ^java.sql.Timestamp | |
102 | [obj] | |
103 | (when-let [dt (to-date-time obj)] | |
91 | 104 | (java.sql.Timestamp. (.getMillis dt)))) |
92 | 105 | |
93 | 106 | (defn to-string |
94 | 107 | "Returns a string representation of obj in UTC time-zone |
95 | 108 | using (ISODateTimeFormat/dateTime) date-time representation." |
96 | [obj] | |
97 | (if-let [^DateTime dt (to-date-time obj)] | |
109 | ^String | |
110 | [obj] | |
111 | (when-let [dt (to-date-time obj)] | |
98 | 112 | (time-fmt/unparse (:date-time time-fmt/formatters) dt))) |
113 | ||
114 | (defn to-edn | |
115 | "Convert `obj` to a string representation readable by clojure.edn/read." | |
116 | ^String | |
117 | [obj] | |
118 | (when-let [dt (to-date-time obj)] | |
119 | (str "#clj-time/date-time \"" (to-string dt) "\""))) | |
120 | ||
121 | ;; pr and prn support to write edn | |
122 | (defmethod print-method org.joda.time.DateTime | |
123 | [v ^java.io.Writer w] | |
124 | (.write w (to-edn v))) | |
99 | 125 | |
100 | 126 | (defn to-timestamp |
101 | 127 | "Convert `obj` to a Java SQL Timestamp instance." |
102 | [obj] | |
103 | (if-let [dt (to-date-time obj)] | |
104 | (Timestamp. (.getMillis dt)))) | |
128 | ^java.sql.Timestamp | |
129 | [obj] | |
130 | (when-let [dt (to-date-time obj)] | |
131 | (java.sql.Timestamp. (.getMillis dt)))) | |
105 | 132 | |
106 | 133 | (defn to-local-date |
107 | 134 | "Convert `obj` to a org.joda.time.LocalDate instance" |
108 | [obj] | |
109 | (if-let [dt (to-date-time obj)] | |
135 | ^org.joda.time.LocalDate | |
136 | [obj] | |
137 | (when-let [dt (to-date-time obj)] | |
110 | 138 | (LocalDate. (.getMillis (from-time-zone dt (default-time-zone)))))) |
111 | 139 | |
112 | 140 | (defn to-local-date-time |
113 | 141 | "Convert `obj` to a org.joda.time.LocalDateTime instance" |
114 | [obj] | |
115 | (if-let [dt (to-date-time obj)] | |
142 | ^org.joda.time.LocalDateTime | |
143 | [obj] | |
144 | (when-let [dt (to-date-time obj)] | |
116 | 145 | (LocalDateTime. (.getMillis (from-time-zone dt (default-time-zone)))))) |
117 | 146 | |
118 | 147 | (defn in-time-zone |
119 | 148 | "Convert `obj` into `tz`, return org.joda.time.LocalDate instance." |
149 | ^org.joda.time.LocalDate | |
120 | 150 | [obj tz] |
121 | (if-let [dt (to-date-time obj)] | |
151 | (when-let [dt (to-date-time obj)] | |
122 | 152 | (-> dt |
123 | 153 | (to-time-zone tz) |
124 | 154 | .toLocalDate))) |
174 | 204 | (to-date-time [string] |
175 | 205 | (from-string string)) |
176 | 206 | |
177 | Timestamp | |
207 | java.sql.Timestamp | |
178 | 208 | (to-date-time [timestamp] |
179 | 209 | (from-date timestamp))) |
118 | 118 | "Returns a new date/time corresponding to the given date/time moved backwards by the given Period(s).") |
119 | 119 | (first-day-of-the-month- [this] "Returns the first day of the month") |
120 | 120 | (last-day-of-the-month- [this] "Returns the last day of the month") |
121 | (week-number-of-year [this] "Returs the number of weeks in the year")) | |
121 | (week-number-of-year [this] "Returns the week of the week based year of the given date/time") | |
122 | (week-year [this] "Returns the the week based year of the given date/time.")) | |
122 | 123 | |
123 | 124 | (defprotocol InTimeUnitProtocol |
124 | 125 | "Interface for in-<time unit> functions" |
156 | 157 | (.. ^DateTime this dayOfMonth withMaximumValue)) |
157 | 158 | (week-number-of-year [this] |
158 | 159 | (.getWeekOfWeekyear this)) |
160 | (week-year [this] (.getWeekyear this)) | |
159 | 161 | |
160 | 162 | org.joda.time.DateMidnight |
161 | 163 | (year [this] (.getYear this)) |
181 | 183 | (.. ^DateMidnight this dayOfMonth withMaximumValue)) |
182 | 184 | (week-number-of-year [this] |
183 | 185 | (.getWeekOfWeekyear this)) |
186 | (week-year [this] (.getWeekyear this)) | |
184 | 187 | |
185 | 188 | org.joda.time.LocalDateTime |
186 | 189 | (year [this] (.getYear this)) |
206 | 209 | (.. ^LocalDateTime this dayOfMonth withMaximumValue)) |
207 | 210 | (week-number-of-year [this] |
208 | 211 | (.getWeekOfWeekyear this)) |
212 | (week-year [this] (.getWeekyear this)) | |
209 | 213 | |
210 | 214 | org.joda.time.YearMonth |
211 | 215 | (year [this] (.getYear this)) |
232 | 236 | (.. ^LocalDate this dayOfMonth withMaximumValue)) |
233 | 237 | (week-number-of-year [this] |
234 | 238 | (.getWeekOfWeekyear this)) |
239 | (week-year [this] (.getWeekyear this)) | |
235 | 240 | |
236 | 241 | org.joda.time.LocalTime |
237 | 242 | (hour [this] (.getHourOfDay this)) |
251 | 256 | |
252 | 257 | (defn now |
253 | 258 | "Returns a DateTime for the current instant in the UTC time zone." |
259 | ^org.joda.time.DateTime | |
254 | 260 | [] |
255 | 261 | (DateTime. ^DateTimeZone utc)) |
256 | 262 | |
257 | 263 | (defn time-now |
258 | 264 | "Returns a LocalTime for the current instant without date or time zone |
259 | 265 | using ISOChronology in the current time zone." |
266 | ^org.joda.time.LocalTime | |
260 | 267 | [] |
261 | 268 | (LocalTime. )) |
262 | 269 | |
268 | 275 | ([^DateTimeZone tz] |
269 | 276 | (DateMidnight. tz))) |
270 | 277 | |
271 | (defn ^DateTime with-time-at-start-of-day | |
278 | (defn with-time-at-start-of-day | |
272 | 279 | "Returns a DateTime representing the start of the day. Normally midnight, |
273 | 280 | but not always true, as in some time zones with daylight savings." |
281 | ^org.joda.time.DateTime | |
274 | 282 | [^DateTime dt] |
275 | 283 | (.withTimeAtStartOfDay dt)) |
276 | 284 | |
277 | 285 | (defn epoch |
278 | 286 | "Returns a DateTime for the beginning of the Unix epoch in the UTC time zone." |
287 | ^org.joda.time.DateTime | |
279 | 288 | [] |
280 | 289 | (DateTime. (long 0) ^DateTimeZone utc)) |
281 | 290 | |
284 | 293 | Specify the year, month of year, day of month. Note that month and day are |
285 | 294 | 1-indexed. Any number of least-significant components can be ommited, in which case |
286 | 295 | they will default to 1." |
287 | ([year] | |
288 | (date-midnight year 1 1)) | |
289 | ([^long year ^long month] | |
290 | (date-midnight year month 1)) | |
291 | ([^Long year ^Long month ^Long day] | |
292 | (DateMidnight. year month day ^DateTimeZone utc))) | |
296 | (^DateMidnight [year] | |
297 | (date-midnight year 1 1)) | |
298 | (^DateMidnight [^long year ^long month] | |
299 | (date-midnight year month 1)) | |
300 | (^DateMidnight [^Long year ^Long month ^Long day] | |
301 | (DateMidnight. year month day ^DateTimeZone utc))) | |
293 | 302 | |
294 | 303 | (defn min-date |
295 | 304 | "Minimum of the provided DateTimes." |
301 | 310 | [dt & dts] |
302 | 311 | (reduce #(if (after? %1 %2) %1 %2) dt dts)) |
303 | 312 | |
304 | (defn ^DateTime date-time | |
313 | (defn date-time | |
305 | 314 | "Constructs and returns a new DateTime in UTC. |
306 | 315 | Specify the year, month of year, day of month, hour of day, minute of hour, |
307 | 316 | second of minute, and millisecond of second. Note that month and day are |
308 | 317 | 1-indexed while hour, second, minute, and millis are 0-indexed. |
309 | 318 | Any number of least-significant components can be ommited, in which case |
310 | 319 | they will default to 1 or 0 as appropriate." |
311 | ([year] | |
320 | (^org.joda.time.DateTime [year] | |
312 | 321 | (date-time year 1 1 0 0 0 0)) |
313 | ([year month] | |
322 | (^org.joda.time.DateTime [year month] | |
314 | 323 | (date-time year month 1 0 0 0 0)) |
315 | ([year month day] | |
324 | (^org.joda.time.DateTime [year month day] | |
316 | 325 | (date-time year month day 0 0 0 0)) |
317 | ([year month day hour] | |
326 | (^org.joda.time.DateTime [year month day hour] | |
318 | 327 | (date-time year month day hour 0 0 0)) |
319 | ([year month day hour minute] | |
328 | (^org.joda.time.DateTime [year month day hour minute] | |
320 | 329 | (date-time year month day hour minute 0 0)) |
321 | ([year month day hour minute second] | |
330 | (^org.joda.time.DateTime [year month day hour minute second] | |
322 | 331 | (date-time year month day hour minute second 0)) |
323 | ([^Integer year ^Integer month ^Integer day ^Integer hour | |
324 | ^Integer minute ^Integer second ^Integer millis] | |
332 | (^org.joda.time.DateTime [^Integer year ^Integer month ^Integer day ^Integer hour | |
333 | ^Integer minute ^Integer second ^Integer millis] | |
325 | 334 | (DateTime. year month day hour minute second millis ^DateTimeZone utc))) |
326 | 335 | |
327 | (defn ^org.joda.time.LocalDateTime local-date-time | |
336 | (defn local-date-time | |
328 | 337 | "Constructs and returns a new LocalDateTime. |
329 | 338 | Specify the year, month of year, day of month, hour of day, minute of hour, |
330 | 339 | second of minute, and millisecond of second. Note that month and day are |
331 | 340 | 1-indexed while hour, second, minute, and millis are 0-indexed. |
332 | 341 | Any number of least-significant components can be ommited, in which case |
333 | 342 | they will default to 1 or 0 as appropriate." |
334 | ([year] | |
343 | (^org.joda.time.LocalDateTime [year] | |
335 | 344 | (local-date-time year 1 1 0 0 0 0)) |
336 | ([year month] | |
345 | (^org.joda.time.LocalDateTime [year month] | |
337 | 346 | (local-date-time year month 1 0 0 0 0)) |
338 | ([year month day] | |
347 | (^org.joda.time.LocalDateTime [year month day] | |
339 | 348 | (local-date-time year month day 0 0 0 0)) |
340 | ([year month day hour] | |
349 | (^org.joda.time.LocalDateTime [year month day hour] | |
341 | 350 | (local-date-time year month day hour 0 0 0)) |
342 | ([year month day hour minute] | |
351 | (^org.joda.time.LocalDateTime [year month day hour minute] | |
343 | 352 | (local-date-time year month day hour minute 0 0)) |
344 | ([year month day hour minute second] | |
353 | (^org.joda.time.LocalDateTime [year month day hour minute second] | |
345 | 354 | (local-date-time year month day hour minute second 0)) |
346 | ([^Integer year ^Integer month ^Integer day ^Integer hour | |
347 | ^Integer minute ^Integer second ^Integer millis] | |
355 | (^org.joda.time.LocalDateTime [^Integer year ^Integer month ^Integer day ^Integer hour | |
356 | ^Integer minute ^Integer second ^Integer millis] | |
348 | 357 | (LocalDateTime. year month day hour minute second millis))) |
349 | 358 | |
350 | (defn ^org.joda.time.YearMonth year-month | |
359 | (defn year-month | |
351 | 360 | "Constructs and returns a new YearMonth. |
352 | 361 | Specify the year and month of year. Month is 1-indexed and defaults |
353 | 362 | to January (1)." |
354 | ([year] | |
355 | (year-month year 1)) | |
356 | ([^Integer year ^Integer month] | |
357 | (YearMonth. year month))) | |
358 | ||
359 | (defn ^org.joda.time.LocalDate local-date | |
363 | (^org.joda.time.YearMonth [year] | |
364 | (year-month year 1)) | |
365 | (^org.joda.time.YearMonth [^Integer year ^Integer month] | |
366 | (YearMonth. year month))) | |
367 | ||
368 | (defn local-date | |
360 | 369 | "Constructs and returns a new LocalDate. |
361 | 370 | Specify the year, month, and day. Does not deal with timezones." |
371 | ^org.joda.time.LocalDate | |
362 | 372 | [^Integer year ^Integer month ^Integer day] |
363 | 373 | (LocalDate. year month day)) |
364 | 374 | |
365 | (defn ^org.joda.time.LocalTime local-time | |
375 | (defn local-time | |
366 | 376 | "Constructs and returns a new LocalTime. |
367 | 377 | Specify the hour of day, minute of hour, second of minute, and millisecond of second. |
368 | 378 | Any number of least-significant components can be ommited, in which case |
369 | 379 | they will default to 1 or 0 as appropriate." |
370 | ([hour] | |
380 | (^org.joda.time.LocalTime [hour] | |
371 | 381 | (local-time hour 0 0 0)) |
372 | ([hour minute] | |
382 | (^org.joda.time.LocalTime [hour minute] | |
373 | 383 | (local-time hour minute 0 0)) |
374 | ([hour minute second] | |
384 | (^org.joda.time.LocalTime [hour minute second] | |
375 | 385 | (local-time hour minute second 0)) |
376 | ([^Integer hour ^Integer minute ^Integer second ^Integer millis] | |
386 | (^org.joda.time.LocalTime [^Integer hour ^Integer minute ^Integer second ^Integer millis] | |
377 | 387 | (LocalTime. hour minute second millis)) |
378 | 388 | ) |
379 | 389 | |
380 | (defn ^org.joda.time.LocalDate today | |
390 | (defn today | |
381 | 391 | "Constructs and returns a new LocalDate representing today's date. |
382 | 392 | LocalDate objects do not deal with timezones at all." |
393 | ^org.joda.time.LocalDate | |
383 | 394 | [] |
384 | 395 | (LocalDate.)) |
385 | 396 | |
386 | 397 | (defn time-zone-for-offset |
387 | 398 | "Returns a DateTimeZone for the given offset, specified either in hours or |
388 | 399 | hours and minutes." |
389 | ([hours] | |
400 | (^org.joda.time.DateTimeZone [hours] | |
390 | 401 | (DateTimeZone/forOffsetHours hours)) |
391 | ([hours minutes] | |
402 | (^org.joda.time.DateTimeZone [hours minutes] | |
392 | 403 | (DateTimeZone/forOffsetHoursMinutes hours minutes))) |
393 | 404 | |
394 | 405 | (defn time-zone-for-id |
395 | 406 | "Returns a DateTimeZone for the given ID, which must be in long form, e.g. |
396 | 407 | 'America/Matamoros'." |
408 | ^org.joda.time.DateTimeZone | |
397 | 409 | [^String id] |
398 | 410 | (DateTimeZone/forID id)) |
399 | 411 | |
404 | 416 | |
405 | 417 | (defn default-time-zone |
406 | 418 | "Returns the default DateTimeZone for the current environment." |
419 | ^org.joda.time.DateTimeZone | |
407 | 420 | [] |
408 | 421 | (DateTimeZone/getDefault)) |
409 | 422 | |
410 | (defn ^org.joda.time.DateTime | |
411 | to-time-zone | |
423 | (defn to-time-zone | |
412 | 424 | "Returns a new ReadableDateTime corresponding to the same absolute instant in time as |
413 | 425 | the given ReadableDateTime, but with calendar fields corresponding to the given |
414 | 426 | TimeZone." |
427 | ^org.joda.time.DateTime | |
415 | 428 | [^DateTime dt ^DateTimeZone tz] |
416 | 429 | (.withZone dt tz)) |
417 | 430 | |
418 | (defn ^org.joda.time.DateTime | |
419 | from-time-zone | |
431 | (defn from-time-zone | |
420 | 432 | "Returns a new ReadableDateTime corresponding to the same point in calendar time as |
421 | 433 | the given ReadableDateTime, but for a correspondingly different absolute instant in |
422 | 434 | time." |
435 | ^org.joda.time.DateTime | |
423 | 436 | [^DateTime dt ^DateTimeZone tz] |
424 | 437 | (.withZoneRetainFields dt tz)) |
425 | 438 | |
426 | 439 | (defn years |
427 | 440 | "Given a number, returns a Period representing that many years. |
428 | 441 | Without an argument, returns a PeriodType representing only years." |
429 | ([] | |
430 | (PeriodType/years)) | |
431 | ([^Integer n] | |
432 | (Years/years n))) | |
442 | (^org.joda.time.PeriodType [] | |
443 | (PeriodType/years)) | |
444 | (^org.joda.time.Years [^Integer n] | |
445 | (Years/years n))) | |
433 | 446 | |
434 | 447 | (defn months |
435 | 448 | "Given a number, returns a Period representing that many months. |
436 | 449 | Without an argument, returns a PeriodType representing only months." |
437 | ([] | |
438 | (PeriodType/months)) | |
439 | ([^Integer n] | |
440 | (Months/months n))) | |
450 | (^org.joda.time.PeriodType [] | |
451 | (PeriodType/months)) | |
452 | (^org.joda.time.Months [^Integer n] | |
453 | (Months/months n))) | |
441 | 454 | |
442 | 455 | (defn weeks |
443 | 456 | "Given a number, returns a Period representing that many weeks. |
444 | 457 | Without an argument, returns a PeriodType representing only weeks." |
445 | ([] | |
446 | (PeriodType/weeks)) | |
447 | ([^Integer n] | |
448 | (Weeks/weeks n))) | |
458 | (^org.joda.time.PeriodType [] | |
459 | (PeriodType/weeks)) | |
460 | (^org.joda.time.Weeks [^Integer n] | |
461 | (Weeks/weeks n))) | |
449 | 462 | |
450 | 463 | (defn days |
451 | 464 | "Given a number, returns a Period representing that many days. |
452 | 465 | Without an argument, returns a PeriodType representing only days." |
453 | ([] | |
454 | (PeriodType/days)) | |
455 | ([^Integer n] | |
456 | (Days/days n))) | |
466 | (^org.joda.time.PeriodType [] | |
467 | (PeriodType/days)) | |
468 | (^org.joda.time.Days [^Integer n] | |
469 | (Days/days n))) | |
457 | 470 | |
458 | 471 | (defn hours |
459 | 472 | "Given a number, returns a Period representing that many hours. |
460 | 473 | Without an argument, returns a PeriodType representing only hours." |
461 | ([] | |
462 | (PeriodType/hours)) | |
463 | ([^Integer n] | |
464 | (Hours/hours n))) | |
474 | (^org.joda.time.PeriodType [] | |
475 | (PeriodType/hours)) | |
476 | (^org.joda.time.Hours [^Integer n] | |
477 | (Hours/hours n))) | |
465 | 478 | |
466 | 479 | (defn minutes |
467 | 480 | "Given a number, returns a Period representing that many minutes. |
468 | 481 | Without an argument, returns a PeriodType representing only minutes." |
469 | ([] | |
470 | (PeriodType/minutes)) | |
471 | ([^Integer n] | |
472 | (Minutes/minutes n))) | |
482 | (^org.joda.time.PeriodType [] | |
483 | (PeriodType/minutes)) | |
484 | (^org.joda.time.Minutes [^Integer n] | |
485 | (Minutes/minutes n))) | |
473 | 486 | |
474 | 487 | (defn seconds |
475 | 488 | "Given a number, returns a Period representing that many seconds. |
476 | 489 | Without an argument, returns a PeriodType representing only seconds." |
477 | ([] | |
478 | (PeriodType/seconds)) | |
479 | ([^Integer n] | |
480 | (Seconds/seconds n))) | |
490 | (^org.joda.time.PeriodType [] | |
491 | (PeriodType/seconds)) | |
492 | (^org.joda.time.Seconds [^Integer n] | |
493 | (Seconds/seconds n))) | |
481 | 494 | |
482 | 495 | (extend-protocol InTimeUnitProtocol |
483 | 496 | org.joda.time.Interval |
538 | 551 | (defn millis |
539 | 552 | "Given a number, returns a Period representing that many milliseconds. |
540 | 553 | Without an argument, returns a PeriodType representing only milliseconds." |
541 | ([] | |
542 | (PeriodType/millis)) | |
543 | ([^Integer n] | |
544 | (Period/millis n))) | |
554 | (^org.joda.time.PeriodType [] | |
555 | (PeriodType/millis)) | |
556 | (^org.joda.time.Period [^Integer n] | |
557 | (Period/millis n))) | |
545 | 558 | |
546 | 559 | (defn plus |
547 | 560 | "Returns a new date/time corresponding to the given date/time moved forwards by |
595 | 608 | (defn interval |
596 | 609 | "Returns an interval representing the span between the two given ReadableDateTimes. |
597 | 610 | Note that intervals are closed on the left and open on the right." |
598 | [^ReadableDateTime dt-a ^ReadableDateTime dt-b] | |
611 | ^Interval [^ReadableDateTime dt-a ^ReadableDateTime dt-b] | |
599 | 612 | (Interval. dt-a dt-b)) |
600 | 613 | |
601 | 614 | (defn start |
614 | 627 | [^Interval in & by] |
615 | 628 | (.withEnd in (apply plus (end in) by))) |
616 | 629 | |
630 | (defn adjust | |
631 | "Returns an Interval with the start and end adjusted by the specified Periods." | |
632 | [^Interval in & by] | |
633 | (interval (apply plus (start in) by) | |
634 | (apply plus (end in) by))) | |
635 | ||
636 | (comment | |
637 | (from-now (days 4)) | |
638 | (interval (now) (from-now (days 4))) | |
639 | (adjust (interval (now) (from-now (days 4))) (hours -4) (minutes 30))) | |
617 | 640 | |
618 | 641 | (defn within? |
619 | 642 | "With 2 arguments: Returns true if the given Interval contains the given |
639 | 662 | ([^ReadablePartial start-a ^ReadablePartial end-a |
640 | 663 | ^ReadablePartial start-b ^ReadablePartial end-b] |
641 | 664 | (or (and (before? start-b end-a) (after? end-b start-a)) |
642 | (and (after? end-b start-a) (before? start-b end-a)) | |
643 | (or (equal? start-a end-b) (equal? start-b end-a))))) | |
665 | (and (after? end-b start-a) (before? start-b end-a))))) | |
644 | 666 | |
645 | 667 | (defn overlap |
646 | 668 | "Returns an Interval representing the overlap of the specified Intervals. |
735 | 757 | (plus (first-day-of-the-month dt) |
736 | 758 | (days (- n 1))))) |
737 | 759 | |
738 | (defn ^DateTime today-at | |
760 | (defn ^org.joda.time.DateTime today-at | |
739 | 761 | ([^long hours ^long minutes ^long seconds ^long millis] |
740 | 762 | (let [^MutableDateTime mdt (.toMutableDateTime ^DateTime (now))] |
741 | 763 | (.toDateTime (doto mdt |
761 | 783 | `(do-at* ~base-date-time |
762 | 784 | (fn [] ~@body))) |
763 | 785 | |
764 | (defn ^DateTime floor | |
786 | (defn floor | |
765 | 787 | "Floors the given date-time dt to the given time unit dt-fn, |
766 | 788 | e.g. (floor (now) hour) returns (now) for all units |
767 | 789 | up to and including the hour" |
768 | ([^DateTime dt dt-fn] | |
790 | (^org.joda.time.DateTime [^DateTime dt dt-fn] | |
769 | 791 | (let [dt-fns [year month day hour minute second milli] |
770 | 792 | tz (.getZone dt)] |
771 | 793 | (.withZoneRetainFields |
775 | 797 | (concat (take-while (partial not= dt-fn) dt-fns) [dt-fn]) |
776 | 798 | (repeat [dt]))) |
777 | 799 | tz)))) |
800 | ||
801 | (defmacro ^:private when-available [sym & body] | |
802 | (when (resolve sym) | |
803 | `(do ~@body))) | |
804 | ||
805 | (when-available Inst | |
806 | (extend-protocol Inst | |
807 | org.joda.time.ReadableInstant | |
808 | (inst-ms* [inst] | |
809 | (.getMillis inst)))) |
39 | 39 | |
40 | 40 | (defn formatter |
41 | 41 | "Returns a custom formatter for the given date-time pattern or keyword." |
42 | ([fmts] | |
42 | (^org.joda.time.format.DateTimeFormatter [fmts] | |
43 | 43 | (formatter fmts utc)) |
44 | ([fmts ^DateTimeZone dtz] | |
44 | (^org.joda.time.format.DateTimeFormatter [fmts ^DateTimeZone dtz] | |
45 | 45 | (cond (keyword? fmts) (.withZone ^DateTimeFormatter (get formatters fmts) dtz) |
46 | 46 | (string? fmts) (.withZone (DateTimeFormat/forPattern fmts) dtz) |
47 | 47 | :else (.withZone ^DateTimeFormatter fmts dtz))) |
48 | ([^DateTimeZone dtz fmts & more] | |
48 | (^org.joda.time.format.DateTimeFormatter [^DateTimeZone dtz fmts & more] | |
49 | 49 | (let [printer (.getPrinter ^DateTimeFormatter (formatter fmts dtz)) |
50 | 50 | parsers (map #(.getParser ^DateTimeFormatter (formatter % dtz)) (cons fmts more))] |
51 | 51 | (-> (DateTimeFormatterBuilder.) |
57 | 57 | |
58 | 58 | (defn formatter-local |
59 | 59 | "Returns a custom formatter with no time zone info." |
60 | ([^String fmt] | |
60 | (^org.joda.time.format.DateTimeFormat [^String fmt] | |
61 | 61 | (DateTimeFormat/forPattern fmt))) |
62 | 62 | |
63 | 63 | (defn with-chronology |
64 | 64 | "Return a copy of a formatter that uses the given Chronology." |
65 | [^DateTimeFormatter f ^Chronology c] | |
65 | ^org.joda.time.format.DateTimeFormatter [^DateTimeFormatter f ^Chronology c] | |
66 | 66 | (.withChronology f c)) |
67 | 67 | |
68 | 68 | (defn with-locale |
69 | 69 | "Return a copy of a formatter that uses the given Locale." |
70 | [^DateTimeFormatter f ^Locale l] | |
70 | ^org.joda.time.format.DateTimeFormatter [^DateTimeFormatter f ^Locale l] | |
71 | 71 | (.withLocale f l)) |
72 | 72 | |
73 | 73 | (defn with-pivot-year |
74 | 74 | "Return a copy of a formatter that uses the given pivot year." |
75 | [^DateTimeFormatter f ^Long pivot-year] | |
75 | ^org.joda.time.format.DateTimeFormatter [^DateTimeFormatter f ^Long pivot-year] | |
76 | 76 | (.withPivotYear f pivot-year)) |
77 | 77 | |
78 | 78 | (defn with-zone |
79 | 79 | "Return a copy of a formatter that uses the given DateTimeZone." |
80 | [^DateTimeFormatter f ^DateTimeZone dtz] | |
80 | ^org.joda.time.format.DateTimeFormatter [^DateTimeFormatter f ^DateTimeZone dtz] | |
81 | 81 | (.withZone f dtz)) |
82 | 82 | |
83 | 83 | (defn with-default-year |
84 | 84 | "Return a copy of a formatter that uses the given default year." |
85 | [^DateTimeFormatter f ^Integer default-year] | |
85 | ^org.joda.time.format.DateTimeFormatter [^DateTimeFormatter f ^Integer default-year] | |
86 | 86 | (.withDefaultYear f default-year)) |
87 | 87 | |
88 | 88 | (def ^{:doc "Map of ISO 8601 and a single RFC 822 formatters that can be used for parsing and, in most |
155 | 155 | (defn parse |
156 | 156 | "Returns a DateTime instance in the UTC time zone obtained by parsing the |
157 | 157 | given string according to the given formatter." |
158 | ([^DateTimeFormatter fmt ^String s] | |
158 | (^org.joda.time.DateTime [^DateTimeFormatter fmt ^String s] | |
159 | 159 | (.parseDateTime fmt s)) |
160 | ([^String s] | |
160 | (^org.joda.time.DateTime [^String s] | |
161 | 161 | (first |
162 | 162 | (for [f (vals formatters) |
163 | 163 | :let [d (try (parse f s) (catch Exception _ nil))] |
166 | 166 | (defn parse-local |
167 | 167 | "Returns a LocalDateTime instance obtained by parsing the |
168 | 168 | given string according to the given formatter." |
169 | ([^DateTimeFormatter fmt ^String s] | |
169 | (^org.joda.time.LocalDateTime [^DateTimeFormatter fmt ^String s] | |
170 | 170 | (.parseLocalDateTime fmt s)) |
171 | ([^String s] | |
171 | (^org.joda.time.LocalDateTime [^String s] | |
172 | 172 | (first |
173 | 173 | (for [f (vals formatters) |
174 | 174 | :let [d (try (parse-local f s) (catch Exception _ nil))] |
177 | 177 | (defn parse-local-date |
178 | 178 | "Returns a LocalDate instance obtained by parsing the |
179 | 179 | given string according to the given formatter." |
180 | ([^DateTimeFormatter fmt ^String s] | |
180 | (^org.joda.time.LocalDate [^DateTimeFormatter fmt ^String s] | |
181 | 181 | (.parseLocalDate fmt s)) |
182 | ([^String s] | |
182 | (^org.joda.time.LocalDate [^String s] | |
183 | 183 | (first |
184 | 184 | (for [f (vals formatters) |
185 | 185 | :let [d (try (parse-local-date f s) (catch Exception _ nil))] |
188 | 188 | (defn parse-local-time |
189 | 189 | "Returns a LocalTime instance obtained by parsing the |
190 | 190 | given string according to the given formatter." |
191 | ([^DateTimeFormatter fmt ^ String s] | |
191 | (^org.joda.time.LocalTime [^DateTimeFormatter fmt ^ String s] | |
192 | 192 | (.parseLocalTime fmt s)) |
193 | 193 | ([^String s] |
194 | 194 | (first |
199 | 199 | (defn unparse |
200 | 200 | "Returns a string representing the given DateTime instance in UTC and in the |
201 | 201 | form determined by the given formatter." |
202 | [^DateTimeFormatter fmt ^DateTime dt] | |
202 | ^String [^DateTimeFormatter fmt ^DateTime dt] | |
203 | 203 | (.print fmt dt)) |
204 | 204 | |
205 | 205 | (defn unparse-local |
206 | 206 | "Returns a string representing the given LocalDateTime instance in the |
207 | 207 | form determined by the given formatter." |
208 | [^DateTimeFormatter fmt ^LocalDateTime dt] | |
208 | ^String [^DateTimeFormatter fmt ^LocalDateTime dt] | |
209 | 209 | (.print fmt dt)) |
210 | 210 | |
211 | 211 | (defn unparse-local-date |
212 | 212 | "Returns a string representing the given LocalDate instance in the form |
213 | 213 | determined by the given formatter." |
214 | [^DateTimeFormatter fmt ^LocalDate ld] | |
214 | ^String [^DateTimeFormatter fmt ^LocalDate ld] | |
215 | 215 | (.print fmt ld)) |
216 | 216 | |
217 | 217 | (defn unparse-local-time |
218 | 218 | "Returns a string representing the given LocalTime instance in the form |
219 | 219 | determined by the given formatter." |
220 | [^DateTimeFormatter fmt ^LocalTime lt] | |
220 | ^String [^DateTimeFormatter fmt ^LocalTime lt] | |
221 | 221 | (.print fmt lt)) |
222 | 222 | |
223 | 223 |
0 | {clj-time/date-time clj-time.coerce/from-string} |
0 | 0 | (ns clj-time.coerce-test |
1 | 1 | (:refer-clojure :exclude [extend second]) |
2 | (:require [clojure.test :refer :all] | |
2 | (:require [clojure.edn :as edn] | |
3 | [clojure.test :refer :all] | |
3 | 4 | [clj-time [core :refer :all] [coerce :refer :all]]) |
4 | 5 | (:import java.util.Date java.sql.Timestamp |
5 | 6 | [org.joda.time LocalDate LocalDateTime])) |
143 | 144 | (is (= "1998-04-25T00:00:00.000Z" (to-string (Timestamp. 893462400000)))) |
144 | 145 | (is (= "1998-04-25T00:00:00.000Z" (to-string "1998-04-25T00:00:00.000Z")))) |
145 | 146 | |
147 | (deftest test-to-edn | |
148 | (is (nil? (to-edn nil))) | |
149 | (is (nil? (to-edn ""))) | |
150 | (is (nil? (to-edn "x"))) | |
151 | (is (= (date-time 1970 1 1 0 0 0 0) | |
152 | (edn/read-string {:readers data-readers} | |
153 | (to-edn 0)))) | |
154 | (are [o] (= (date-time 1998 4 25 0 0 0 0) | |
155 | (edn/read-string {:readers data-readers} | |
156 | (to-edn o))) | |
157 | (date-time 1998 4 25) | |
158 | (date-midnight 1998 4 25) | |
159 | (Date. 893462400000) | |
160 | (java.sql.Date. 893462400000) | |
161 | 893462400000 | |
162 | (Timestamp. 893462400000) | |
163 | "1998-04-25T00:00:00.000Z")) | |
164 | ||
146 | 165 | (deftest test-to-timestamp |
147 | 166 | (is (nil? (to-timestamp nil))) |
148 | 167 | (is (nil? (to-timestamp ""))) |
3 | 3 | [clj-time.core :refer :all]) |
4 | 4 | (:import java.util.Date |
5 | 5 | org.joda.time.DateTime)) |
6 | ||
7 | ;; visual sanity check on version being used: | |
8 | (println "\nTesting clj-time with Clojure" (clojure-version) "\n") | |
6 | 9 | |
7 | 10 | (deftest test-now |
8 | 11 | (is (= (date-time 2010 1 1) |
380 | 383 | (is (= 30240 (-> 3 weeks in-minutes))) |
381 | 384 | (is (thrown? UnsupportedOperationException (-> 2 months in-minutes))) |
382 | 385 | (is (thrown? UnsupportedOperationException (-> 2 years in-minutes)))) |
383 | ||
386 | ||
384 | 387 | (deftest test-period-in-hours |
385 | 388 | (is (= 0 (-> 30 seconds in-hours))) |
386 | 389 | (is (= 0 (-> 4 minutes in-hours))) |
425 | 428 | (is (thrown? UnsupportedOperationException (-> 2 weeks in-years))) |
426 | 429 | (is (= 1 (-> 14 months in-years))) |
427 | 430 | (is (= 3 (-> 3 years in-years)))) |
431 | ||
432 | (deftest test-adjust | |
433 | (let [s (now) e (plus s (hours 3)) | |
434 | i (interval s e)] | |
435 | (is (abuts? i (adjust i (hours 3)))) | |
436 | (is (abuts? i (adjust i (hours -3)))) | |
437 | (is (not (within? i (plus s (hours 4))))) | |
438 | (is (within? (adjust i (hours 3)) (plus s (hours 4)))) | |
439 | (is (not (within? i (minus s (hours 2))))) | |
440 | (is (within? (adjust i (hours -3)) (minus s (hours 2)))) | |
441 | (is (overlaps? i (adjust i (hours 1)))) | |
442 | (is (overlaps? i (adjust i (hours -1)))))) | |
428 | 443 | |
429 | 444 | (deftest test-within? |
430 | 445 | (let [d1 (date-time 1985) |
490 | 505 | (is (overlaps? ld1 ld5 ld1 ld5)) |
491 | 506 | (is (overlaps? ld1 ld5 ld2 ld4)) |
492 | 507 | (is (overlaps? ld2 ld4 ld1 ld5)) |
493 | (is (overlaps? ld1 ld2 ld2 ld3)) | |
494 | (is (overlaps? ld2 ld3 ld1 ld2)) | |
508 | ;; these ranges abut and do not overlap -- fixed in #264 | |
509 | (is (not (overlaps? ld1 ld2 ld2 ld3))) | |
510 | ;; these ranges abut and do not overlap -- fixed in #264 | |
511 | (is (not (overlaps? ld2 ld3 ld1 ld2))) | |
495 | 512 | (is (not (overlaps? ld1 ld2 ld3 ld4))) |
496 | 513 | (is (not (overlaps? ld1 ld3 ld4 ld5))))) |
497 | 514 | |
591 | 608 | (is (= 1 (week-number-of-year (date-time 2012 12 31)))) |
592 | 609 | (is (= 1 (week-number-of-year (date-time 2013 1 1))))) |
593 | 610 | |
611 | (deftest test-week-year | |
612 | (is (= 2015 (week-year (date-time 2016 1 3)))) | |
613 | (is (= 2016 (week-year (date-time 2016 1 4)))) | |
614 | (is (= 2016 (week-year (date-time 2017 1 1)))) | |
615 | (is (= 2017 (week-year (date-time 2017 1 2))))) | |
616 | ||
594 | 617 | (deftest test-number-of-days-in-the-month |
595 | 618 | (is (= 31 (number-of-days-in-the-month 2012 1))) |
596 | 619 | (is (= 31 (number-of-days-in-the-month (date-time 2012 1 3)))) |
134 | 134 | (is (= "2012-02-01 22:15" |
135 | 135 | (unparse fmt (date-time 2012 2 1 22 15)))))) |
136 | 136 | |
137 | (deftest test-parser-with-builtin-time-formatters | |
138 | (let [fmt (formatters :time-no-ms)] | |
139 | (is (= "10:00:20Z" (unparse fmt (parse fmt "10:00:20Z"))))) | |
140 | ||
141 | (let [fmt (formatters :week-date)] | |
142 | (is (= "2018-W44-2" (unparse fmt (parse fmt "2018-W44-2"))))) | |
143 | ||
144 | (let [fmt (formatters :time)] | |
145 | (is (= "10:00:20.120Z" (unparse fmt (parse fmt "10:00:20.120Z"))))) | |
146 | ) | |
147 | ||
148 | (deftest test-parser-with-builtin-time-formatters-with-zone | |
149 | (let [fmt (with-zone (formatters :time-no-ms) (time-zone-for-offset 2))] | |
150 | (is (= "10:00:20+02:00" (unparse fmt (parse fmt "10:00:20+02:00"))))) | |
151 | ||
152 | (let [fmt (formatters :week-date-time-no-ms)] | |
153 | (is (= "2018-W44-2T05:09:54Z" (unparse fmt (parse fmt "2018-W44-2T05:09:54Z"))))) | |
154 | ||
155 | (let [fmt (with-zone (formatters :time) (time-zone-for-offset 2))] | |
156 | (is (= "10:00:20.120+02:00" (unparse fmt (parse fmt "10:00:20.120+02:00")))))) | |
157 | ||
137 | 158 | (deftest test-mysql-format |
138 | 159 | (are [expectation mysql] (= (parse mysql) expectation) |
139 | 160 | (date-time 2013 1 1 0 0 0) "2013-01-01 00:00:00" |
0 | (ns clj-time.spec-test | |
1 | (:require [clojure.test :refer :all] | |
2 | [clojure.spec :as spec] | |
3 | [clojure.spec.gen :as gen] | |
4 | [clj-time.core :refer :all] | |
5 | [clj-time.types :as types] | |
6 | [clj-time.spec :as ts])) | |
7 | ||
8 | (deftest test-spec-defs | |
9 | (is (spec/valid? ::ts/date-time (date-time 2018 8 22 7 12 58))) | |
10 | (is (not (spec/valid? ::ts/local-date-time (date-time 2018 8 22 7 12 58)))) | |
11 | (is (not (spec/valid? ::ts/local-date (date-time 2018 8 22 7 12 58)))) | |
12 | ||
13 | (is (not (spec/valid? ::ts/date-time (local-date-time 2018 8 22 7 12 58)))) | |
14 | (is (spec/valid? ::ts/local-date-time (local-date-time 2018 8 22 7 12 58))) | |
15 | (is (not (spec/valid? ::ts/local-date (local-date-time 2018 8 22 7 12 58)))) | |
16 | ||
17 | (is (not (spec/valid? ::ts/date-time (local-date 2018 8 22)))) | |
18 | (is (not (spec/valid? ::ts/local-date-time (local-date 2018 8 22)))) | |
19 | (is (spec/valid? ::ts/local-date (local-date 2018 8 22)))) | |
20 | ||
21 | (deftest test-generators | |
22 | (is (every? types/date-time? (gen/sample (spec/gen ::ts/date-time)))) | |
23 | (is (every? types/local-date-time? (gen/sample (spec/gen ::ts/local-date-time)))) | |
24 | (is (every? types/local-date? (gen/sample (spec/gen ::ts/local-date)))) | |
25 | ||
26 | (is (every? #(and (before? % (date-time 2031 1 1)) | |
27 | (before? (date-time 2010 12 31) %)) | |
28 | (gen/sample (spec/gen ::ts/date-time))))) | |
29 | ||
30 | ||
31 | (deftest test-period-generators | |
32 | ; These generators are meant to be used with the ts/*period* dynamic var. | |
33 | ; See test-generator-with-custom-period | |
34 | (is (every? int? (gen/sample (spec/gen ::ts/past)))) | |
35 | (is (every? int? (gen/sample (spec/gen ::ts/past-and-future)))) | |
36 | (is (every? int? (gen/sample (spec/gen ::ts/future))))) | |
37 | ||
38 | (deftest test-generator-with-custom-period | |
39 | (binding [ts/*period* #(spec/gen ::ts/past)] | |
40 | (is (every? #(and (before? % (date-time 2011 1 1)) | |
41 | (before? (date-time 2000 12 31) %)) | |
42 | (gen/sample (spec/gen ::ts/date-time)))))) |
0 | (ns clj-time.inst-test | |
1 | (:refer-clojure :exclude [extend second]) | |
2 | (:require [clojure.test :refer :all] | |
3 | [clj-time.core :refer :all]) | |
4 | (:import org.joda.time.DateTime)) | |
5 | ||
6 | (deftest test-inst | |
7 | (let [^DateTime n (now)] | |
8 | (is (inst? n)) | |
9 | (is (= (inst-ms n) (.getMillis n))))) |
0 | (ns clj-time.spec-test | |
1 | (:require [clojure.test :refer [deftest is]] | |
2 | [clojure.spec.alpha :as spec] | |
3 | [clojure.spec.gen.alpha :as gen] | |
4 | [clj-time.core :refer [before? | |
5 | date-time | |
6 | local-date local-date-time]] | |
7 | [clj-time.types :as types] | |
8 | [clj-time.spec :as ts])) | |
9 | ||
10 | (deftest test-spec-defs | |
11 | (is (spec/valid? ::ts/date-time (date-time 2018 8 22 7 12 58))) | |
12 | (is (not (spec/valid? ::ts/local-date-time (date-time 2018 8 22 7 12 58)))) | |
13 | (is (not (spec/valid? ::ts/local-date (date-time 2018 8 22 7 12 58)))) | |
14 | ||
15 | (is (not (spec/valid? ::ts/date-time (local-date-time 2018 8 22 7 12 58)))) | |
16 | (is (spec/valid? ::ts/local-date-time (local-date-time 2018 8 22 7 12 58))) | |
17 | (is (not (spec/valid? ::ts/local-date (local-date-time 2018 8 22 7 12 58)))) | |
18 | ||
19 | (is (not (spec/valid? ::ts/date-time (local-date 2018 8 22)))) | |
20 | (is (not (spec/valid? ::ts/local-date-time (local-date 2018 8 22)))) | |
21 | (is (spec/valid? ::ts/local-date (local-date 2018 8 22)))) | |
22 | ||
23 | (deftest test-generators | |
24 | (is (every? types/date-time? (gen/sample (spec/gen ::ts/date-time)))) | |
25 | (is (every? types/local-date-time? (gen/sample (spec/gen ::ts/local-date-time)))) | |
26 | (is (every? types/local-date? (gen/sample (spec/gen ::ts/local-date)))) | |
27 | ||
28 | (is (every? #(and (before? % (date-time 2031 1 1)) | |
29 | (before? (date-time 2010 12 31) %)) | |
30 | (gen/sample (spec/gen ::ts/date-time))))) | |
31 | ||
32 | ||
33 | (deftest test-period-generators | |
34 | ; These generators are meant to be used with the ts/*period* dynamic var. | |
35 | ; See test-generator-with-custom-period | |
36 | (is (every? int? (gen/sample (spec/gen ::ts/past)))) | |
37 | (is (every? int? (gen/sample (spec/gen ::ts/past-and-future)))) | |
38 | (is (every? int? (gen/sample (spec/gen ::ts/future))))) | |
39 | ||
40 | (deftest test-generator-with-custom-period | |
41 | (binding [ts/*period* #(spec/gen ::ts/past)] | |
42 | (is (every? #(and (before? % (date-time 2011 1 1)) | |
43 | (before? (date-time 2000 12 31) %)) | |
44 | (gen/sample (spec/gen ::ts/date-time)))))) |