Codebase list clj-time-clojure / 2a0b028
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
16 changed file(s) with 454 addition(s) and 200 deletion(s). Raw diff Collapse all Expand all
55 pom.xml
66 *.jar
77 *~
8 .cpcache
89 .lein-*
910 .nrepl-*
1011 checkouts
77 script: lein test-all
88 jdk:
99 - openjdk7
10 - oraclejdk7
10 - openjdk8
1111 - oraclejdk8
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
045 ## Changes Between 0.13.0 and 0.14.0
146
247 * 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
26
37 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.**
416
517 ## Artifacts
618
1931
2032 With Leiningen:
2133
22 ``` clj
23 [clj-time "0.14.0"]
34 ```
35 [clj-time "0.15.2"]
2436 ```
2537
2638 With Maven:
2941 <dependency>
3042 <groupId>clj-time</groupId>
3143 <artifactId>clj-time</artifactId>
32 <version>0.14.0</version>
44 <version>0.15.2</version>
3345 </dependency>
3446 ```
3547
3648 ## Bugs and Enhancements
3749
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!).
3951
4052 ## Mailing List
4153
137149 => #<DateTime 1986-12-05T00:00:00.000Z>
138150 ```
139151
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
140159 An `Interval` is used to represent the span of time between two
141160 `DateTime` instances. Construct one using `interval`, then query them
142161 using `within?`, `overlaps?`, and `abuts?`
231250 => "2012-02-01"
232251 ```
233252
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
234255 ### clj-time.coerce
235256
236257 The namespace `clj-time.coerce` contains utility functions for
237258 coercing Joda `DateTime` instances to and from various other types:
238259
239260
240 ``` clj
261 ```clojure
241262 (require '[clj-time.coerce :as c])
242263 ```
243264
265286 `java.sql.Timestamp` (`to-sql-time` and `from-sql-time`) and several
266287 other types.
267288
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
268321 ### clj-time.local
269322
270323 The namespace `clj-time.local` contains functions for working with
335388 (require '[clj-time.core :as t])
336389 (require '[clj-time.predicates :as pr])
337390 ```
338 ``` clojure
391 ```clojure
339392 (pr/monday? (t/date-time 1999 9 9))
340393 => false
341394
384437
385438 Running the tests:
386439
387 $ rm -f test/readme.clj && lein test-all && lein test-readme
440 $ rm -f test/readme.clj && lein test-all
388441
389442 (assumes Leiningen 2.x)
390443
0 {:deps {joda-time {:mvn/version "2.10"}}}
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"
13 :description "A date and time library for Clojure, wrapping Joda Time."
24 :url "https://github.com/clj-time/clj-time"
35 :mailing-list {:name "clj-time mailing list"
68 :license {:name "MIT License"
79 :url "http://www.opensource.org/licenses/mit-license.php"
810 :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"]]
1113 :min-lein-version "2.0.0"
1214 :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"]]
1618 :plugins [[lein-midje "3.2.1"]
1719 [midje-readme "1.0.9"]]
1820 :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"]]}
2021 :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"]}
2129 :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"]}}
2633
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"]})
99 (:refer-clojure :exclude [extend second])
1010 (:require [clj-time.core :refer :all]
1111 [clj-time.format :as time-fmt])
12 (:import [java.sql Timestamp]
12 (:import [java.io Writer]
13 [java.sql Timestamp]
1314 [java.util Date]
1415 [org.joda.time DateTime DateTimeZone DateMidnight YearMonth
1516 LocalDate LocalDateTime]))
1617
1718 (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."))
2020
2121 (defn from-long
2222 "Returns a DateTime instance in the UTC time zone corresponding to the given
2323 number of milliseconds after the Unix epoch."
24 ^org.joda.time.DateTime
2425 [^Long millis]
2526 (DateTime. millis ^DateTimeZone utc))
26
2727
2828 (defn from-epoch
2929 "Returns a DateTime instance in the UTC time zone
3030 from given Unix epoch."
31 ^org.joda.time.DateTime
3132 [^Long epoch]
3233 (from-long (* epoch 1000)))
33
3434
3535 (defn from-string
3636 "return DateTime instance from string using
3737 formatters in clj-time.format, returning first
3838 which parses"
39 ^org.joda.time.DateTime
3940 [^String s]
4041 (time-fmt/parse s))
4142
43 (def data-readers
44 "tagged literal support if loader does not find \"data_readers.clj\""
45 {'clj-time/date-time from-string})
46
4247 (defn from-date
4348 "Returns a DateTime instance in the UTC time zone corresponding to the given
4449 Java Date object."
45 [^Date date]
50 ^org.joda.time.DateTime
51 [^java.util.Date date]
4652 (when date
4753 (from-long (.getTime date))))
4854
4955 (defn from-sql-date
5056 "Returns a DateTime instance in the UTC time zone corresponding to the given
5157 java.sql.Date object."
58 ^org.joda.time.DateTime
5259 [^java.sql.Date sql-date]
5360 (when sql-date
5461 (from-long (.getTime sql-date))))
5663 (defn from-sql-time
5764 "Returns a DateTime instance in the UTC time zone corresponding to the given
5865 java.sql.Timestamp object."
66 ^org.joda.time.DateTime
5967 [^java.sql.Timestamp sql-time]
6068 (when sql-time
6169 (from-long (.getTime sql-time))))
6270
6371 (defn to-long
6472 "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)]
6776 (.getMillis dt)))
6877
6978 (defn to-epoch
7079 "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)))
7484
7585 (defn to-date
7686 "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)]
7990 (Date. (.getMillis dt))))
8091
8192 (defn to-sql-date
8293 "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)]
8597 (java.sql.Date. (.getMillis dt))))
8698
8799 (defn to-sql-time
88100 "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)]
91104 (java.sql.Timestamp. (.getMillis dt))))
92105
93106 (defn to-string
94107 "Returns a string representation of obj in UTC time-zone
95108 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)]
98112 (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)))
99125
100126 (defn to-timestamp
101127 "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))))
105132
106133 (defn to-local-date
107134 "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)]
110138 (LocalDate. (.getMillis (from-time-zone dt (default-time-zone))))))
111139
112140 (defn to-local-date-time
113141 "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)]
116145 (LocalDateTime. (.getMillis (from-time-zone dt (default-time-zone))))))
117146
118147 (defn in-time-zone
119148 "Convert `obj` into `tz`, return org.joda.time.LocalDate instance."
149 ^org.joda.time.LocalDate
120150 [obj tz]
121 (if-let [dt (to-date-time obj)]
151 (when-let [dt (to-date-time obj)]
122152 (-> dt
123153 (to-time-zone tz)
124154 .toLocalDate)))
174204 (to-date-time [string]
175205 (from-string string))
176206
177 Timestamp
207 java.sql.Timestamp
178208 (to-date-time [timestamp]
179209 (from-date timestamp)))
118118 "Returns a new date/time corresponding to the given date/time moved backwards by the given Period(s).")
119119 (first-day-of-the-month- [this] "Returns the first day of the month")
120120 (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."))
122123
123124 (defprotocol InTimeUnitProtocol
124125 "Interface for in-<time unit> functions"
156157 (.. ^DateTime this dayOfMonth withMaximumValue))
157158 (week-number-of-year [this]
158159 (.getWeekOfWeekyear this))
160 (week-year [this] (.getWeekyear this))
159161
160162 org.joda.time.DateMidnight
161163 (year [this] (.getYear this))
181183 (.. ^DateMidnight this dayOfMonth withMaximumValue))
182184 (week-number-of-year [this]
183185 (.getWeekOfWeekyear this))
186 (week-year [this] (.getWeekyear this))
184187
185188 org.joda.time.LocalDateTime
186189 (year [this] (.getYear this))
206209 (.. ^LocalDateTime this dayOfMonth withMaximumValue))
207210 (week-number-of-year [this]
208211 (.getWeekOfWeekyear this))
212 (week-year [this] (.getWeekyear this))
209213
210214 org.joda.time.YearMonth
211215 (year [this] (.getYear this))
232236 (.. ^LocalDate this dayOfMonth withMaximumValue))
233237 (week-number-of-year [this]
234238 (.getWeekOfWeekyear this))
239 (week-year [this] (.getWeekyear this))
235240
236241 org.joda.time.LocalTime
237242 (hour [this] (.getHourOfDay this))
251256
252257 (defn now
253258 "Returns a DateTime for the current instant in the UTC time zone."
259 ^org.joda.time.DateTime
254260 []
255261 (DateTime. ^DateTimeZone utc))
256262
257263 (defn time-now
258264 "Returns a LocalTime for the current instant without date or time zone
259265 using ISOChronology in the current time zone."
266 ^org.joda.time.LocalTime
260267 []
261268 (LocalTime. ))
262269
268275 ([^DateTimeZone tz]
269276 (DateMidnight. tz)))
270277
271 (defn ^DateTime with-time-at-start-of-day
278 (defn with-time-at-start-of-day
272279 "Returns a DateTime representing the start of the day. Normally midnight,
273280 but not always true, as in some time zones with daylight savings."
281 ^org.joda.time.DateTime
274282 [^DateTime dt]
275283 (.withTimeAtStartOfDay dt))
276284
277285 (defn epoch
278286 "Returns a DateTime for the beginning of the Unix epoch in the UTC time zone."
287 ^org.joda.time.DateTime
279288 []
280289 (DateTime. (long 0) ^DateTimeZone utc))
281290
284293 Specify the year, month of year, day of month. Note that month and day are
285294 1-indexed. Any number of least-significant components can be ommited, in which case
286295 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)))
293302
294303 (defn min-date
295304 "Minimum of the provided DateTimes."
301310 [dt & dts]
302311 (reduce #(if (after? %1 %2) %1 %2) dt dts))
303312
304 (defn ^DateTime date-time
313 (defn date-time
305314 "Constructs and returns a new DateTime in UTC.
306315 Specify the year, month of year, day of month, hour of day, minute of hour,
307316 second of minute, and millisecond of second. Note that month and day are
308317 1-indexed while hour, second, minute, and millis are 0-indexed.
309318 Any number of least-significant components can be ommited, in which case
310319 they will default to 1 or 0 as appropriate."
311 ([year]
320 (^org.joda.time.DateTime [year]
312321 (date-time year 1 1 0 0 0 0))
313 ([year month]
322 (^org.joda.time.DateTime [year month]
314323 (date-time year month 1 0 0 0 0))
315 ([year month day]
324 (^org.joda.time.DateTime [year month day]
316325 (date-time year month day 0 0 0 0))
317 ([year month day hour]
326 (^org.joda.time.DateTime [year month day hour]
318327 (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]
320329 (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]
322331 (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]
325334 (DateTime. year month day hour minute second millis ^DateTimeZone utc)))
326335
327 (defn ^org.joda.time.LocalDateTime local-date-time
336 (defn local-date-time
328337 "Constructs and returns a new LocalDateTime.
329338 Specify the year, month of year, day of month, hour of day, minute of hour,
330339 second of minute, and millisecond of second. Note that month and day are
331340 1-indexed while hour, second, minute, and millis are 0-indexed.
332341 Any number of least-significant components can be ommited, in which case
333342 they will default to 1 or 0 as appropriate."
334 ([year]
343 (^org.joda.time.LocalDateTime [year]
335344 (local-date-time year 1 1 0 0 0 0))
336 ([year month]
345 (^org.joda.time.LocalDateTime [year month]
337346 (local-date-time year month 1 0 0 0 0))
338 ([year month day]
347 (^org.joda.time.LocalDateTime [year month day]
339348 (local-date-time year month day 0 0 0 0))
340 ([year month day hour]
349 (^org.joda.time.LocalDateTime [year month day hour]
341350 (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]
343352 (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]
345354 (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]
348357 (LocalDateTime. year month day hour minute second millis)))
349358
350 (defn ^org.joda.time.YearMonth year-month
359 (defn year-month
351360 "Constructs and returns a new YearMonth.
352361 Specify the year and month of year. Month is 1-indexed and defaults
353362 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
360369 "Constructs and returns a new LocalDate.
361370 Specify the year, month, and day. Does not deal with timezones."
371 ^org.joda.time.LocalDate
362372 [^Integer year ^Integer month ^Integer day]
363373 (LocalDate. year month day))
364374
365 (defn ^org.joda.time.LocalTime local-time
375 (defn local-time
366376 "Constructs and returns a new LocalTime.
367377 Specify the hour of day, minute of hour, second of minute, and millisecond of second.
368378 Any number of least-significant components can be ommited, in which case
369379 they will default to 1 or 0 as appropriate."
370 ([hour]
380 (^org.joda.time.LocalTime [hour]
371381 (local-time hour 0 0 0))
372 ([hour minute]
382 (^org.joda.time.LocalTime [hour minute]
373383 (local-time hour minute 0 0))
374 ([hour minute second]
384 (^org.joda.time.LocalTime [hour minute second]
375385 (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]
377387 (LocalTime. hour minute second millis))
378388 )
379389
380 (defn ^org.joda.time.LocalDate today
390 (defn today
381391 "Constructs and returns a new LocalDate representing today's date.
382392 LocalDate objects do not deal with timezones at all."
393 ^org.joda.time.LocalDate
383394 []
384395 (LocalDate.))
385396
386397 (defn time-zone-for-offset
387398 "Returns a DateTimeZone for the given offset, specified either in hours or
388399 hours and minutes."
389 ([hours]
400 (^org.joda.time.DateTimeZone [hours]
390401 (DateTimeZone/forOffsetHours hours))
391 ([hours minutes]
402 (^org.joda.time.DateTimeZone [hours minutes]
392403 (DateTimeZone/forOffsetHoursMinutes hours minutes)))
393404
394405 (defn time-zone-for-id
395406 "Returns a DateTimeZone for the given ID, which must be in long form, e.g.
396407 'America/Matamoros'."
408 ^org.joda.time.DateTimeZone
397409 [^String id]
398410 (DateTimeZone/forID id))
399411
404416
405417 (defn default-time-zone
406418 "Returns the default DateTimeZone for the current environment."
419 ^org.joda.time.DateTimeZone
407420 []
408421 (DateTimeZone/getDefault))
409422
410 (defn ^org.joda.time.DateTime
411 to-time-zone
423 (defn to-time-zone
412424 "Returns a new ReadableDateTime corresponding to the same absolute instant in time as
413425 the given ReadableDateTime, but with calendar fields corresponding to the given
414426 TimeZone."
427 ^org.joda.time.DateTime
415428 [^DateTime dt ^DateTimeZone tz]
416429 (.withZone dt tz))
417430
418 (defn ^org.joda.time.DateTime
419 from-time-zone
431 (defn from-time-zone
420432 "Returns a new ReadableDateTime corresponding to the same point in calendar time as
421433 the given ReadableDateTime, but for a correspondingly different absolute instant in
422434 time."
435 ^org.joda.time.DateTime
423436 [^DateTime dt ^DateTimeZone tz]
424437 (.withZoneRetainFields dt tz))
425438
426439 (defn years
427440 "Given a number, returns a Period representing that many years.
428441 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)))
433446
434447 (defn months
435448 "Given a number, returns a Period representing that many months.
436449 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)))
441454
442455 (defn weeks
443456 "Given a number, returns a Period representing that many weeks.
444457 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)))
449462
450463 (defn days
451464 "Given a number, returns a Period representing that many days.
452465 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)))
457470
458471 (defn hours
459472 "Given a number, returns a Period representing that many hours.
460473 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)))
465478
466479 (defn minutes
467480 "Given a number, returns a Period representing that many minutes.
468481 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)))
473486
474487 (defn seconds
475488 "Given a number, returns a Period representing that many seconds.
476489 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)))
481494
482495 (extend-protocol InTimeUnitProtocol
483496 org.joda.time.Interval
538551 (defn millis
539552 "Given a number, returns a Period representing that many milliseconds.
540553 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)))
545558
546559 (defn plus
547560 "Returns a new date/time corresponding to the given date/time moved forwards by
595608 (defn interval
596609 "Returns an interval representing the span between the two given ReadableDateTimes.
597610 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]
599612 (Interval. dt-a dt-b))
600613
601614 (defn start
614627 [^Interval in & by]
615628 (.withEnd in (apply plus (end in) by)))
616629
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)))
617640
618641 (defn within?
619642 "With 2 arguments: Returns true if the given Interval contains the given
639662 ([^ReadablePartial start-a ^ReadablePartial end-a
640663 ^ReadablePartial start-b ^ReadablePartial end-b]
641664 (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)))))
644666
645667 (defn overlap
646668 "Returns an Interval representing the overlap of the specified Intervals.
735757 (plus (first-day-of-the-month dt)
736758 (days (- n 1)))))
737759
738 (defn ^DateTime today-at
760 (defn ^org.joda.time.DateTime today-at
739761 ([^long hours ^long minutes ^long seconds ^long millis]
740762 (let [^MutableDateTime mdt (.toMutableDateTime ^DateTime (now))]
741763 (.toDateTime (doto mdt
761783 `(do-at* ~base-date-time
762784 (fn [] ~@body)))
763785
764 (defn ^DateTime floor
786 (defn floor
765787 "Floors the given date-time dt to the given time unit dt-fn,
766788 e.g. (floor (now) hour) returns (now) for all units
767789 up to and including the hour"
768 ([^DateTime dt dt-fn]
790 (^org.joda.time.DateTime [^DateTime dt dt-fn]
769791 (let [dt-fns [year month day hour minute second milli]
770792 tz (.getZone dt)]
771793 (.withZoneRetainFields
775797 (concat (take-while (partial not= dt-fn) dt-fns) [dt-fn])
776798 (repeat [dt])))
777799 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))))
3939
4040 (defn formatter
4141 "Returns a custom formatter for the given date-time pattern or keyword."
42 ([fmts]
42 (^org.joda.time.format.DateTimeFormatter [fmts]
4343 (formatter fmts utc))
44 ([fmts ^DateTimeZone dtz]
44 (^org.joda.time.format.DateTimeFormatter [fmts ^DateTimeZone dtz]
4545 (cond (keyword? fmts) (.withZone ^DateTimeFormatter (get formatters fmts) dtz)
4646 (string? fmts) (.withZone (DateTimeFormat/forPattern fmts) dtz)
4747 :else (.withZone ^DateTimeFormatter fmts dtz)))
48 ([^DateTimeZone dtz fmts & more]
48 (^org.joda.time.format.DateTimeFormatter [^DateTimeZone dtz fmts & more]
4949 (let [printer (.getPrinter ^DateTimeFormatter (formatter fmts dtz))
5050 parsers (map #(.getParser ^DateTimeFormatter (formatter % dtz)) (cons fmts more))]
5151 (-> (DateTimeFormatterBuilder.)
5757
5858 (defn formatter-local
5959 "Returns a custom formatter with no time zone info."
60 ([^String fmt]
60 (^org.joda.time.format.DateTimeFormat [^String fmt]
6161 (DateTimeFormat/forPattern fmt)))
6262
6363 (defn with-chronology
6464 "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]
6666 (.withChronology f c))
6767
6868 (defn with-locale
6969 "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]
7171 (.withLocale f l))
7272
7373 (defn with-pivot-year
7474 "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]
7676 (.withPivotYear f pivot-year))
7777
7878 (defn with-zone
7979 "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]
8181 (.withZone f dtz))
8282
8383 (defn with-default-year
8484 "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]
8686 (.withDefaultYear f default-year))
8787
8888 (def ^{:doc "Map of ISO 8601 and a single RFC 822 formatters that can be used for parsing and, in most
155155 (defn parse
156156 "Returns a DateTime instance in the UTC time zone obtained by parsing the
157157 given string according to the given formatter."
158 ([^DateTimeFormatter fmt ^String s]
158 (^org.joda.time.DateTime [^DateTimeFormatter fmt ^String s]
159159 (.parseDateTime fmt s))
160 ([^String s]
160 (^org.joda.time.DateTime [^String s]
161161 (first
162162 (for [f (vals formatters)
163163 :let [d (try (parse f s) (catch Exception _ nil))]
166166 (defn parse-local
167167 "Returns a LocalDateTime instance obtained by parsing the
168168 given string according to the given formatter."
169 ([^DateTimeFormatter fmt ^String s]
169 (^org.joda.time.LocalDateTime [^DateTimeFormatter fmt ^String s]
170170 (.parseLocalDateTime fmt s))
171 ([^String s]
171 (^org.joda.time.LocalDateTime [^String s]
172172 (first
173173 (for [f (vals formatters)
174174 :let [d (try (parse-local f s) (catch Exception _ nil))]
177177 (defn parse-local-date
178178 "Returns a LocalDate instance obtained by parsing the
179179 given string according to the given formatter."
180 ([^DateTimeFormatter fmt ^String s]
180 (^org.joda.time.LocalDate [^DateTimeFormatter fmt ^String s]
181181 (.parseLocalDate fmt s))
182 ([^String s]
182 (^org.joda.time.LocalDate [^String s]
183183 (first
184184 (for [f (vals formatters)
185185 :let [d (try (parse-local-date f s) (catch Exception _ nil))]
188188 (defn parse-local-time
189189 "Returns a LocalTime instance obtained by parsing the
190190 given string according to the given formatter."
191 ([^DateTimeFormatter fmt ^ String s]
191 (^org.joda.time.LocalTime [^DateTimeFormatter fmt ^ String s]
192192 (.parseLocalTime fmt s))
193193 ([^String s]
194194 (first
199199 (defn unparse
200200 "Returns a string representing the given DateTime instance in UTC and in the
201201 form determined by the given formatter."
202 [^DateTimeFormatter fmt ^DateTime dt]
202 ^String [^DateTimeFormatter fmt ^DateTime dt]
203203 (.print fmt dt))
204204
205205 (defn unparse-local
206206 "Returns a string representing the given LocalDateTime instance in the
207207 form determined by the given formatter."
208 [^DateTimeFormatter fmt ^LocalDateTime dt]
208 ^String [^DateTimeFormatter fmt ^LocalDateTime dt]
209209 (.print fmt dt))
210210
211211 (defn unparse-local-date
212212 "Returns a string representing the given LocalDate instance in the form
213213 determined by the given formatter."
214 [^DateTimeFormatter fmt ^LocalDate ld]
214 ^String [^DateTimeFormatter fmt ^LocalDate ld]
215215 (.print fmt ld))
216216
217217 (defn unparse-local-time
218218 "Returns a string representing the given LocalTime instance in the form
219219 determined by the given formatter."
220 [^DateTimeFormatter fmt ^LocalTime lt]
220 ^String [^DateTimeFormatter fmt ^LocalTime lt]
221221 (.print fmt lt))
222222
223223
0 {clj-time/date-time clj-time.coerce/from-string}
00 (ns clj-time.coerce-test
11 (:refer-clojure :exclude [extend second])
2 (:require [clojure.test :refer :all]
2 (:require [clojure.edn :as edn]
3 [clojure.test :refer :all]
34 [clj-time [core :refer :all] [coerce :refer :all]])
45 (:import java.util.Date java.sql.Timestamp
56 [org.joda.time LocalDate LocalDateTime]))
143144 (is (= "1998-04-25T00:00:00.000Z" (to-string (Timestamp. 893462400000))))
144145 (is (= "1998-04-25T00:00:00.000Z" (to-string "1998-04-25T00:00:00.000Z"))))
145146
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
146165 (deftest test-to-timestamp
147166 (is (nil? (to-timestamp nil)))
148167 (is (nil? (to-timestamp "")))
33 [clj-time.core :refer :all])
44 (:import java.util.Date
55 org.joda.time.DateTime))
6
7 ;; visual sanity check on version being used:
8 (println "\nTesting clj-time with Clojure" (clojure-version) "\n")
69
710 (deftest test-now
811 (is (= (date-time 2010 1 1)
380383 (is (= 30240 (-> 3 weeks in-minutes)))
381384 (is (thrown? UnsupportedOperationException (-> 2 months in-minutes)))
382385 (is (thrown? UnsupportedOperationException (-> 2 years in-minutes))))
383
386
384387 (deftest test-period-in-hours
385388 (is (= 0 (-> 30 seconds in-hours)))
386389 (is (= 0 (-> 4 minutes in-hours)))
425428 (is (thrown? UnsupportedOperationException (-> 2 weeks in-years)))
426429 (is (= 1 (-> 14 months in-years)))
427430 (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))))))
428443
429444 (deftest test-within?
430445 (let [d1 (date-time 1985)
490505 (is (overlaps? ld1 ld5 ld1 ld5))
491506 (is (overlaps? ld1 ld5 ld2 ld4))
492507 (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)))
495512 (is (not (overlaps? ld1 ld2 ld3 ld4)))
496513 (is (not (overlaps? ld1 ld3 ld4 ld5)))))
497514
591608 (is (= 1 (week-number-of-year (date-time 2012 12 31))))
592609 (is (= 1 (week-number-of-year (date-time 2013 1 1)))))
593610
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
594617 (deftest test-number-of-days-in-the-month
595618 (is (= 31 (number-of-days-in-the-month 2012 1)))
596619 (is (= 31 (number-of-days-in-the-month (date-time 2012 1 3))))
134134 (is (= "2012-02-01 22:15"
135135 (unparse fmt (date-time 2012 2 1 22 15))))))
136136
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
137158 (deftest test-mysql-format
138159 (are [expectation mysql] (= (parse mysql) expectation)
139160 (date-time 2013 1 1 0 0 0) "2013-01-01 00:00:00"
+0
-43
test_clj_1.9/clj_time/spec_test.clj less more
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))))))