Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64352fd

Browse files
authored
Merge pull request #71 from brettrowberry/master
Added not-before? and not-after?
2 parents 3a0fe6f + fbd53c5 commit 64352fd

File tree

5 files changed

+227
-42
lines changed

5 files changed

+227
-42
lines changed

‎.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ pom.xml.asc
77
*.class
88
.lein-*
99
/.nrepl-port
10+
11+
# deps.edn
12+
.cpcache/
13+
14+
# Mercurial
1015
.hgignore
1116
.hg/
17+
18+
# Calva
19+
.lsp/
20+
.calva/

‎README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ You can also take a look at a [comprehensive comparison](http://time4j.net/tutor
5959

6060
## Usage
6161

62-
Add the following dependency to your `project.clj` or `build.boot`:
62+
Add the following dependency to your `deps.edn`
63+
```clj
64+
clojure.java-time/clojure.java-time {:mvn/version "0.3.2"}
65+
```
66+
67+
or to your `project.clj` or `build.boot`:
6368

6469
```clj
6570
[clojure.java-time "0.3.2"]
@@ -302,6 +307,26 @@ last argument. Offsets can be specified as float values:
302307
=> #<java.time.ZoneOffset -01:30>
303308
```
304309

310+
Compare dates:
311+
312+
```clj
313+
(before? (year 2020) (year 2021))
314+
=> true
315+
316+
(after? (year 2021) (year 2021))
317+
=> false
318+
319+
(let [expiration-date (year 2010)
320+
purchase-date (year 2010)]
321+
(not-before? expiration-date purchase-date))
322+
=> true
323+
324+
(let [start-date (year 2011)
325+
cutoff-date (year 2010)]
326+
(not-after? start-date cutoff-date))
327+
=> false
328+
```
329+
305330
#### Conversions
306331

307332
Time entities can be converted to other time entities if the target contains

‎src/java_time.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
with-clock with-clock-fn]
1111

1212
[java-time.core
13-
zero? negative? negate abs max min before? after?
13+
zero? negative? negate abs max min
14+
before? not-after? after? not-before?
1415
supports? chronology fields units properties property
1516
as value range min-value max-value largest-min-value smallest-max-value
1617
with-value with-min-value with-max-value with-largest-min-value with-smallest-max-value

‎src/java_time/core.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@
170170
(before? y (first more)))
171171
false)))
172172

173+
(def not-after?
174+
"Similar to [[before?]], but also returns truthy if the inputs are equal."
175+
(complement after?))
176+
173177
(defn after?
174178
"Returns a truthy value if time entities are ordered from the latest to the
175179
earliest (same semantics as `>`):
@@ -189,6 +193,10 @@
189193
(after? y (first more)))
190194
false)))
191195

196+
(def not-before?
197+
"Similar to [[after?]], but also returns truthy if the inputs are equal."
198+
(complement before?))
199+
192200
(defn plus
193201
"Adds all of the `os` to the time entity `o`. `plus` is not commutative, the
194202
first argument is always the entity which will accumulate the rest of the

‎test/java_time_test.clj

Lines changed: 182 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -403,43 +403,165 @@
403403
(deftest ordering
404404

405405
(testing "times"
406-
(is (j/after? (j/local-date-time clock) (j/minus (j/local-date-time clock) (j/days 5))))
407-
(is (j/before? (j/local-date-time clock) (j/plus (j/local-date-time clock) (j/days 5))))
408-
409-
(is (j/after? (j/local-date clock) (j/minus (j/local-date clock) (j/days 5))))
410-
(is (j/before? (j/local-date clock) (j/plus (j/local-date clock) (j/days 5))))
411-
412-
(is (j/after? (j/local-time clock) (j/minus (j/local-time clock) (j/minutes 5))))
413-
(is (j/before? (j/local-time clock) (j/plus (j/local-time clock) (j/minutes 5))))
414-
415-
(is (j/after? (j/zoned-date-time clock) (j/minus (j/zoned-date-time clock) (j/minutes 5))))
416-
(is (j/before? (j/zoned-date-time clock) (j/plus (j/zoned-date-time clock) (j/minutes 5))))
417-
418-
(is (j/after? (j/offset-date-time clock) (j/minus (j/offset-date-time clock) (j/minutes 5))))
419-
(is (j/before? (j/offset-date-time clock) (j/plus (j/offset-date-time clock) (j/minutes 5))))
420-
421-
(is (j/after? (j/offset-time clock) (j/minus (j/offset-time clock) (j/minutes 5))))
422-
(is (j/before? (j/offset-time clock) (j/plus (j/offset-time clock) (j/minutes 5))))
423-
424-
(is (j/after? (j/instant clock) (j/minus (j/instant clock) (j/minutes 5))))
425-
(is (j/before? (j/instant clock) (j/plus (j/instant clock) (j/minutes 5)))))
406+
(let [ldt (j/local-date-time clock)
407+
ldt+5 (j/plus ldt (j/days 5))]
408+
(is (j/after? ldt+5 ldt))
409+
(is (not (j/after? ldt ldt+5)))
410+
(is (j/before? ldt ldt+5))
411+
(is (not (j/before? ldt+5 ldt)))
412+
(is (j/not-after? ldt ldt))
413+
(is (j/not-after? ldt ldt+5))
414+
(is (not (j/not-after? ldt+5 ldt)))
415+
(is (j/not-before? ldt ldt))
416+
(is (j/not-before? ldt+5 ldt))
417+
(is (not (j/not-before? ldt ldt+5))))
418+
419+
(let [ld (j/local-date clock)
420+
ld+5 (j/plus ld (j/days 5))]
421+
(is (j/after? ld+5 ld))
422+
(is (not (j/after? ld ld+5)))
423+
(is (j/before? ld ld+5))
424+
(is (not (j/before? ld+5 ld)))
425+
(is (j/not-after? ld ld))
426+
(is (j/not-after? ld ld+5))
427+
(is (not (j/not-after? ld+5 ld)))
428+
(is (j/not-before? ld ld))
429+
(is (j/not-before? ld+5 ld))
430+
(is (not (j/not-before? ld ld+5))))
431+
432+
(let [lt (j/local-time clock)
433+
lt+5 (j/plus lt (j/minutes 5))]
434+
(is (j/after? lt+5 lt))
435+
(is (not (j/after? lt lt+5)))
436+
(is (j/before? lt lt+5))
437+
(is (not (j/before? lt+5 lt)))
438+
(is (j/not-after? lt lt))
439+
(is (j/not-after? lt lt+5))
440+
(is (not (j/not-after? lt+5 lt)))
441+
(is (j/not-before? lt lt))
442+
(is (j/not-before? lt+5 lt))
443+
(is (not (j/not-before? lt lt+5))))
444+
445+
(let [zdt (j/zoned-date-time clock)
446+
zdt+5 (j/plus zdt (j/minutes 5))]
447+
(is (j/after? zdt+5 zdt))
448+
(is (not (j/after? zdt zdt+5)))
449+
(is (j/before? zdt zdt+5))
450+
(is (not (j/before? zdt+5 zdt)))
451+
(is (j/not-after? zdt zdt))
452+
(is (j/not-after? zdt zdt+5))
453+
(is (not (j/not-after? zdt+5 zdt)))
454+
(is (j/not-before? zdt zdt))
455+
(is (j/not-before? zdt+5 zdt))
456+
(is (not (j/not-before? zdt zdt+5))))
457+
458+
(let [odt (j/offset-date-time clock)
459+
odt+5 (j/plus odt (j/minutes 5))]
460+
(is (j/after? odt+5 odt))
461+
(is (not (j/after? odt odt+5)))
462+
(is (j/before? odt odt+5))
463+
(is (not (j/before? odt+5 odt)))
464+
(is (j/not-after? odt odt))
465+
(is (j/not-after? odt odt+5))
466+
(is (not (j/not-after? odt+5 odt)))
467+
(is (j/not-before? odt odt))
468+
(is (j/not-before? odt+5 odt))
469+
(is (not (j/not-before? odt odt+5))))
470+
471+
(let [ot (j/offset-time clock)
472+
ot+5 (j/plus ot (j/minutes 5))]
473+
(is (j/after? ot+5 ot))
474+
(is (not (j/after? ot ot+5)))
475+
(is (j/before? ot ot+5))
476+
(is (not (j/before? ot+5 ot)))
477+
(is (j/not-after? ot ot))
478+
(is (j/not-after? ot ot+5))
479+
(is (not (j/not-after? ot+5 ot)))
480+
(is (j/not-before? ot ot))
481+
(is (j/not-before? ot+5 ot))
482+
(is (not (j/not-before? ot ot+5))))
483+
484+
(let [i (j/instant clock)
485+
i+5 (j/plus i (j/minutes 5))]
486+
(is (j/after? i+5 i))
487+
(is (not (j/after? i i+5)))
488+
(is (j/before? i i+5))
489+
(is (not (j/before? i+5 i)))
490+
(is (j/not-after? i i))
491+
(is (j/not-after? i i+5))
492+
(is (not (j/not-after? i+5 i)))
493+
(is (j/not-before? i i))
494+
(is (j/not-before? i+5 i))
495+
(is (not (j/not-before? i i+5)))))
426496

427497
(testing "clocks"
428-
(is (j/after? (j/fixed-clock 1000) (j/fixed-clock 0)))
429-
(is (j/before? (j/fixed-clock 1000) (j/fixed-clock 5000))))
498+
(let [fc (j/fixed-clock 0)
499+
fc+1000 (j/fixed-clock 1000)]
500+
(is (j/after? fc+1000 fc))
501+
(is (not (j/after? fc fc+1000)))
502+
(is (j/before? fc fc+1000))
503+
(is (not (j/before? fc+1000 fc)))
504+
(is (j/not-after? fc fc))
505+
(is (j/not-after? fc fc+1000))
506+
(is (not (j/not-after? fc+1000 fc)))
507+
(is (j/not-before? fc fc))
508+
(is (j/not-before? fc+1000 fc))
509+
(is (not (j/not-before? fc fc+1000)))))
430510

431511
(testing "fields"
432-
(is (j/after? (j/day-of-week :saturday) :thursday))
433-
(is (j/before? (j/day-of-week :saturday) :sunday))
434-
435-
(is (j/after? (j/month :february) :january))
436-
(is (j/before? (j/month :february) :march))
437-
438-
(is (j/after? (j/year 2010) 2009))
439-
(is (j/before? (j/year 2010) 2011))
440-
441-
(is (j/after? (j/month-day 5 1) (j/month-day 4 1)))
442-
(is (j/before? (j/month-day 1 1) (j/month-day 4 1)))))
512+
(let [thursday (j/day-of-week :thursday)
513+
saturday (j/day-of-week :saturday)
514+
sunday (j/day-of-week :sunday)]
515+
(is (j/after? saturday :thursday))
516+
(is (not (j/after? thursday :saturday)))
517+
(is (j/before? saturday :sunday))
518+
(is (not (j/before? sunday :saturday)))
519+
(is (j/not-after? saturday saturday))
520+
(is (j/not-after? saturday sunday))
521+
(is (not (j/not-after? sunday saturday)))
522+
(is (j/not-before? saturday saturday))
523+
(is (j/not-before? sunday saturday))
524+
(is (not (j/not-before? saturday sunday))))
525+
526+
(let [january (j/month :january)
527+
february (j/month :february)
528+
march (j/month :march)]
529+
(is (j/after? february :january))
530+
(is (not (j/after? january :february)))
531+
(is (j/before? february :march))
532+
(is (not (j/before? march :february)))
533+
(is (j/not-after? january january))
534+
(is (j/not-after? february march))
535+
(is (not (j/not-after? march february)))
536+
(is (j/not-before? january january))
537+
(is (j/not-before? february january))
538+
(is (not (j/not-before? january february))))
539+
540+
(let [year-2009 (j/year 2009)
541+
year-2010 (j/year 2010)]
542+
(is (j/after? year-2010 2009))
543+
(is (not (j/after? year-2009 2010)))
544+
(is (j/before? year-2009 2010))
545+
(is (not (j/before? year-2010 2009)))
546+
(is (j/not-after? year-2010 year-2010))
547+
(is (j/not-after? year-2009 year-2010))
548+
(is (not (j/not-after? year-2010 year-2009)))
549+
(is (j/not-before? year-2010 year-2010))
550+
(is (j/not-before? year-2010 year-2009))
551+
(is (not (j/not-before? year-2009 year-2010))))
552+
553+
(let [jan-1 (j/month-day 1 1)
554+
apr-1 (j/month-day 4 1)]
555+
(is (j/after? apr-1 jan-1))
556+
(is (not (j/after? jan-1 apr-1)))
557+
(is (j/before? jan-1 apr-1))
558+
(is (not (j/before? apr-1 jan-1)))
559+
(is (j/not-after? jan-1 jan-1))
560+
(is (j/not-after? jan-1 apr-1))
561+
(is (not (j/not-after? apr-1 jan-1)))
562+
(is (j/not-before? jan-1 jan-1))
563+
(is (j/not-before? apr-1 jan-1))
564+
(is (not (j/not-before? jan-1 apr-1))))))
443565

444566
(deftest mock-clock
445567
(testing "constructors"
@@ -756,13 +878,33 @@
756878
(is (nil? (j/gap (j/interval 0 1000) (j/interval 500 1500))))))
757879

758880
(testing "ordering"
759-
(is (j/before? (j/interval 1000 2000) (j/instant 5000)))
760-
(is (not (j/before? (j/interval 1000 5000) (j/instant 5000))))
761-
(is (j/before? (j/interval 1000 5000) (j/interval 5001 6000)))
762-
763-
(is (j/after? (j/interval 1000 5000) (j/instant 100)))
764-
(is (not (j/after? (j/interval 1000 5000) (j/instant 2000))))
765-
(is (j/after? (j/interval 1000 5000) (j/interval 100 999)))))
881+
(let [interval-1-2 (j/interval 1 2)
882+
interval-3-4 (j/interval 3 4)
883+
instant-1 (j/instant 1)
884+
instant-3 (j/instant 3)]
885+
(is (j/before? interval-1-2 interval-3-4))
886+
(is (not (j/before? interval-3-4 interval-1-2)))
887+
(is (j/before? interval-1-2 instant-3))
888+
(is (not (j/before? interval-3-4 instant-1)))
889+
890+
(is (j/after? interval-3-4 interval-1-2))
891+
(is (not (j/after? interval-1-2 interval-3-4)))
892+
(is (j/after? interval-3-4 instant-1))
893+
(is (not (j/after? interval-1-2 instant-3)))
894+
895+
(is (j/not-before? interval-3-4 interval-3-4))
896+
(is (j/not-before? interval-1-2 interval-3-4))
897+
(is (not (j/not-before? interval-1-2 interval-3-4)))
898+
(is (j/not-before? interval-3-4 instant-3))
899+
(is (j/not-before? interval-3-4 instant-1))
900+
(is (not (j/not-before? interval-1-2 instant-3)))
901+
902+
(is (j/not-after? interval-1-2 interval-1-2))
903+
(is (j/not-after? interval-1-2 interval-3-4))
904+
(is (not (j/not-after? interval-3-4 interval-1-2)))
905+
(is (j/not-after? interval-1-2 instant-1))
906+
(is (j/not-after? interval-1-2 instant-3))
907+
(is (not (j/not-after? interval-3-4 instant-1)))))
766908

767909
(jt.u/when-joda-time-loaded
768910

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /