From 2ebc4dda4e64662ad1bfcbfd27f52d8790a7e3ff Mon Sep 17 00:00:00 2001 From: Ambrose Bonnaire-Sergeant Date: Wed, 15 Nov 2023 23:00:05 -0600 Subject: [PATCH] Fix #98: Fix not-{before,after}?, introduce <, >, <=, >=, +, -, neg? --- CHANGELOG.md | 12 ++ docs/CHANGELOG.html | 2 +- docs/README.html | 6 +- docs/index.html | 4 +- docs/java-time.api.html | 345 +++++++++++++++++++-------------- docs/java-time.html | 347 ++++++++++++++++++++-------------- docs/java-time.repl.html | 2 +- project.clj | 10 +- src/java_time.clj | 13 +- src/java_time/api.clj | 13 +- src/java_time/core.clj | 183 +++++++++++++++--- test/java_time/api_test.clj | 343 +++++++++++++++------------------ test/java_time/dev/gen.clj | 6 +- test/java_time/test_utils.clj | 241 +++++++++++++++++++++++ 14 files changed, 998 insertions(+), 529 deletions(-) create mode 100644 test/java_time/test_utils.clj diff --git a/CHANGELOG.md b/CHANGELOG.md index 5529f2a..4be98b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## NEXT + +- [#98](https://github.com/dm3/clojure.java-time/issues/98): fix `not-before?` and `not-after?` with one or more than two arguments +- new aliases + - `java-time.api/+` aliases `java-time.api/plus` + - `java-time.api/-` aliases `java-time.api/minus` + - `java-time.api/neg?` aliases `java-time.api/negative?` + - `java-time.api/<` aliases `java-time.api/before?` + - `java-time.api/>` aliases `java-time.api/after?` + - `java-time.api/<=` aliases `java-time.api/not-after?` + - `java-time.api/>=` aliases `java-time.api/not-before?` + ## 1.3.0 - remove `:redef` on protocol methods, they are never direct-linked: https://ask.clojure.org/index.php/10967/are-protocol-methods-guaranteed-to-not-be-directly-linked?show=10990#a10990 diff --git a/docs/CHANGELOG.html b/docs/CHANGELOG.html index 4e3e0a0..2dc1fdb 100644 --- a/docs/CHANGELOG.html +++ b/docs/CHANGELOG.html @@ -1,6 +1,6 @@ -Changelog

Changelog

+Changelog

Changelog

1.3.0

-

instant->sql-timestamp

(instant->sql-timestamp instant-or-millis)

Creates a java.sql.Timestamp from the provided instant-or-millis - a millisecond numeric time value or something convertible to an Instant.

+

instant->sql-timestamp

(instant->sql-timestamp instant-or-millis)

Creates a java.sql.Timestamp from the provided instant-or-millis - a millisecond numeric time value or something convertible to an Instant.

Please consider using the JSR-310 Java Time types instead of java.sql.Timestamp if your drivers support them.

java.sql.Timestamp is a version of a java.util.Date supposed to be used as a local date-time (no time zone) for the purposes of conversion from/to native JDBC driver TIMESTAMP types.

-

instant?

(instant? v)

Returns true if v is an instance of java.time.Instant, otherwise false.

-

interval

(interval o)(interval a b)

Constructs an interval out of a string, start and end instants or a start + duration:

+

instant?

(instant? v)

Returns true if v is an instance of java.time.Instant, otherwise false.

+

interval

(interval o)(interval a b)

Constructs an interval out of a string, start and end instants or a start + duration:

(j/interval "2010-01-01T00:00:00Z/2013-01-01T00:00:00Z")
 => #<Interval 2010-01-01T00:00:00Z/2013-01-01T00:00:00Z>
 
@@ -145,8 +186,8 @@
 => #<Interval 1970-01-01T00:01:40Z/1970-01-01T00:16:40Z>
 

Requires the optional threeten-extra dependency.

-

interval?

(interval? o)

True if Interval

-

iterate

(iterate f initial v & vs)

Returns a lazy sequence of initial , (apply f initial v vs), etc.

+

interval?

(interval? o)

True if Interval

+

iterate

(iterate f initial v & vs)

Returns a lazy sequence of initial , (apply f initial v vs), etc.

Useful when you want to produce a sequence of temporal entities, for example:

(iterate plus (days 0) 1)
 => (#<Period P0D> #<Period P1D> #<Period P2D> ...)
@@ -157,11 +198,11 @@
 (iterate adjust (local-date 2010 1 1) :next-working-day)
 => (#<LocalDate 2010-01-01> #<LocalDate 2010-01-04> ...)
 
-

java-date

(java-date)(java-date a)(java-date a b)

Creates a java.util.Date out of any combination of arguments valid for instant or the Instant itself.

+

java-date

(java-date)(java-date a)(java-date a b)

Creates a java.util.Date out of any combination of arguments valid for instant or the Instant itself.

A java.util.Date represents an instant in time. It’s a direct analog of the java.time.Instant type introduced in the JSR-310. Please consider using the java.time.Instant (through instant) directly.

-

largest-min-value

(largest-min-value p)

Largest minimum value of this property

-

leap?

(leap? o)

True if the year of this entity is a leap year.

-

local-date

(local-date)(local-date arg0)(local-date arg0 arg1)(local-date arg0 arg1 arg2)

Creates a LocalDate. The following arguments are supported:

+

largest-min-value

(largest-min-value p)

Largest minimum value of this property

+

leap?

(leap? o)

True if the year of this entity is a leap year.

+

local-date

(local-date)(local-date arg0)(local-date arg0 arg1)(local-date arg0 arg1 arg2)

Creates a LocalDate. The following arguments are supported:

  • no arguments - current local-date
  • one argument @@ -186,7 +227,7 @@
-

local-date-time

(local-date-time)(local-date-time y m d h)(local-date-time y m d h mm)(local-date-time y m d h mm ss)(local-date-time y m d h mm ss n)(local-date-time arg0)(local-date-time arg0 arg1)(local-date-time arg0 arg1 arg2)

Creates a LocalDateTime. The following arguments are supported:

+

local-date-time

(local-date-time)(local-date-time y m d h)(local-date-time y m d h mm)(local-date-time y m d h mm ss)(local-date-time y m d h mm ss n)(local-date-time arg0)(local-date-time arg0 arg1)(local-date-time arg0 arg1 arg2)

Creates a LocalDateTime. The following arguments are supported:

  • no arguments - current local date-time
  • one argument @@ -209,9 +250,9 @@

    three and more arguments - year/month/day/…

-

local-date-time?

(local-date-time? v)

Returns true if v is an instance of java.time.LocalDateTime, otherwise false.

-

local-date?

(local-date? v)

Returns true if v is an instance of java.time.LocalDate, otherwise false.

-

local-time

(local-time)(local-time h m s nn)(local-time arg0)(local-time arg0 arg1)(local-time arg0 arg1 arg2)

Creates a LocalTime. The following arguments are supported:

+

local-date-time?

(local-date-time? v)

Returns true if v is an instance of java.time.LocalDateTime, otherwise false.

+

local-date?

(local-date? v)

Returns true if v is an instance of java.time.LocalDate, otherwise false.

+

local-time

(local-time)(local-time h m s nn)(local-time arg0)(local-time arg0 arg1)(local-time arg0 arg1 arg2)

Creates a LocalTime. The following arguments are supported:

  • no arguments - current local time
  • one argument @@ -231,19 +272,19 @@
  • three/four arguments - hour, minute, second, nanos
-

local-time?

(local-time? v)

Returns true if v is an instance of java.time.LocalTime, otherwise false.

-

max

(max o & os)

Latest/longest of the given time entities. Entities should be of the same type

-

max-value

(max-value p)

Maximum value of this property, e.g. 29th of February for months

-

micros

(micros micros)

Duration of a specified number of microseconds.

-

millis

(millis i)

Returns a Duration of i millis.

-

min

(min o & os)

Earliest/shortest of the given time entities. Entities should be of the same type

-

min-value

(min-value p)

Minimum value of this property

-

minus

(minus o & os)

Subtracts all of the os from the time entity o

+

local-time?

(local-time? v)

Returns true if v is an instance of java.time.LocalTime, otherwise false.

+

max

(max o & os)

Latest/longest of the given time entities. Entities should be of the same type

+

max-value

(max-value p)

Maximum value of this property, e.g. 29th of February for months

+

micros

(micros micros)

Duration of a specified number of microseconds.

+

millis

(millis i)

Returns a Duration of i millis.

+

min

(min o & os)

Earliest/shortest of the given time entities. Entities should be of the same type

+

min-value

(min-value p)

Minimum value of this property

+

minus

(minus o & os)

Subtracts all of the os from the time entity o

(j/minus (j/local-date 2015) (j/years 1))
 => <java.time.LocalDate "2014-01-01">
 
-

minutes

(minutes i)

Returns a Duration of i minutes.

-

mock-clock

(mock-clock)(mock-clock instant)(mock-clock instant zone)

Returns a mock implementation of the java.time.Clock. The mock supports advance-clock! operation which allows to move the time in the clock, e.g.:

+

minutes

(minutes i)

Returns a Duration of i minutes.

+

mock-clock

(mock-clock)(mock-clock instant)(mock-clock instant zone)

Returns a mock implementation of the java.time.Clock. The mock supports advance-clock! operation which allows to move the time in the clock, e.g.:

(let [clock (mock-clock 0 "UTC")]
   (with-clock clock
     (is (= (value clock) 0))
@@ -254,13 +295,13 @@
 

You can move the clock back via advancing by a negative temporal amount.

Creates a clock at epoch in the default time zone when called without arguments.

-

monday?

(monday? i)

Returns true if the given time entity with the day-of-week property falls on a Monday, otherwise false.

-

month

(month)(month v)(month fmt arg)

Returns the Month for the given month keyword name (e.g. :january), ordinal or entity. Current month if no arguments given.

-

month-day

(month-day)(month-day arg)(month-day a b)

Returns the MonthDay for the given entity, string, clock, zone or month/day combination. Current month-day if no arguments given.

-

month-day?

(month-day? o)

Returns true if o is java.time.MonthDay, otherwise false.

-

month?

(month? o)

True if java.time.Month.

-

months

(months i)

Returns a Period of i months.

-

move-end-by

(move-end-by i & os)

Moves the end instant of the interval by the sum of given periods/durations/numbers of milliseconds.

+

monday?

(monday? i)

Returns true if the given time entity with the day-of-week property falls on a Monday, otherwise false.

+

month

(month)(month v)(month fmt arg)

Returns the Month for the given month keyword name (e.g. :january), ordinal or entity. Current month if no arguments given.

+

month-day

(month-day)(month-day arg)(month-day a b)

Returns the MonthDay for the given entity, string, clock, zone or month/day combination. Current month-day if no arguments given.

+

month-day?

(month-day? o)

Returns true if o is java.time.MonthDay, otherwise false.

+

month?

(month? o)

True if java.time.Month.

+

months

(months i)

Returns a Period of i months.

+

move-end-by

(move-end-by i & os)

Moves the end instant of the interval by the sum of given periods/durations/numbers of milliseconds.

(move-start-by (interval 0 10000) (millis 1000) (seconds 1))
 => #<Interval ...:00Z/...:12Z>
 
@@ -268,7 +309,7 @@
(move-end-by (interval 0 10000) (millis -11000))
 => DateTimeException...
 
-

move-end-to

(move-end-to i new-end)

Moves the end of the interval to the given instant (or something convertible to an instant):

+

move-end-to

(move-end-to i new-end)

Moves the end of the interval to the given instant (or something convertible to an instant):

(move-end-to (interval 0 10000) (instant 15000))
 => #<Interval ...:00Z/...:15Z>
 
@@ -276,7 +317,7 @@
(move-end-to (interval 0 10000) (millis -1))
 => DateTimeException...
 
-

move-start-by

(move-start-by i & os)

Moves the start instant of the interval by the sum of given periods/durations/numbers of milliseconds:

+

move-start-by

(move-start-by i & os)

Moves the start instant of the interval by the sum of given periods/durations/numbers of milliseconds:

(move-start-by (interval 0 10000) (millis 1000) (seconds 1))
 => #<Interval ...:02Z/...:10Z>
 
@@ -284,7 +325,7 @@
(move-start-by (interval 0 10000) (millis 11000))
 ;=> DateTimeException...
 
-

move-start-to

(move-start-to i new-start)

Moves the start instant of the interval to the given instant (or something convertible to an instant):

+

move-start-to

(move-start-to i new-start)

Moves the start instant of the interval to the given instant (or something convertible to an instant):

(move-start-to (interval 0 10000) (instant 5000))
 => #<Interval ...:05Z/...:10Z>
 
@@ -292,15 +333,29 @@
(move-start-to (interval 0 10000) (millis 15000))
 => DateTimeException...
 
-

multiply-by

(multiply-by o v)

Entity o multiplied by the value v

-

nanos

(nanos i)

Returns a Duration of i nanos.

-

negate

(negate a)

Negates a temporal amount:

+

multiply-by

(multiply-by o v)

Entity o multiplied by the value v

+

nanos

(nanos i)

Returns a Duration of i nanos.

+

negate

(negate a)

Negates a temporal amount:

(negate (negate x)) == x

-

negative?

(negative? a)

True if the amount is negative

-

not-after?

(not-after? x)(not-after? x y)(not-after? x y & more)

Like before?, except returns true if the inputs are equal.

-

not-before?

(not-before? x)(not-before? x y)(not-before? x y & more)

Like after?, except returns true if the inputs are equal.

-

offset-clock

(offset-clock d)(offset-clock c d)

Creates a clock offset from the current/provided clock by a given duration.

-

offset-date-time

(offset-date-time)(offset-date-time y m d h)(offset-date-time y mo d h m)(offset-date-time y mo d h m s)(offset-date-time y mo d h m s n)(offset-date-time y mo d h m s n o)(offset-date-time arg0)(offset-date-time arg0 arg1)(offset-date-time arg0 arg1 arg2)

Creates an OffsetDateTime. The following arguments are supported:

+

negative?

(negative? a)

True if the amount is negative

+

not-after?

(not-after? x)(not-after? x y)(not-after? x y & more)

Returns true if time entities are ordered from the earliest to the latest (same semantics as <=), otherwise false.

+
(not-after? (local-date 2009) (local-date 2010) (local-date 2011))
+;=> true
+
+(not-after? (interval (instant 10000) (instant 1000000))
+            (instant 99999999))
+;=> true
+
+

not-before?

(not-before? x)(not-before? x y)(not-before? x y & more)

Returns true if time entities are ordered from the latest to the earliest (same semantics as >=), otherwise false.

+
(not-before? (local-date 2011) (local-date 2010) (local-date 2009))
+;=> true
+
+(not-before? (instant 99999999)
+             (interval (instant 10000) (instant 1000000)))
+;=> true
+
+

offset-clock

(offset-clock d)(offset-clock c d)

Creates a clock offset from the current/provided clock by a given duration.

+

offset-date-time

(offset-date-time)(offset-date-time y m d h)(offset-date-time y mo d h m)(offset-date-time y mo d h m s)(offset-date-time y mo d h m s n)(offset-date-time y mo d h m s n o)(offset-date-time arg0)(offset-date-time arg0 arg1)(offset-date-time arg0 arg1 arg2)

Creates an OffsetDateTime. The following arguments are supported:

  • no arguments - current date-time with the default offset
  • one argument @@ -330,8 +385,8 @@
  • eight arguments - time fields up to nanoseconds and a zone offset

If zone offset is not specified, default will be used. You can check the default offset by invoking (zone-offset).

-

offset-date-time?

(offset-date-time? v)

Returns true if v is an instance of java.time.OffsetDateTime, otherwise false.

-

offset-time

(offset-time)(offset-time h m s)(offset-time h m s n)(offset-time h m s n o)(offset-time arg0)(offset-time arg0 arg1)

Creates an OffsetTime. The following arguments are supported:

+

offset-date-time?

(offset-date-time? v)

Returns true if v is an instance of java.time.OffsetDateTime, otherwise false.

+

offset-time

(offset-time)(offset-time h m s)(offset-time h m s n)(offset-time h m s n o)(offset-time arg0)(offset-time arg0 arg1)

Creates an OffsetTime. The following arguments are supported:

  • no arguments - current time with the default offset
  • one argument @@ -356,10 +411,10 @@
  • five arguments - last is the offset

If zone offset is not specified, default will be used. You can check the default offset by invoking (zone-offset).

-

offset-time?

(offset-time? v)

Returns true if v is an instance of java.time.OffsetTime, otherwise false.

-

overlap

(overlap i oi)

Gets the overlap between this interval and the other one or nil

-

overlaps?

(overlaps? i oi)

True if this interval overlaps the other one

-

period

(period)(period arg0)(period arg0 arg1)(period arg0 arg1 arg2)

Creates a period - a temporal entity consisting of years, months and days.

+

offset-time?

(offset-time? v)

Returns true if v is an instance of java.time.OffsetTime, otherwise false.

+

overlap

(overlap i oi)

Gets the overlap between this interval and the other one or nil

+

overlaps?

(overlaps? i oi)

True if this interval overlaps the other one

+

period

(period)(period arg0)(period arg0 arg1)(period arg0 arg1 arg2)

Creates a period - a temporal entity consisting of years, months and days.

Given one argument will

  • interpret as years if a number
  • @@ -374,98 +429,98 @@
  • get a period of a specified number of years and months

Given three arguments will create a year/month/day period.

-

period?

(period? v)

Returns true if v is an instance of java.time.Period, otherwise false.

-

plus

(plus o & os)

Adds all of the os to the time entity o. plus is not commutative, the first argument is always the entity which will accumulate the rest of the arguments.

+

period?

(period? v)

Returns true if v is an instance of java.time.Period, otherwise false.

+

plus

(plus o & os)

Adds all of the os to the time entity o. plus is not commutative, the first argument is always the entity which will accumulate the rest of the arguments.

(j/plus (j/local-date 2015) (j/years 1))
 => <java.time.LocalDate "2016-01-01">
 
-

properties

(properties o)

Map of properties present in this temporal entity

-

property

(property o k)

Property of this temporal entity under key k

-

quarter

(quarter)(quarter v)(quarter fmt arg)

Returns the Quarter for the given quarter keyword name (e.g. :q1), ordinal or entity. Current quarter if no arguments given.

-

quarter?

(quarter? o)

True if org.threeten.extra.Quarter.

-

range

(range p)

Range of values for this property

-

saturday?

(saturday? i)

Returns true if the given time entity with the day-of-week property falls on a Saturday, otherwise false.

-

seconds

(seconds i)

Returns a Duration of i seconds.

-

set-clock!

(set-clock! clock time)

Sets the clock to the given time.

+

properties

(properties o)

Map of properties present in this temporal entity

+

property

(property o k)

Property of this temporal entity under key k

+

quarter

(quarter)(quarter v)(quarter fmt arg)

Returns the Quarter for the given quarter keyword name (e.g. :q1), ordinal or entity. Current quarter if no arguments given.

+

quarter?

(quarter? o)

True if org.threeten.extra.Quarter.

+

range

(range p)

Range of values for this property

+

saturday?

(saturday? i)

Returns true if the given time entity with the day-of-week property falls on a Saturday, otherwise false.

+

seconds

(seconds i)

Returns a Duration of i seconds.

+

set-clock!

(set-clock! clock time)

Sets the clock to the given time.

This mutates the mock clock.

-

smallest-max-value

(smallest-max-value p)

Smallest maximum value of this property, e.g. 28th of February for months

-

sql-date

(sql-date)(sql-date arg0)(sql-date arg0 arg1)(sql-date arg0 arg1 arg2)

Creates a java.sql.Date out of any combination of arguments valid for local-date or the LocalDate itself.

+

smallest-max-value

(smallest-max-value p)

Smallest maximum value of this property, e.g. 28th of February for months

+

sql-date

(sql-date)(sql-date arg0)(sql-date arg0 arg1)(sql-date arg0 arg1 arg2)

Creates a java.sql.Date out of any combination of arguments valid for local-date or the LocalDate itself.

Please consider using the JSR-310 Java Time types instead of java.sql.Date if your drivers support them.

Even though java.sql.Date extends a java.util.Date, it’s supposed to be used as a local date (no time component or time zone) for the purposes of conversion from/to native JDBC driver DATE types.

-

sql-time

(sql-time)(sql-time arg0)(sql-time arg0 arg1)(sql-time arg0 arg1 arg2)

Creates a java.sql.Time out of any combination of arguments valid for local-time (except the nanos constructor) or the LocalTime itself.

+

sql-time

(sql-time)(sql-time arg0)(sql-time arg0 arg1)(sql-time arg0 arg1 arg2)

Creates a java.sql.Time out of any combination of arguments valid for local-time (except the nanos constructor) or the LocalTime itself.

Please consider using the JSR-310 Java Time types instead of java.sql.Time if your drivers support them.

Even though java.sql.Time extends a java.util.Date, it’s supposed to be used as a local time (no date component or time zone) for the purposes of conversion from/to native JDBC driver TIME types.

-

sql-timestamp

(sql-timestamp)(sql-timestamp arg0)(sql-timestamp arg0 arg1)(sql-timestamp arg0 arg1 arg2)(sql-timestamp arg0 arg1 arg2 arg3)(sql-timestamp arg0 arg1 arg2 arg3 arg4)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5 arg6)

Creates a java.sql.Timestamp in the local time zone out of any combination of arguments valid for local-date-time or the LocalDateTime itself.

+

sql-timestamp

(sql-timestamp)(sql-timestamp arg0)(sql-timestamp arg0 arg1)(sql-timestamp arg0 arg1 arg2)(sql-timestamp arg0 arg1 arg2 arg3)(sql-timestamp arg0 arg1 arg2 arg3 arg4)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5 arg6)

Creates a java.sql.Timestamp in the local time zone out of any combination of arguments valid for local-date-time or the LocalDateTime itself.

Does not support Timestamp construction from an Instant or a long millis value—please use instant->sql-timestamp for this purpose.

Please consider using the JSR-310 Java Time types instead of java.sql.Timestamp if your drivers support them.

java.sql.Timestamp is a version of a java.util.Date supposed to be used as a local date-time (no time zone) for the purposes of conversion from/to native JDBC driver TIMESTAMP types.

-

standard-days

(standard-days i)

Returns a Duration of i days.

-

start

(start i)

Gets the start instant of the interval

-

sunday?

(sunday? i)

Returns true if the given time entity with the day-of-week property falls on a Sunday, otherwise false.

-

supports?

(supports? o p)

True if the o entity supports the p property

-

system-clock

(system-clock)(system-clock k)

Creates a system clock. In the default time zone if called without arguments, otherwise accepts a Zone Id.

-

thursday?

(thursday? i)

Returns true if the given time entity with the day-of-week property falls on a Thursday, otherwise false.

-

tick-clock

(tick-clock d)(tick-clock c d)

Creates a clock wrapping system/provided clock that only ticks as per specified duration.

-

time-between

(time-between o e u)

Time between temporal entities o and e in unit u.

+

standard-days

(standard-days i)

Returns a Duration of i days.

+

start

(start i)

Gets the start instant of the interval

+

sunday?

(sunday? i)

Returns true if the given time entity with the day-of-week property falls on a Sunday, otherwise false.

+

supports?

(supports? o p)

True if the o entity supports the p property

+

system-clock

(system-clock)(system-clock k)

Creates a system clock. In the default time zone if called without arguments, otherwise accepts a Zone Id.

+

thursday?

(thursday? i)

Returns true if the given time entity with the day-of-week property falls on a Thursday, otherwise false.

+

tick-clock

(tick-clock d)(tick-clock c d)

Creates a clock wrapping system/provided clock that only ticks as per specified duration.

+

time-between

(time-between o e u)

Time between temporal entities o and e in unit u.

(j/time-between (j/local-date 2015) (j/local-date 2016) :days)
 => 365
 
 (j/time-between :days (j/local-date 2015) (j/local-date 2016))
 => 365
 
-

to-java-date

deprecated

(to-java-date o)

Converts a date entity to a java.util.Date.

+

to-java-date

deprecated

(to-java-date o)

Converts a date entity to a java.util.Date.

Deprecated: This function only has a single arity and works for entities directly convertible to java.time.Instant. Please consider using java-date instead.

-

to-millis-from-epoch

(to-millis-from-epoch o)

Converts a date entity to a long representing the number of milliseconds from epoch.

-

to-sql-date

deprecated

(to-sql-date o)

Converts a local date entity to a java.sql.Date.

+

to-millis-from-epoch

(to-millis-from-epoch o)

Converts a date entity to a long representing the number of milliseconds from epoch.

+

to-sql-date

deprecated

(to-sql-date o)

Converts a local date entity to a java.sql.Date.

Deprecated: This function only has a single arity and works for entities directly convertible to java.time.LocalDate. Please consider using sql-date instead.

-

to-sql-timestamp

deprecated

(to-sql-timestamp o)

Converts a date entity to a java.sql.Timestamp.

+

to-sql-timestamp

deprecated

(to-sql-timestamp o)

Converts a date entity to a java.sql.Timestamp.

Deprecated: This function only has a single arity and works for entities directly convertible to java.time.Instant. Please consider using sql-timestamp instead.

-

truncate-to

(truncate-to o u)

Truncates this entity to the specified time unit. Only works for units that divide into the length of standard day without remainder (up to :days).

-

tuesday?

(tuesday? i)

Returns true if the given time entity with the day-of-week property falls on a Tuesday, otherwise false.

-

unit

(unit k)(unit entity k)

Returns a TemporalUnit for the given key k or extracts the field from the given temporal entity.

+

truncate-to

(truncate-to o u)

Truncates this entity to the specified time unit. Only works for units that divide into the length of standard day without remainder (up to :days).

+

tuesday?

(tuesday? i)

Returns true if the given time entity with the day-of-week property falls on a Tuesday, otherwise false.

+

unit

(unit k)(unit entity k)

Returns a TemporalUnit for the given key k or extracts the field from the given temporal entity.

You can see predefined units via java-time.repl/show-units.

If you want to make your own custom TemporalUnits resolvable, you need to rebind the java-time.properties/*units* to a custom java_time.properties.UnitGroup.

-

unit?

(unit? o)

True if this is a TemporalUnit.

-

units

(units o)

Units present in this temporal entity.

-

value

(value p)

Value of the property

-

value-range

(value-range min max)(value-range arg0)

Creates a ValueRange given the min and max amounts or a map of :min-smallest, :max-smallest, :min-largest and :max-largest.

-

wednesday?

(wednesday? i)

Returns true if the given time entity with the day-of-week property falls on a Wednesday, otherwise false.

-

weekday?

(weekday? dt)

Complement of weekend?.

-

weekend?

(weekend? dt)

Returns true if argument is saturday? or sunday?, otherwise false.

-

weeks

(weeks i)

Returns a Period of i weeks.

-

when-joda-time-loaded

macro

(when-joda-time-loaded & body)

Execute the body when Joda-Time classes are found on the classpath.

+

unit?

(unit? o)

True if this is a TemporalUnit.

+

units

(units o)

Units present in this temporal entity.

+

value

(value p)

Value of the property

+

value-range

(value-range min max)(value-range arg0)

Creates a ValueRange given the min and max amounts or a map of :min-smallest, :max-smallest, :min-largest and :max-largest.

+

wednesday?

(wednesday? i)

Returns true if the given time entity with the day-of-week property falls on a Wednesday, otherwise false.

+

weekday?

(weekday? dt)

Complement of weekend?.

+

weekend?

(weekend? dt)

Returns true if argument is saturday? or sunday?, otherwise false.

+

weeks

(weeks i)

Returns a Period of i weeks.

+

when-joda-time-loaded

macro

(when-joda-time-loaded & body)

Execute the body when Joda-Time classes are found on the classpath.

Take care - when AOT-compiling code using this macro, the Joda-Time classes must be on the classpath at compile time!

-

with-clock

macro

(with-clock c & forms)

Executes the given forms in the scope of the provided clock.

+

with-clock

macro

(with-clock c & forms)

Executes the given forms in the scope of the provided clock.

All the temporal entities that get created without parameters will inherit their values from the clock:

(with-clock (system-clock “Europe/London”) (zone-id)) => #<java.time.ZoneRegion Europe/London>

-

with-clock-fn

(with-clock-fn c f)

Executes the given function in the scope of the provided clock. All the temporal entities that get created without parameters will inherit their values from the clock.

-

with-largest-min-value

(with-largest-min-value p)

Underlying temporal entity with the value set to the largest minimum available for this property

-

with-max-value

(with-max-value p)

Underlying temporal entity with the value set to the maximum available for this property

-

with-min-value

(with-min-value p)

Underlying temporal entity with the value set to the minimum available for this property

-

with-offset

(with-offset o offset)

Sets the offset to the specified value ensuring that the local time stays the same.

+

with-clock-fn

(with-clock-fn c f)

Executes the given function in the scope of the provided clock. All the temporal entities that get created without parameters will inherit their values from the clock.

+

with-largest-min-value

(with-largest-min-value p)

Underlying temporal entity with the value set to the largest minimum available for this property

+

with-max-value

(with-max-value p)

Underlying temporal entity with the value set to the maximum available for this property

+

with-min-value

(with-min-value p)

Underlying temporal entity with the value set to the minimum available for this property

+

with-offset

(with-offset o offset)

Sets the offset to the specified value ensuring that the local time stays the same.

(offset-time 10 30 0 0 +2) => #<java.time.OffsetTime 10:30+02:00> (with-offset *1 +3) => #<java.time.OffsetTime 10:30+03:00>

-

with-offset-same-instant

(with-offset-same-instant o offset)

Sets the offset to the specified value ensuring that the result has the same instant, e.g.:

+

with-offset-same-instant

(with-offset-same-instant o offset)

Sets the offset to the specified value ensuring that the result has the same instant, e.g.:

(offset-time 10 30 0 0 +2) => #<java.time.OffsetTime 10:30+02:00> (with-offset-same-instant *1 +3) => #<java.time.OffsetTime 11:30+03:00>

-

with-smallest-max-value

(with-smallest-max-value p)

Underlying temporal entity with the value set to the smallest maximum available for this property

-

with-value

(with-value p v)

Underlying temporal entity with the value of this property set to v

-

with-zone

(with-zone o z)

Returns this temporal entity with the specified ZoneId

-

with-zone-same-instant

(with-zone-same-instant zdt z)

Sets the zone to the specified value ensuring that the result has the same instant, e.g.:

+

with-smallest-max-value

(with-smallest-max-value p)

Underlying temporal entity with the value set to the smallest maximum available for this property

+

with-value

(with-value p v)

Underlying temporal entity with the value of this property set to v

+

with-zone

(with-zone o z)

Returns this temporal entity with the specified ZoneId

+

with-zone-same-instant

(with-zone-same-instant zdt z)

Sets the zone to the specified value ensuring that the result has the same instant, e.g.:

(zoned-date-time 2015) => #<java.time.ZonedDateTime 2015-01-01T00:00+00:00Europe/London> (with-zone-same-instant *1 “America/New_York”) => #<java.time.ZonedDateTime 2014-12-31T18:00-05:00America/New_York>

-

year

(year)(year arg)(year fmt arg)

Returns the Year for the given entity, string, clock, zone or number. Current year if no arguments given.

-

year-month

(year-month)(year-month arg)(year-month a b)

Returns the YearMonth for the given entity, string, clock, zone or month/day combination. Current year-month if no arguments given.

-

year-month?

(year-month? o)

Returns true if o is java.time.YearMonth, otherwise false.

-

year-quarter

(year-quarter)(year-quarter arg)(year-quarter a b)

Returns the YearQuarter for the given entity, clock, zone or year with quarter. Current year quarter if no arguments given.

-

year-quarter?

(year-quarter? o)

Returns true if o is org.threeten.extra.YearQuarter, otherwise false.

-

year?

(year? o)

Returns true if o is java.time.Year, otherwise false.

-

years

(years i)

Returns a Period of i years.

-

zero?

(zero? a)

True if the amount is zero

-

zone-id

(zone-id)(zone-id arg0)(zone-id arg0 arg1)

Creates a ZoneId from a string identifier, java.util.TimeZone or extracts from another temporal entity.

+

year

(year)(year arg)(year fmt arg)

Returns the Year for the given entity, string, clock, zone or number. Current year if no arguments given.

+

year-month

(year-month)(year-month arg)(year-month a b)

Returns the YearMonth for the given entity, string, clock, zone or month/day combination. Current year-month if no arguments given.

+

year-month?

(year-month? o)

Returns true if o is java.time.YearMonth, otherwise false.

+

year-quarter

(year-quarter)(year-quarter arg)(year-quarter a b)

Returns the YearQuarter for the given entity, clock, zone or year with quarter. Current year quarter if no arguments given.

+

year-quarter?

(year-quarter? o)

Returns true if o is org.threeten.extra.YearQuarter, otherwise false.

+

year?

(year? o)

Returns true if o is java.time.Year, otherwise false.

+

years

(years i)

Returns a Period of i years.

+

zero?

(zero? a)

True if the amount is zero

+

zone-id

(zone-id)(zone-id arg0)(zone-id arg0 arg1)

Creates a ZoneId from a string identifier, java.util.TimeZone or extracts from another temporal entity.

Returns default system zone id if no arguments provided.

Given two arguments will use the second as the offset.

-

zone-id?

(zone-id? v)

Returns true if v is an instance of java.time.ZoneId, otherwise false.

-

zone-offset

(zone-offset)(zone-offset o)(zone-offset h m)(zone-offset h m s)

Creates a ZoneOffset from a string identifier (e.g. “+01”), a number of hours/hours and minutes/hours, minutes and seconds or extracts from another temporal entity.

+

zone-id?

(zone-id? v)

Returns true if v is an instance of java.time.ZoneId, otherwise false.

+

zone-offset

(zone-offset)(zone-offset o)(zone-offset h m)(zone-offset h m s)

Creates a ZoneOffset from a string identifier (e.g. “+01”), a number of hours/hours and minutes/hours, minutes and seconds or extracts from another temporal entity.

Returns default system zone offset if no arguments provided.

-

zoned-date-time

(zoned-date-time)(zoned-date-time y m d h)(zoned-date-time y mo d h m)(zoned-date-time y mo d h m s)(zoned-date-time y mo d h m s n)(zoned-date-time y mo d h m s n o)(zoned-date-time arg0)(zoned-date-time arg0 arg1)(zoned-date-time arg0 arg1 arg2)

Creates a ZonedDateTime. The following arguments are supported:

+

zoned-date-time

(zoned-date-time)(zoned-date-time y m d h)(zoned-date-time y mo d h m)(zoned-date-time y mo d h m s)(zoned-date-time y mo d h m s n)(zoned-date-time y mo d h m s n o)(zoned-date-time arg0)(zoned-date-time arg0 arg1)(zoned-date-time arg0 arg1 arg2)

Creates a ZonedDateTime. The following arguments are supported:

  • no arguments - current date-time in the default zone
  • one argument @@ -494,5 +549,5 @@
  • eight arguments - last is the zone id

If zone id is not specified, default zone id will be used. You can check the default zone by invoking (zone-id).

-

zoned-date-time?

(zoned-date-time? v)

Returns true if v is an instance of java.time.ZonedDateTime, otherwise false.

-
\ No newline at end of file +

zoned-date-time?

(zoned-date-time? v)

Returns true if v is an instance of java.time.ZonedDateTime, otherwise false.

+
\ No newline at end of file diff --git a/docs/java-time.html b/docs/java-time.html index 6ecc12e..7044e18 100644 --- a/docs/java-time.html +++ b/docs/java-time.html @@ -1,15 +1,56 @@ -java-time documentation

java-time

deprecated in 1.1.0

This namespace has been deprecated due to #91. Please migrate to java-time.api

+java-time documentation

java-time

deprecated in 1.1.0

This namespace has been deprecated due to #91. Please migrate to java-time.api

This namespace will continue to exist and be updated. For maximum JVM compatibility, please migrate to java-time.api, which provides the same interface. java-time and java-time.api and can be freely intermixed within the same library, so you can safely migrate your own code even if your dependencies use the old namespace.

Migration steps:

  1. rename all references from java-time to java-time.api. eg., (:require [java-time :as jt]) => (:require [java-time.api :as jt])
-

abs

(abs a)

Returns the absolute value of a temporal amount:

+

*

(* o v)

Entity o multiplied by the value v

+

+

(+ o & os)

Adds all of the os to the time entity o. + is not commutative, the first argument is always the entity which will accumulate the rest of the arguments.

+
(j/+ (j/local-date 2015) (j/years 1))
+=> <java.time.LocalDate "2016-01-01">
+
+

-

(- o & os)

Subtracts all of the os from the time entity o

+
(j/- (j/local-date 2015) (j/years 1))
+=> <java.time.LocalDate "2014-01-01">
+
+

<

(< x)(< x y)(< x y & more)

Returns true if time entities are ordered from the earliest to the latest (same semantics as <), otherwise false.

+
(j/< (local-date 2009) (local-date 2010) (local-date 2011))
+=> true
+
+(j/< (interval (instant 10000) (instant 1000000))
+     (instant 99999999))
+=> true
+
+

<=

(<= x)(<= x y)(<= x y & more)

Returns true if time entities are ordered from the earliest to the latest (same semantics as <=), otherwise false.

+
(j/<= (local-date 2009) (local-date 2010) (local-date 2011))
+;=> true
+
+(j/<= (interval (instant 10000) (instant 1000000))
+      (instant 99999999))
+;=> true
+
+

>

(> x)(> x y)(> x y & more)

Returns true if time entities are ordered from the latest to the earliest (same semantics as >), otherwise false.

+
(j/> (local-date 2011) (local-date 2010) (local-date 2009))
+=> true
+
+(j/> (instant 99999999)
+     (interval (instant 10000) (instant 1000000)))
+=> true
+
+

>=

(>= x)(>= x y)(>= x y & more)

Returns true if time entities are ordered from the latest to the earliest (same semantics as >=), otherwise false.

+
(j/>= (local-date 2011) (local-date 2010) (local-date 2009))
+;=> true
+
+(j/>= (instant 99999999)
+      (interval (instant 10000) (instant 1000000)))
+;=> true
+
+

abs

(abs a)

Returns the absolute value of a temporal amount:

(abs (negate x)) == (abs x)

-

abuts?

(abuts? i oi)

True if this interval abut with the other one

-

adjust

(adjust entity adjuster & args)

Adjusts the temporal entity using the provided adjuster with optional args.

+

abuts?

(abuts? i oi)

True if this interval abut with the other one

+

adjust

(adjust entity adjuster & args)

Adjusts the temporal entity using the provided adjuster with optional args.

The adjuster should either be a keyword which resolves to one of the predefined adjusters (see java-time.repl/show-adjusters) an instance of TemporalAdjuster or a function which returns another temporal entity when applied to the given one:

(adjust (local-date 2015 1 1) :next-working-day)
 => #<LocalDate 2015-1-2>
@@ -20,9 +61,9 @@
 (adjust (local-date 2015 1 1) plus (days 1))
 => #<LocalDate 2015-1-2>
 
-

advance-clock!

(advance-clock! clock amount)

Advances the clock by the given time amount.

+

advance-clock!

(advance-clock! clock amount)

Advances the clock by the given time amount.

This mutates the mock clock.

-

after?

(after? x)(after? x y)(after? x y & more)

Returns true if time entities are ordered from the latest to the earliest (same semantics as >), otherwise false.

+

after?

(after? x)(after? x y)(after? x y & more)

Returns true if time entities are ordered from the latest to the earliest (same semantics as >), otherwise false.

(after? (local-date 2011) (local-date 2010) (local-date 2009))
 => true
 
@@ -30,24 +71,24 @@
         (interval (instant 10000) (instant 1000000)))
 => true
 
-

am-pm

(am-pm)(am-pm v)(am-pm fmt arg)

Returns the AmPm for the given keyword name (:am or :pm), ordinal or entity. Current AM/PM if no arguments given.

-

am-pm?

(am-pm? o)

True if org.threeten.extra.AmPm.

-

as

(as o k)(as o k1 k2)(as o k1 k2 & ks)

Values of property/unit identified by keys/objects ks of the temporal entity o, e.g.

+

am-pm

(am-pm)(am-pm v)(am-pm fmt arg)

Returns the AmPm for the given keyword name (:am or :pm), ordinal or entity. Current AM/PM if no arguments given.

+

am-pm?

(am-pm? o)

True if org.threeten.extra.AmPm.

+

as

(as o k)(as o k1 k2)(as o k1 k2 & ks)

Values of property/unit identified by keys/objects ks of the temporal entity o, e.g.

(as (duration 1 :hour) :minutes)
 => 60
 
 (as (local-date 2015 9) :year :month-of-year)
 => [2015 9]
 
-

as-map

(as-map e)(as-map e value-fn)

Converts a time entity to a map of property key -> value as defined by the passed in value-fn. By default the actual value of the unit/field is produced.

+

as-map

(as-map e)(as-map e value-fn)

Converts a time entity to a map of property key -> value as defined by the passed in value-fn. By default the actual value of the unit/field is produced.

(as-map (duration))
 => {:nanos 0, :seconds 0}
 
 (as-map (local-date 2015 1 1))
 => {:year 2015, :month-of-year 1, :day-of-month 1, ...}
 
-

available-zone-ids

(available-zone-ids)

Returns a set of string identifiers for all available ZoneIds.

-

before?

(before? x)(before? x y)(before? x y & more)

Returns true if time entities are ordered from the earliest to the latest (same semantics as <), otherwise false.

+

available-zone-ids

(available-zone-ids)

Returns a set of string identifiers for all available ZoneIds.

+

before?

(before? x)(before? x y)(before? x y & more)

Returns true if time entities are ordered from the earliest to the latest (same semantics as <), otherwise false.

(before? (local-date 2009) (local-date 2010) (local-date 2011))
 => true
 
@@ -55,10 +96,10 @@
          (instant 99999999))
 => true
 
-

chronology

(chronology o)

The Chronology of the entity

-

clock?

(clock? x)

Returns true if x is an instance of java.time.Clock.

-

contains?

(contains? i o)

True if the interval contains the given instant or interval

-

convert-amount

(convert-amount amount from-unit to-unit)

Converts an amount from one unit to another. Returns a map of:

+

chronology

(chronology o)

The Chronology of the entity

+

clock?

(clock? x)

Returns true if x is an instance of java.time.Clock.

+

contains?

(contains? i o)

True if the interval contains the given instant or interval

+

convert-amount

(convert-amount amount from-unit to-unit)

Converts an amount from one unit to another. Returns a map of:

  • :whole - the whole part of the conversion in the to unit
  • :remainder - the remainder in the from unit
  • @@ -69,14 +110,14 @@
    (convert-amount 10000 :seconds :hours)
     => {:remainder 2800 :whole 2}
     
    -

day-of-month

(day-of-month)(day-of-month arg)(day-of-month fmt arg)

Returns the DayOfMonth for the given entity, clock, zone or day of month. Current day of month if no arguments given.

-

day-of-month?

(day-of-month? o)

Returns true if o is org.threeten.extra.DayOfMonth, otherwise false.

-

day-of-week

(day-of-week)(day-of-week v)(day-of-week fmt arg)

Returns the DayOfWeek for the given day keyword name (e.g. :monday), ordinal or entity. Current day if no arguments given.

-

day-of-week?

(day-of-week? o)

True if java.time.DayOfWeek.

-

day-of-year

(day-of-year)(day-of-year arg)(day-of-year fmt arg)

Returns the DayOfYear for the given entity, clock, zone or day of year. Current day of year if no arguments given.

-

day-of-year?

(day-of-year? o)

Returns true if o is org.threeten.extra.DayOfYear, otherwise false.

-

days

(days i)

Returns a Period of i days.

-

duration

(duration)(duration arg0)(duration arg0 arg1)

Creates a duration - a temporal entity representing standard days, hours, minutes, millis, micros and nanos. The duration itself contains only seconds and nanos as properties.

+

day-of-month

(day-of-month)(day-of-month arg)(day-of-month fmt arg)

Returns the DayOfMonth for the given entity, clock, zone or day of month. Current day of month if no arguments given.

+

day-of-month?

(day-of-month? o)

Returns true if o is org.threeten.extra.DayOfMonth, otherwise false.

+

day-of-week

(day-of-week)(day-of-week v)(day-of-week fmt arg)

Returns the DayOfWeek for the given day keyword name (e.g. :monday), ordinal or entity. Current day if no arguments given.

+

day-of-week?

(day-of-week? o)

True if java.time.DayOfWeek.

+

day-of-year

(day-of-year)(day-of-year arg)(day-of-year fmt arg)

Returns the DayOfYear for the given entity, clock, zone or day of year. Current day of year if no arguments given.

+

day-of-year?

(day-of-year? o)

Returns true if o is org.threeten.extra.DayOfYear, otherwise false.

+

days

(days i)

Returns a Period of i days.

+

duration

(duration)(duration arg0)(duration arg0 arg1)

Creates a duration - a temporal entity representing standard days, hours, minutes, millis, micros and nanos. The duration itself contains only seconds and nanos as properties.

Given one argument will:

  • interpret as millis if a number
  • @@ -89,15 +130,15 @@
  • get a duration between two Temporals
  • get a duration of a specified unit, e.g. (duration 100 :seconds)
-

duration?

(duration? v)

Returns true if v is an instance of java.time.Duration, otherwise false.

-

end

(end i)

Gets the end instant of the interval

-

field

(field k)(field entity k)

Returns a TemporalField for the given key k or extracts the field from the given temporal entity.

+

duration?

(duration? v)

Returns true if v is an instance of java.time.Duration, otherwise false.

+

end

(end i)

Gets the end instant of the interval

+

field

(field k)(field entity k)

Returns a TemporalField for the given key k or extracts the field from the given temporal entity.

You can see predefined fields via java-time.repl/show-fields.

If you want to make your own custom TemporalFields resolvable, you need to rebind the java-time.properties/*fields* to a custom java_time.properties.FieldGroup.

-

field?

(field? o)

True if this is a TemporalField.

-

fields

(fields o)

Fields present in this temporal entity

-

fixed-clock

(fixed-clock)(fixed-clock i)(fixed-clock i z)

Creates a fixed clock either at the current instant or at the supplied instant/instant + zone.

-

format

(format o)(format fmt o)

Formats the given time entity as a string.

+

field?

(field? o)

True if this is a TemporalField.

+

fields

(fields o)

Fields present in this temporal entity

+

fixed-clock

(fixed-clock)(fixed-clock i)(fixed-clock i z)

Creates a fixed clock either at the current instant or at the supplied instant/instant + zone.

+

format

(format o)(format fmt o)

Formats the given time entity as a string.

Accepts something that can be converted to a DateTimeFormatter or a formatter key, e.g. :iso-offset-time, as a first argument. Given one argument uses the default format.

(format (zoned-date-time))
 => "2015-03-21T09:22:46.677800+01:00[Europe/London]"
@@ -105,7 +146,7 @@
 (format :iso-date (zoned-date-time))
 "2015-03-21+01:00"
 
-

formatter

(formatter fmt)(formatter fmt arg1)

Constructs a DateTimeFormatter out of a

+

formatter

(formatter fmt)(formatter fmt arg1)

Constructs a DateTimeFormatter out of a

  • format string - “yyyy/MM/dd”, “HH:mm”, etc.
  • formatter name - :iso-date, :iso-time, etc.
  • @@ -115,10 +156,10 @@
  • resolver-style - either :strict, :smartor :lenient
  • case - either :insensitive or :sensitive (defaults to :sensitive)
-

friday?

(friday? i)

Returns true if the given time entity with the day-of-week property falls on a Friday, otherwise false.

-

gap

(gap i oi)

Gets the gap between this interval and the other one or nil

-

hours

(hours i)

Returns a Duration of i hours.

-

instant

(instant)(instant arg0)(instant arg0 arg1)

Creates an Instant. The following arguments are supported:

+

friday?

(friday? i)

Returns true if the given time entity with the day-of-week property falls on a Friday, otherwise false.

+

gap

(gap i oi)

Gets the gap between this interval and the other one or nil

+

hours

(hours i)

Returns a Duration of i hours.

+

instant

(instant)(instant arg0)(instant arg0 arg1)

Creates an Instant. The following arguments are supported:

  • no arguments - current instant
  • one argument @@ -136,11 +177,11 @@
-

instant->sql-timestamp

(instant->sql-timestamp instant-or-millis)

Creates a java.sql.Timestamp from the provided instant-or-millis - a millisecond numeric time value or something convertible to an Instant.

+

instant->sql-timestamp

(instant->sql-timestamp instant-or-millis)

Creates a java.sql.Timestamp from the provided instant-or-millis - a millisecond numeric time value or something convertible to an Instant.

Please consider using the JSR-310 Java Time types instead of java.sql.Timestamp if your drivers support them.

java.sql.Timestamp is a version of a java.util.Date supposed to be used as a local date-time (no time zone) for the purposes of conversion from/to native JDBC driver TIMESTAMP types.

-

instant?

(instant? v)

Returns true if v is an instance of java.time.Instant, otherwise false.

-

interval

(interval o)(interval a b)

Constructs an interval out of a string, start and end instants or a start + duration:

+

instant?

(instant? v)

Returns true if v is an instance of java.time.Instant, otherwise false.

+

interval

(interval o)(interval a b)

Constructs an interval out of a string, start and end instants or a start + duration:

(j/interval "2010-01-01T00:00:00Z/2013-01-01T00:00:00Z")
 => #<Interval 2010-01-01T00:00:00Z/2013-01-01T00:00:00Z>
 
@@ -151,8 +192,8 @@
 => #<Interval 1970-01-01T00:01:40Z/1970-01-01T00:16:40Z>
 

Requires the optional threeten-extra dependency.

-

interval?

(interval? o)

True if Interval

-

iterate

(iterate f initial v & vs)

Returns a lazy sequence of initial , (apply f initial v vs), etc.

+

interval?

(interval? o)

True if Interval

+

iterate

(iterate f initial v & vs)

Returns a lazy sequence of initial , (apply f initial v vs), etc.

Useful when you want to produce a sequence of temporal entities, for example:

(iterate plus (days 0) 1)
 => (#<Period P0D> #<Period P1D> #<Period P2D> ...)
@@ -163,11 +204,11 @@
 (iterate adjust (local-date 2010 1 1) :next-working-day)
 => (#<LocalDate 2010-01-01> #<LocalDate 2010-01-04> ...)
 
-

java-date

(java-date)(java-date a)(java-date a b)

Creates a java.util.Date out of any combination of arguments valid for instant or the Instant itself.

+

java-date

(java-date)(java-date a)(java-date a b)

Creates a java.util.Date out of any combination of arguments valid for instant or the Instant itself.

A java.util.Date represents an instant in time. It’s a direct analog of the java.time.Instant type introduced in the JSR-310. Please consider using the java.time.Instant (through instant) directly.

-

largest-min-value

(largest-min-value p)

Largest minimum value of this property

-

leap?

(leap? o)

True if the year of this entity is a leap year.

-

local-date

(local-date)(local-date arg0)(local-date arg0 arg1)(local-date arg0 arg1 arg2)

Creates a LocalDate. The following arguments are supported:

+

largest-min-value

(largest-min-value p)

Largest minimum value of this property

+

leap?

(leap? o)

True if the year of this entity is a leap year.

+

local-date

(local-date)(local-date arg0)(local-date arg0 arg1)(local-date arg0 arg1 arg2)

Creates a LocalDate. The following arguments are supported:

  • no arguments - current local-date
  • one argument @@ -192,7 +233,7 @@
-

local-date-time

(local-date-time)(local-date-time y m d h)(local-date-time y m d h mm)(local-date-time y m d h mm ss)(local-date-time y m d h mm ss n)(local-date-time arg0)(local-date-time arg0 arg1)(local-date-time arg0 arg1 arg2)

Creates a LocalDateTime. The following arguments are supported:

+

local-date-time

(local-date-time)(local-date-time y m d h)(local-date-time y m d h mm)(local-date-time y m d h mm ss)(local-date-time y m d h mm ss n)(local-date-time arg0)(local-date-time arg0 arg1)(local-date-time arg0 arg1 arg2)

Creates a LocalDateTime. The following arguments are supported:

  • no arguments - current local date-time
  • one argument @@ -215,9 +256,9 @@

    three and more arguments - year/month/day/…

-

local-date-time?

(local-date-time? v)

Returns true if v is an instance of java.time.LocalDateTime, otherwise false.

-

local-date?

(local-date? v)

Returns true if v is an instance of java.time.LocalDate, otherwise false.

-

local-time

(local-time)(local-time h m s nn)(local-time arg0)(local-time arg0 arg1)(local-time arg0 arg1 arg2)

Creates a LocalTime. The following arguments are supported:

+

local-date-time?

(local-date-time? v)

Returns true if v is an instance of java.time.LocalDateTime, otherwise false.

+

local-date?

(local-date? v)

Returns true if v is an instance of java.time.LocalDate, otherwise false.

+

local-time

(local-time)(local-time h m s nn)(local-time arg0)(local-time arg0 arg1)(local-time arg0 arg1 arg2)

Creates a LocalTime. The following arguments are supported:

  • no arguments - current local time
  • one argument @@ -237,19 +278,19 @@
  • three/four arguments - hour, minute, second, nanos
-

local-time?

(local-time? v)

Returns true if v is an instance of java.time.LocalTime, otherwise false.

-

max

(max o & os)

Latest/longest of the given time entities. Entities should be of the same type

-

max-value

(max-value p)

Maximum value of this property, e.g. 29th of February for months

-

micros

(micros micros)

Duration of a specified number of microseconds.

-

millis

(millis i)

Returns a Duration of i millis.

-

min

(min o & os)

Earliest/shortest of the given time entities. Entities should be of the same type

-

min-value

(min-value p)

Minimum value of this property

-

minus

(minus o & os)

Subtracts all of the os from the time entity o

+

local-time?

(local-time? v)

Returns true if v is an instance of java.time.LocalTime, otherwise false.

+

max

(max o & os)

Latest/longest of the given time entities. Entities should be of the same type

+

max-value

(max-value p)

Maximum value of this property, e.g. 29th of February for months

+

micros

(micros micros)

Duration of a specified number of microseconds.

+

millis

(millis i)

Returns a Duration of i millis.

+

min

(min o & os)

Earliest/shortest of the given time entities. Entities should be of the same type

+

min-value

(min-value p)

Minimum value of this property

+

minus

(minus o & os)

Subtracts all of the os from the time entity o

(j/minus (j/local-date 2015) (j/years 1))
 => <java.time.LocalDate "2014-01-01">
 
-

minutes

(minutes i)

Returns a Duration of i minutes.

-

mock-clock

(mock-clock)(mock-clock instant)(mock-clock instant zone)

Returns a mock implementation of the java.time.Clock. The mock supports advance-clock! operation which allows to move the time in the clock, e.g.:

+

minutes

(minutes i)

Returns a Duration of i minutes.

+

mock-clock

(mock-clock)(mock-clock instant)(mock-clock instant zone)

Returns a mock implementation of the java.time.Clock. The mock supports advance-clock! operation which allows to move the time in the clock, e.g.:

(let [clock (mock-clock 0 "UTC")]
   (with-clock clock
     (is (= (value clock) 0))
@@ -260,13 +301,13 @@
 

You can move the clock back via advancing by a negative temporal amount.

Creates a clock at epoch in the default time zone when called without arguments.

-

monday?

(monday? i)

Returns true if the given time entity with the day-of-week property falls on a Monday, otherwise false.

-

month

(month)(month v)(month fmt arg)

Returns the Month for the given month keyword name (e.g. :january), ordinal or entity. Current month if no arguments given.

-

month-day

(month-day)(month-day arg)(month-day a b)

Returns the MonthDay for the given entity, string, clock, zone or month/day combination. Current month-day if no arguments given.

-

month-day?

(month-day? o)

Returns true if o is java.time.MonthDay, otherwise false.

-

month?

(month? o)

True if java.time.Month.

-

months

(months i)

Returns a Period of i months.

-

move-end-by

(move-end-by i & os)

Moves the end instant of the interval by the sum of given periods/durations/numbers of milliseconds.

+

monday?

(monday? i)

Returns true if the given time entity with the day-of-week property falls on a Monday, otherwise false.

+

month

(month)(month v)(month fmt arg)

Returns the Month for the given month keyword name (e.g. :january), ordinal or entity. Current month if no arguments given.

+

month-day

(month-day)(month-day arg)(month-day a b)

Returns the MonthDay for the given entity, string, clock, zone or month/day combination. Current month-day if no arguments given.

+

month-day?

(month-day? o)

Returns true if o is java.time.MonthDay, otherwise false.

+

month?

(month? o)

True if java.time.Month.

+

months

(months i)

Returns a Period of i months.

+

move-end-by

(move-end-by i & os)

Moves the end instant of the interval by the sum of given periods/durations/numbers of milliseconds.

(move-start-by (interval 0 10000) (millis 1000) (seconds 1))
 => #<Interval ...:00Z/...:12Z>
 
@@ -274,7 +315,7 @@
(move-end-by (interval 0 10000) (millis -11000))
 => DateTimeException...
 
-

move-end-to

(move-end-to i new-end)

Moves the end of the interval to the given instant (or something convertible to an instant):

+

move-end-to

(move-end-to i new-end)

Moves the end of the interval to the given instant (or something convertible to an instant):

(move-end-to (interval 0 10000) (instant 15000))
 => #<Interval ...:00Z/...:15Z>
 
@@ -282,7 +323,7 @@
(move-end-to (interval 0 10000) (millis -1))
 => DateTimeException...
 
-

move-start-by

(move-start-by i & os)

Moves the start instant of the interval by the sum of given periods/durations/numbers of milliseconds:

+

move-start-by

(move-start-by i & os)

Moves the start instant of the interval by the sum of given periods/durations/numbers of milliseconds:

(move-start-by (interval 0 10000) (millis 1000) (seconds 1))
 => #<Interval ...:02Z/...:10Z>
 
@@ -290,7 +331,7 @@
(move-start-by (interval 0 10000) (millis 11000))
 ;=> DateTimeException...
 
-

move-start-to

(move-start-to i new-start)

Moves the start instant of the interval to the given instant (or something convertible to an instant):

+

move-start-to

(move-start-to i new-start)

Moves the start instant of the interval to the given instant (or something convertible to an instant):

(move-start-to (interval 0 10000) (instant 5000))
 => #<Interval ...:05Z/...:10Z>
 
@@ -298,15 +339,29 @@
(move-start-to (interval 0 10000) (millis 15000))
 => DateTimeException...
 
-

multiply-by

(multiply-by o v)

Entity o multiplied by the value v

-

nanos

(nanos i)

Returns a Duration of i nanos.

-

negate

(negate a)

Negates a temporal amount:

+

multiply-by

(multiply-by o v)

Entity o multiplied by the value v

+

nanos

(nanos i)

Returns a Duration of i nanos.

+

negate

(negate a)

Negates a temporal amount:

(negate (negate x)) == x

-

negative?

(negative? a)

True if the amount is negative

-

not-after?

(not-after? x)(not-after? x y)(not-after? x y & more)

Like before?, except returns true if the inputs are equal.

-

not-before?

(not-before? x)(not-before? x y)(not-before? x y & more)

Like after?, except returns true if the inputs are equal.

-

offset-clock

(offset-clock d)(offset-clock c d)

Creates a clock offset from the current/provided clock by a given duration.

-

offset-date-time

(offset-date-time)(offset-date-time y m d h)(offset-date-time y mo d h m)(offset-date-time y mo d h m s)(offset-date-time y mo d h m s n)(offset-date-time y mo d h m s n o)(offset-date-time arg0)(offset-date-time arg0 arg1)(offset-date-time arg0 arg1 arg2)

Creates an OffsetDateTime. The following arguments are supported:

+

negative?

(negative? a)

True if the amount is negative

+

not-after?

(not-after? x)(not-after? x y)(not-after? x y & more)

Returns true if time entities are ordered from the earliest to the latest (same semantics as <=), otherwise false.

+
(not-after? (local-date 2009) (local-date 2010) (local-date 2011))
+;=> true
+
+(not-after? (interval (instant 10000) (instant 1000000))
+            (instant 99999999))
+;=> true
+
+

not-before?

(not-before? x)(not-before? x y)(not-before? x y & more)

Returns true if time entities are ordered from the latest to the earliest (same semantics as >=), otherwise false.

+
(not-before? (local-date 2011) (local-date 2010) (local-date 2009))
+;=> true
+
+(not-before? (instant 99999999)
+             (interval (instant 10000) (instant 1000000)))
+;=> true
+
+

offset-clock

(offset-clock d)(offset-clock c d)

Creates a clock offset from the current/provided clock by a given duration.

+

offset-date-time

(offset-date-time)(offset-date-time y m d h)(offset-date-time y mo d h m)(offset-date-time y mo d h m s)(offset-date-time y mo d h m s n)(offset-date-time y mo d h m s n o)(offset-date-time arg0)(offset-date-time arg0 arg1)(offset-date-time arg0 arg1 arg2)

Creates an OffsetDateTime. The following arguments are supported:

  • no arguments - current date-time with the default offset
  • one argument @@ -336,8 +391,8 @@
  • eight arguments - time fields up to nanoseconds and a zone offset

If zone offset is not specified, default will be used. You can check the default offset by invoking (zone-offset).

-

offset-date-time?

(offset-date-time? v)

Returns true if v is an instance of java.time.OffsetDateTime, otherwise false.

-

offset-time

(offset-time)(offset-time h m s)(offset-time h m s n)(offset-time h m s n o)(offset-time arg0)(offset-time arg0 arg1)

Creates an OffsetTime. The following arguments are supported:

+

offset-date-time?

(offset-date-time? v)

Returns true if v is an instance of java.time.OffsetDateTime, otherwise false.

+

offset-time

(offset-time)(offset-time h m s)(offset-time h m s n)(offset-time h m s n o)(offset-time arg0)(offset-time arg0 arg1)

Creates an OffsetTime. The following arguments are supported:

  • no arguments - current time with the default offset
  • one argument @@ -362,10 +417,10 @@
  • five arguments - last is the offset

If zone offset is not specified, default will be used. You can check the default offset by invoking (zone-offset).

-

offset-time?

(offset-time? v)

Returns true if v is an instance of java.time.OffsetTime, otherwise false.

-

overlap

(overlap i oi)

Gets the overlap between this interval and the other one or nil

-

overlaps?

(overlaps? i oi)

True if this interval overlaps the other one

-

period

(period)(period arg0)(period arg0 arg1)(period arg0 arg1 arg2)

Creates a period - a temporal entity consisting of years, months and days.

+

offset-time?

(offset-time? v)

Returns true if v is an instance of java.time.OffsetTime, otherwise false.

+

overlap

(overlap i oi)

Gets the overlap between this interval and the other one or nil

+

overlaps?

(overlaps? i oi)

True if this interval overlaps the other one

+

period

(period)(period arg0)(period arg0 arg1)(period arg0 arg1 arg2)

Creates a period - a temporal entity consisting of years, months and days.

Given one argument will

  • interpret as years if a number
  • @@ -380,98 +435,98 @@
  • get a period of a specified number of years and months

Given three arguments will create a year/month/day period.

-

period?

(period? v)

Returns true if v is an instance of java.time.Period, otherwise false.

-

plus

(plus o & os)

Adds all of the os to the time entity o. plus is not commutative, the first argument is always the entity which will accumulate the rest of the arguments.

+

period?

(period? v)

Returns true if v is an instance of java.time.Period, otherwise false.

+

plus

(plus o & os)

Adds all of the os to the time entity o. plus is not commutative, the first argument is always the entity which will accumulate the rest of the arguments.

(j/plus (j/local-date 2015) (j/years 1))
 => <java.time.LocalDate "2016-01-01">
 
-

properties

(properties o)

Map of properties present in this temporal entity

-

property

(property o k)

Property of this temporal entity under key k

-

quarter

(quarter)(quarter v)(quarter fmt arg)

Returns the Quarter for the given quarter keyword name (e.g. :q1), ordinal or entity. Current quarter if no arguments given.

-

quarter?

(quarter? o)

True if org.threeten.extra.Quarter.

-

range

(range p)

Range of values for this property

-

saturday?

(saturday? i)

Returns true if the given time entity with the day-of-week property falls on a Saturday, otherwise false.

-

seconds

(seconds i)

Returns a Duration of i seconds.

-

set-clock!

(set-clock! clock time)

Sets the clock to the given time.

+

properties

(properties o)

Map of properties present in this temporal entity

+

property

(property o k)

Property of this temporal entity under key k

+

quarter

(quarter)(quarter v)(quarter fmt arg)

Returns the Quarter for the given quarter keyword name (e.g. :q1), ordinal or entity. Current quarter if no arguments given.

+

quarter?

(quarter? o)

True if org.threeten.extra.Quarter.

+

range

(range p)

Range of values for this property

+

saturday?

(saturday? i)

Returns true if the given time entity with the day-of-week property falls on a Saturday, otherwise false.

+

seconds

(seconds i)

Returns a Duration of i seconds.

+

set-clock!

(set-clock! clock time)

Sets the clock to the given time.

This mutates the mock clock.

-

smallest-max-value

(smallest-max-value p)

Smallest maximum value of this property, e.g. 28th of February for months

-

sql-date

(sql-date)(sql-date arg0)(sql-date arg0 arg1)(sql-date arg0 arg1 arg2)

Creates a java.sql.Date out of any combination of arguments valid for local-date or the LocalDate itself.

+

smallest-max-value

(smallest-max-value p)

Smallest maximum value of this property, e.g. 28th of February for months

+

sql-date

(sql-date)(sql-date arg0)(sql-date arg0 arg1)(sql-date arg0 arg1 arg2)

Creates a java.sql.Date out of any combination of arguments valid for local-date or the LocalDate itself.

Please consider using the JSR-310 Java Time types instead of java.sql.Date if your drivers support them.

Even though java.sql.Date extends a java.util.Date, it’s supposed to be used as a local date (no time component or time zone) for the purposes of conversion from/to native JDBC driver DATE types.

-

sql-time

(sql-time)(sql-time arg0)(sql-time arg0 arg1)(sql-time arg0 arg1 arg2)

Creates a java.sql.Time out of any combination of arguments valid for local-time (except the nanos constructor) or the LocalTime itself.

+

sql-time

(sql-time)(sql-time arg0)(sql-time arg0 arg1)(sql-time arg0 arg1 arg2)

Creates a java.sql.Time out of any combination of arguments valid for local-time (except the nanos constructor) or the LocalTime itself.

Please consider using the JSR-310 Java Time types instead of java.sql.Time if your drivers support them.

Even though java.sql.Time extends a java.util.Date, it’s supposed to be used as a local time (no date component or time zone) for the purposes of conversion from/to native JDBC driver TIME types.

-

sql-timestamp

(sql-timestamp)(sql-timestamp arg0)(sql-timestamp arg0 arg1)(sql-timestamp arg0 arg1 arg2)(sql-timestamp arg0 arg1 arg2 arg3)(sql-timestamp arg0 arg1 arg2 arg3 arg4)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5 arg6)

Creates a java.sql.Timestamp in the local time zone out of any combination of arguments valid for local-date-time or the LocalDateTime itself.

+

sql-timestamp

(sql-timestamp)(sql-timestamp arg0)(sql-timestamp arg0 arg1)(sql-timestamp arg0 arg1 arg2)(sql-timestamp arg0 arg1 arg2 arg3)(sql-timestamp arg0 arg1 arg2 arg3 arg4)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5)(sql-timestamp arg0 arg1 arg2 arg3 arg4 arg5 arg6)

Creates a java.sql.Timestamp in the local time zone out of any combination of arguments valid for local-date-time or the LocalDateTime itself.

Does not support Timestamp construction from an Instant or a long millis value—please use instant->sql-timestamp for this purpose.

Please consider using the JSR-310 Java Time types instead of java.sql.Timestamp if your drivers support them.

java.sql.Timestamp is a version of a java.util.Date supposed to be used as a local date-time (no time zone) for the purposes of conversion from/to native JDBC driver TIMESTAMP types.

-

standard-days

(standard-days i)

Returns a Duration of i days.

-

start

(start i)

Gets the start instant of the interval

-

sunday?

(sunday? i)

Returns true if the given time entity with the day-of-week property falls on a Sunday, otherwise false.

-

supports?

(supports? o p)

True if the o entity supports the p property

-

system-clock

(system-clock)(system-clock k)

Creates a system clock. In the default time zone if called without arguments, otherwise accepts a Zone Id.

-

thursday?

(thursday? i)

Returns true if the given time entity with the day-of-week property falls on a Thursday, otherwise false.

-

tick-clock

(tick-clock d)(tick-clock c d)

Creates a clock wrapping system/provided clock that only ticks as per specified duration.

-

time-between

(time-between o e u)

Time between temporal entities o and e in unit u.

+

standard-days

(standard-days i)

Returns a Duration of i days.

+

start

(start i)

Gets the start instant of the interval

+

sunday?

(sunday? i)

Returns true if the given time entity with the day-of-week property falls on a Sunday, otherwise false.

+

supports?

(supports? o p)

True if the o entity supports the p property

+

system-clock

(system-clock)(system-clock k)

Creates a system clock. In the default time zone if called without arguments, otherwise accepts a Zone Id.

+

thursday?

(thursday? i)

Returns true if the given time entity with the day-of-week property falls on a Thursday, otherwise false.

+

tick-clock

(tick-clock d)(tick-clock c d)

Creates a clock wrapping system/provided clock that only ticks as per specified duration.

+

time-between

(time-between o e u)

Time between temporal entities o and e in unit u.

(j/time-between (j/local-date 2015) (j/local-date 2016) :days)
 => 365
 
 (j/time-between :days (j/local-date 2015) (j/local-date 2016))
 => 365
 
-

to-java-date

deprecated

(to-java-date o)

Converts a date entity to a java.util.Date.

+

to-java-date

deprecated

(to-java-date o)

Converts a date entity to a java.util.Date.

Deprecated: This function only has a single arity and works for entities directly convertible to java.time.Instant. Please consider using java-date instead.

-

to-millis-from-epoch

(to-millis-from-epoch o)

Converts a date entity to a long representing the number of milliseconds from epoch.

-

to-sql-date

deprecated

(to-sql-date o)

Converts a local date entity to a java.sql.Date.

+

to-millis-from-epoch

(to-millis-from-epoch o)

Converts a date entity to a long representing the number of milliseconds from epoch.

+

to-sql-date

deprecated

(to-sql-date o)

Converts a local date entity to a java.sql.Date.

Deprecated: This function only has a single arity and works for entities directly convertible to java.time.LocalDate. Please consider using sql-date instead.

-

to-sql-timestamp

deprecated

(to-sql-timestamp o)

Converts a date entity to a java.sql.Timestamp.

+

to-sql-timestamp

deprecated

(to-sql-timestamp o)

Converts a date entity to a java.sql.Timestamp.

Deprecated: This function only has a single arity and works for entities directly convertible to java.time.Instant. Please consider using sql-timestamp instead.

-

truncate-to

(truncate-to o u)

Truncates this entity to the specified time unit. Only works for units that divide into the length of standard day without remainder (up to :days).

-

tuesday?

(tuesday? i)

Returns true if the given time entity with the day-of-week property falls on a Tuesday, otherwise false.

-

unit

(unit k)(unit entity k)

Returns a TemporalUnit for the given key k or extracts the field from the given temporal entity.

+

truncate-to

(truncate-to o u)

Truncates this entity to the specified time unit. Only works for units that divide into the length of standard day without remainder (up to :days).

+

tuesday?

(tuesday? i)

Returns true if the given time entity with the day-of-week property falls on a Tuesday, otherwise false.

+

unit

(unit k)(unit entity k)

Returns a TemporalUnit for the given key k or extracts the field from the given temporal entity.

You can see predefined units via java-time.repl/show-units.

If you want to make your own custom TemporalUnits resolvable, you need to rebind the java-time.properties/*units* to a custom java_time.properties.UnitGroup.

-

unit?

(unit? o)

True if this is a TemporalUnit.

-

units

(units o)

Units present in this temporal entity.

-

value

(value p)

Value of the property

-

value-range

(value-range min max)(value-range arg0)

Creates a ValueRange given the min and max amounts or a map of :min-smallest, :max-smallest, :min-largest and :max-largest.

-

wednesday?

(wednesday? i)

Returns true if the given time entity with the day-of-week property falls on a Wednesday, otherwise false.

-

weekday?

(weekday? dt)

Complement of weekend?.

-

weekend?

(weekend? dt)

Returns true if argument is saturday? or sunday?, otherwise false.

-

weeks

(weeks i)

Returns a Period of i weeks.

-

when-joda-time-loaded

macro

(when-joda-time-loaded & body)

Execute the body when Joda-Time classes are found on the classpath.

+

unit?

(unit? o)

True if this is a TemporalUnit.

+

units

(units o)

Units present in this temporal entity.

+

value

(value p)

Value of the property

+

value-range

(value-range min max)(value-range arg0)

Creates a ValueRange given the min and max amounts or a map of :min-smallest, :max-smallest, :min-largest and :max-largest.

+

wednesday?

(wednesday? i)

Returns true if the given time entity with the day-of-week property falls on a Wednesday, otherwise false.

+

weekday?

(weekday? dt)

Complement of weekend?.

+

weekend?

(weekend? dt)

Returns true if argument is saturday? or sunday?, otherwise false.

+

weeks

(weeks i)

Returns a Period of i weeks.

+

when-joda-time-loaded

macro

(when-joda-time-loaded & body)

Execute the body when Joda-Time classes are found on the classpath.

Take care - when AOT-compiling code using this macro, the Joda-Time classes must be on the classpath at compile time!

-

with-clock

macro

(with-clock c & forms)

Executes the given forms in the scope of the provided clock.

+

with-clock

macro

(with-clock c & forms)

Executes the given forms in the scope of the provided clock.

All the temporal entities that get created without parameters will inherit their values from the clock:

(with-clock (system-clock “Europe/London”) (zone-id)) => #<java.time.ZoneRegion Europe/London>

-

with-clock-fn

(with-clock-fn c f)

Executes the given function in the scope of the provided clock. All the temporal entities that get created without parameters will inherit their values from the clock.

-

with-largest-min-value

(with-largest-min-value p)

Underlying temporal entity with the value set to the largest minimum available for this property

-

with-max-value

(with-max-value p)

Underlying temporal entity with the value set to the maximum available for this property

-

with-min-value

(with-min-value p)

Underlying temporal entity with the value set to the minimum available for this property

-

with-offset

(with-offset o offset)

Sets the offset to the specified value ensuring that the local time stays the same.

+

with-clock-fn

(with-clock-fn c f)

Executes the given function in the scope of the provided clock. All the temporal entities that get created without parameters will inherit their values from the clock.

+

with-largest-min-value

(with-largest-min-value p)

Underlying temporal entity with the value set to the largest minimum available for this property

+

with-max-value

(with-max-value p)

Underlying temporal entity with the value set to the maximum available for this property

+

with-min-value

(with-min-value p)

Underlying temporal entity with the value set to the minimum available for this property

+

with-offset

(with-offset o offset)

Sets the offset to the specified value ensuring that the local time stays the same.

(offset-time 10 30 0 0 +2) => #<java.time.OffsetTime 10:30+02:00> (with-offset *1 +3) => #<java.time.OffsetTime 10:30+03:00>

-

with-offset-same-instant

(with-offset-same-instant o offset)

Sets the offset to the specified value ensuring that the result has the same instant, e.g.:

+

with-offset-same-instant

(with-offset-same-instant o offset)

Sets the offset to the specified value ensuring that the result has the same instant, e.g.:

(offset-time 10 30 0 0 +2) => #<java.time.OffsetTime 10:30+02:00> (with-offset-same-instant *1 +3) => #<java.time.OffsetTime 11:30+03:00>

-

with-smallest-max-value

(with-smallest-max-value p)

Underlying temporal entity with the value set to the smallest maximum available for this property

-

with-value

(with-value p v)

Underlying temporal entity with the value of this property set to v

-

with-zone

(with-zone o z)

Returns this temporal entity with the specified ZoneId

-

with-zone-same-instant

(with-zone-same-instant zdt z)

Sets the zone to the specified value ensuring that the result has the same instant, e.g.:

+

with-smallest-max-value

(with-smallest-max-value p)

Underlying temporal entity with the value set to the smallest maximum available for this property

+

with-value

(with-value p v)

Underlying temporal entity with the value of this property set to v

+

with-zone

(with-zone o z)

Returns this temporal entity with the specified ZoneId

+

with-zone-same-instant

(with-zone-same-instant zdt z)

Sets the zone to the specified value ensuring that the result has the same instant, e.g.:

(zoned-date-time 2015) => #<java.time.ZonedDateTime 2015-01-01T00:00+00:00Europe/London> (with-zone-same-instant *1 “America/New_York”) => #<java.time.ZonedDateTime 2014-12-31T18:00-05:00America/New_York>

-

year

(year)(year arg)(year fmt arg)

Returns the Year for the given entity, string, clock, zone or number. Current year if no arguments given.

-

year-month

(year-month)(year-month arg)(year-month a b)

Returns the YearMonth for the given entity, string, clock, zone or month/day combination. Current year-month if no arguments given.

-

year-month?

(year-month? o)

Returns true if o is java.time.YearMonth, otherwise false.

-

year-quarter

(year-quarter)(year-quarter arg)(year-quarter a b)

Returns the YearQuarter for the given entity, clock, zone or year with quarter. Current year quarter if no arguments given.

-

year-quarter?

(year-quarter? o)

Returns true if o is org.threeten.extra.YearQuarter, otherwise false.

-

year?

(year? o)

Returns true if o is java.time.Year, otherwise false.

-

years

(years i)

Returns a Period of i years.

-

zero?

(zero? a)

True if the amount is zero

-

zone-id

(zone-id)(zone-id arg0)(zone-id arg0 arg1)

Creates a ZoneId from a string identifier, java.util.TimeZone or extracts from another temporal entity.

+

year

(year)(year arg)(year fmt arg)

Returns the Year for the given entity, string, clock, zone or number. Current year if no arguments given.

+

year-month

(year-month)(year-month arg)(year-month a b)

Returns the YearMonth for the given entity, string, clock, zone or month/day combination. Current year-month if no arguments given.

+

year-month?

(year-month? o)

Returns true if o is java.time.YearMonth, otherwise false.

+

year-quarter

(year-quarter)(year-quarter arg)(year-quarter a b)

Returns the YearQuarter for the given entity, clock, zone or year with quarter. Current year quarter if no arguments given.

+

year-quarter?

(year-quarter? o)

Returns true if o is org.threeten.extra.YearQuarter, otherwise false.

+

year?

(year? o)

Returns true if o is java.time.Year, otherwise false.

+

years

(years i)

Returns a Period of i years.

+

zero?

(zero? a)

True if the amount is zero

+

zone-id

(zone-id)(zone-id arg0)(zone-id arg0 arg1)

Creates a ZoneId from a string identifier, java.util.TimeZone or extracts from another temporal entity.

Returns default system zone id if no arguments provided.

Given two arguments will use the second as the offset.

-

zone-id?

(zone-id? v)

Returns true if v is an instance of java.time.ZoneId, otherwise false.

-

zone-offset

(zone-offset)(zone-offset o)(zone-offset h m)(zone-offset h m s)

Creates a ZoneOffset from a string identifier (e.g. “+01”), a number of hours/hours and minutes/hours, minutes and seconds or extracts from another temporal entity.

+

zone-id?

(zone-id? v)

Returns true if v is an instance of java.time.ZoneId, otherwise false.

+

zone-offset

(zone-offset)(zone-offset o)(zone-offset h m)(zone-offset h m s)

Creates a ZoneOffset from a string identifier (e.g. “+01”), a number of hours/hours and minutes/hours, minutes and seconds or extracts from another temporal entity.

Returns default system zone offset if no arguments provided.

-

zoned-date-time

(zoned-date-time)(zoned-date-time y m d h)(zoned-date-time y mo d h m)(zoned-date-time y mo d h m s)(zoned-date-time y mo d h m s n)(zoned-date-time y mo d h m s n o)(zoned-date-time arg0)(zoned-date-time arg0 arg1)(zoned-date-time arg0 arg1 arg2)

Creates a ZonedDateTime. The following arguments are supported:

+

zoned-date-time

(zoned-date-time)(zoned-date-time y m d h)(zoned-date-time y mo d h m)(zoned-date-time y mo d h m s)(zoned-date-time y mo d h m s n)(zoned-date-time y mo d h m s n o)(zoned-date-time arg0)(zoned-date-time arg0 arg1)(zoned-date-time arg0 arg1 arg2)

Creates a ZonedDateTime. The following arguments are supported:

  • no arguments - current date-time in the default zone
  • one argument @@ -500,5 +555,5 @@
  • eight arguments - last is the zone id

If zone id is not specified, default zone id will be used. You can check the default zone by invoking (zone-id).

-

zoned-date-time?

(zoned-date-time? v)

Returns true if v is an instance of java.time.ZonedDateTime, otherwise false.

-
\ No newline at end of file +

zoned-date-time?

(zoned-date-time? v)

Returns true if v is an instance of java.time.ZonedDateTime, otherwise false.

+
\ No newline at end of file diff --git a/docs/java-time.repl.html b/docs/java-time.repl.html index ec881f9..d80c77c 100644 --- a/docs/java-time.repl.html +++ b/docs/java-time.repl.html @@ -1,3 +1,3 @@ -java-time.repl documentation

java-time.repl

show-adjusters

(show-adjusters)

show-fields

(show-fields)

show-formatters

(show-formatters)

show-graph

(show-graph)

show-path

(show-path from to)

show-timezones

(show-timezones)

show-units

(show-units)
\ No newline at end of file +java-time.repl documentation

java-time.repl

show-adjusters

(show-adjusters)

show-fields

(show-fields)

show-formatters

(show-formatters)

show-graph

(show-graph)

show-path

(show-path from to)

show-timezones

(show-timezones)

show-units

(show-units)
\ No newline at end of file diff --git a/project.clj b/project.clj index 66aaa2c..e7f3fce 100644 --- a/project.clj +++ b/project.clj @@ -2,6 +2,7 @@ (def clojure-versions ["1.8" "1.9" "1.10" "1.11" "1.12"]) (def threeten-extra-version "1.4") (def joda-time-version "2.10.1") +(def math-combinatorics-version "0.2.0") (defproject clojure.java-time "1.4.0-SNAPSHOT" :description "Clojure wrapper for Java 8 Time API" :url "http://github.com/dm3/clojure.java-time" @@ -29,13 +30,15 @@ [com.taoensso/tufte "2.2.0"] [org.clojure/tools.namespace "1.3.0"] [joda-time/joda-time ~joda-time-version] - [org.threeten/threeten-extra ~threeten-extra-version]] + [org.threeten/threeten-extra ~threeten-extra-version] + [org.clojure/math.combinatorics ~math-combinatorics-version]] :source-paths ["dev"] :global-vars {*warn-on-reflection* true} :eastwood {:exclude-namespaces [java-time java-time.api ;;FIXME - java-time.api-test] + java-time.api-test + java-time.test-utils] :exclude-linters []}} ;; lein doc :codox {:injections [(require 'java-time) @@ -49,6 +52,7 @@ :async-profiler {:jvm-opts ["-Djdk.attach.allowAttachSelf" "-XX:+UnlockDiagnosticVMOptions" "-XX:+DebugNonSafepoints"] :dependencies [[com.clojure-goes-fast/clj-async-profiler "0.3.1"]]} + :test {:dependencies [[org.clojure/math.combinatorics ~math-combinatorics-version]]} :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]} :1.8-three-ten-joda {:dependencies [[org.clojure/clojure "1.8.0"] [org.threeten/threeten-extra ~threeten-extra-version] @@ -72,7 +76,7 @@ [org.threeten/threeten-extra ~threeten-extra-version] [joda-time/joda-time ~joda-time-version]] :repositories [["sonatype-oss-public" {:url "https://oss.sonatype.org/content/groups/public"}]]}} - :aliases {"all" ["with-profile" ~(str/join ":" (mapcat (juxt identity #(str % "-three-ten-joda")) clojure-versions))] + :aliases {"all" ["with-profile" ~(str "test," (str/join ":" (mapcat (juxt identity #(str % "-three-ten-joda")) clojure-versions)))] "warm-deps" ["all" "deps"] "doc" ["do" ["with-profile" "-user,+codox" "codox"] diff --git a/src/java_time.clj b/src/java_time.clj index 11d6afc..938d474 100644 --- a/src/java_time.clj +++ b/src/java_time.clj @@ -1,5 +1,5 @@ ;; NOTE: This namespace is generated by java-time.dev.gen -(ns ^{:deprecated "1.1.0", :doc "This namespace has been deprecated due to [#91](https://github.com/dm3/clojure.java-time/issues/91).\n Please migrate to [[java-time.api]]\n\n This namespace will continue to exist and be updated. For\n maximum JVM compatibility, please migrate to `java-time.api`,\n which provides the same interface. `java-time` and `java-time.api`\n and can be freely intermixed within the same library, so you can\n safely migrate your own code even if your dependencies use the old namespace.\n \n Migration steps:\n\n 1. rename all references from `java-time` to [[java-time.api]].\n eg., `(:require [java-time :as jt])` => `(:require [java-time.api :as jt])`", :superseded-by "java-time.api"} java-time (:refer-clojure :exclude (zero? range iterate max min contains? format abs)) (:require [java-time core properties temporal amount zone single-field local chrono convert sugar seqs adjuster interval format joda clock pre-java8 mock])) +(ns ^{:deprecated "1.1.0", :doc "This namespace has been deprecated due to [#91](https://github.com/dm3/clojure.java-time/issues/91).\n Please migrate to [[java-time.api]]\n\n This namespace will continue to exist and be updated. For\n maximum JVM compatibility, please migrate to `java-time.api`,\n which provides the same interface. `java-time` and `java-time.api`\n and can be freely intermixed within the same library, so you can\n safely migrate your own code even if your dependencies use the old namespace.\n \n Migration steps:\n\n 1. rename all references from `java-time` to [[java-time.api]].\n eg., `(:require [java-time :as jt])` => `(:require [java-time.api :as jt])`", :superseded-by "java-time.api"} java-time (:refer-clojure :exclude (zero? range iterate max min contains? format abs < > <= >= * - + neg?)) (:require [java-time core properties temporal amount zone single-field local chrono convert sugar seqs adjuster interval format joda clock pre-java8 mock])) (defmacro with-clock {:doc "Executes the given `forms` in the scope of the provided `clock`.\n\n All the temporal entities that get created without parameters will inherit\n their values from the clock:\n\n (with-clock (system-clock \"Europe/London\")\n (zone-id))\n => #"} [c & forms] (list* (quote java-time.clock/with-clock) c forms)) (defmacro when-joda-time-loaded {:doc "Execute the `body` when Joda-Time classes are found on the classpath.\n\n Take care - when AOT-compiling code using this macro, the Joda-Time classes\n must be on the classpath at compile time!"} [& body] (list* (quote java-time.util/when-joda-time-loaded) body)) (def ^{:arglists (quote ([^Clock c f])), :doc "Executes the given function in the scope of the provided clock. All the\n temporal entities that get created without parameters will inherit their\n values from the clock."} with-clock-fn java-time.clock/with-clock-fn) @@ -10,9 +10,9 @@ (def ^{:arglists (quote ([o & os])), :doc "Latest/longest of the given time entities. Entities should be of the same\n type"} max java-time.core/max) (def ^{:arglists (quote ([o & os])), :doc "Earliest/shortest of the given time entities. Entities should be of the same\n type"} min java-time.core/min) (def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<`), otherwise `false`.\n\n ```\n (before? (local-date 2009) (local-date 2010) (local-date 2011))\n => true\n\n (before? (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n => true\n ```"} before? java-time.core/before?) -(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Like [[before?]], except returns `true` if the inputs are equal."} not-after? java-time.core/not-after?) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<=`), otherwise `false`.\n\n ```\n (not-after? (local-date 2009) (local-date 2010) (local-date 2011))\n ;=> true\n\n (not-after? (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n ;=> true\n ```"} not-after? java-time.core/not-after?) (def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>`), otherwise `false`.\n\n ```\n (after? (local-date 2011) (local-date 2010) (local-date 2009))\n => true\n\n (after? (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n => true\n ```"} after? java-time.core/after?) -(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Like [[after?]], except returns `true` if the inputs are equal."} not-before? java-time.core/not-before?) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>=`), otherwise `false`.\n\n ```\n (not-before? (local-date 2011) (local-date 2010) (local-date 2009))\n ;=> true\n\n (not-before? (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n ;=> true\n ```"} not-before? java-time.core/not-before?) (defn ^{:doc "True if the `o` entity supports the `p` property"} supports? ([o p] (java-time.core/supports? o p))) (defn ^{:doc "Fields present in this temporal entity"} fields ([o] (java-time.core/fields o))) (defn ^{:doc "Units present in this temporal entity."} units ([o] (java-time.core/units o))) @@ -31,6 +31,13 @@ (def ^{:arglists (quote ([o & os])), :doc "Adds all of the `os` to the time entity `o`. `plus` is not commutative, the\n first argument is always the entity which will accumulate the rest of the\n arguments.\n\n ```\n (j/plus (j/local-date 2015) (j/years 1))\n => \n ```"} plus java-time.core/plus) (def ^{:arglists (quote ([o & os])), :doc "Subtracts all of the `os` from the time entity `o`\n\n ```\n (j/minus (j/local-date 2015) (j/years 1))\n => \n ```"} minus java-time.core/minus) (defn ^{:doc "Entity `o` multiplied by the value `v`"} multiply-by ([o v] (java-time.core/multiply-by o v))) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<`), otherwise `false`.\n\n ```\n (j/< (local-date 2009) (local-date 2010) (local-date 2011))\n => true\n\n (j/< (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n => true\n ```"} < java-time.core/<) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>`), otherwise `false`.\n\n ```\n (j/> (local-date 2011) (local-date 2010) (local-date 2009))\n => true\n\n (j/> (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n => true\n ```"} > java-time.core/>) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<=`), otherwise `false`.\n\n ```\n (j/<= (local-date 2009) (local-date 2010) (local-date 2011))\n ;=> true\n\n (j/<= (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n ;=> true\n ```"} <= java-time.core/<=) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>=`), otherwise `false`.\n\n ```\n (j/>= (local-date 2011) (local-date 2010) (local-date 2009))\n ;=> true\n\n (j/>= (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n ;=> true\n ```"} >= java-time.core/>=) +(def ^{:arglists (quote ([o v])), :doc "Entity `o` multiplied by the value `v`"} * java-time.core/*) +(def ^{:arglists (quote ([o & os])), :doc "Subtracts all of the `os` from the time entity `o`\n\n ```\n (j/- (j/local-date 2015) (j/years 1))\n => \n ```"} - java-time.core/-) +(def ^{:arglists (quote ([o & os])), :doc "Adds all of the `os` to the time entity `o`. `+` is not commutative, the\n first argument is always the entity which will accumulate the rest of the\n arguments.\n\n ```\n (j/+ (j/local-date 2015) (j/years 1))\n => \n ```"} + java-time.core/+) (defn ^{:doc "The `Chronology` of the entity", :tag java.time.chrono.Chronology} chronology ([o] (java-time.core/chronology o))) (defn ^{:doc "True if the year of this entity is a leap year."} leap? ([o] (java-time.core/leap? o))) (defn ^{:doc "Underlying temporal entity with the value of this property set to `v`"} with-value ([p v] (java-time.core/with-value p v))) diff --git a/src/java_time/api.clj b/src/java_time/api.clj index 6e7cd67..bd71e38 100644 --- a/src/java_time/api.clj +++ b/src/java_time/api.clj @@ -1,5 +1,5 @@ ;; NOTE: This namespace is generated by java-time.dev.gen -(ns ^{:supercedes "java-time"} java-time.api (:refer-clojure :exclude (zero? range iterate max min contains? format abs)) (:require [java-time core properties temporal amount zone single-field local chrono convert sugar seqs adjuster interval format joda clock pre-java8 mock])) +(ns ^{:supercedes "java-time"} java-time.api (:refer-clojure :exclude (zero? range iterate max min contains? format abs < > <= >= * - + neg?)) (:require [java-time core properties temporal amount zone single-field local chrono convert sugar seqs adjuster interval format joda clock pre-java8 mock])) (defmacro with-clock {:doc "Executes the given `forms` in the scope of the provided `clock`.\n\n All the temporal entities that get created without parameters will inherit\n their values from the clock:\n\n (with-clock (system-clock \"Europe/London\")\n (zone-id))\n => #"} [c & forms] (list* (quote java-time.clock/with-clock) c forms)) (defmacro when-joda-time-loaded {:doc "Execute the `body` when Joda-Time classes are found on the classpath.\n\n Take care - when AOT-compiling code using this macro, the Joda-Time classes\n must be on the classpath at compile time!"} [& body] (list* (quote java-time.util/when-joda-time-loaded) body)) (def ^{:arglists (quote ([^Clock c f])), :doc "Executes the given function in the scope of the provided clock. All the\n temporal entities that get created without parameters will inherit their\n values from the clock."} with-clock-fn java-time.clock/with-clock-fn) @@ -10,9 +10,9 @@ (def ^{:arglists (quote ([o & os])), :doc "Latest/longest of the given time entities. Entities should be of the same\n type"} max java-time.core/max) (def ^{:arglists (quote ([o & os])), :doc "Earliest/shortest of the given time entities. Entities should be of the same\n type"} min java-time.core/min) (def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<`), otherwise `false`.\n\n ```\n (before? (local-date 2009) (local-date 2010) (local-date 2011))\n => true\n\n (before? (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n => true\n ```"} before? java-time.core/before?) -(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Like [[before?]], except returns `true` if the inputs are equal."} not-after? java-time.core/not-after?) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<=`), otherwise `false`.\n\n ```\n (not-after? (local-date 2009) (local-date 2010) (local-date 2011))\n ;=> true\n\n (not-after? (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n ;=> true\n ```"} not-after? java-time.core/not-after?) (def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>`), otherwise `false`.\n\n ```\n (after? (local-date 2011) (local-date 2010) (local-date 2009))\n => true\n\n (after? (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n => true\n ```"} after? java-time.core/after?) -(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Like [[after?]], except returns `true` if the inputs are equal."} not-before? java-time.core/not-before?) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>=`), otherwise `false`.\n\n ```\n (not-before? (local-date 2011) (local-date 2010) (local-date 2009))\n ;=> true\n\n (not-before? (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n ;=> true\n ```"} not-before? java-time.core/not-before?) (defn ^{:doc "True if the `o` entity supports the `p` property"} supports? ([o p] (java-time.core/supports? o p))) (defn ^{:doc "Fields present in this temporal entity"} fields ([o] (java-time.core/fields o))) (defn ^{:doc "Units present in this temporal entity."} units ([o] (java-time.core/units o))) @@ -31,6 +31,13 @@ (def ^{:arglists (quote ([o & os])), :doc "Adds all of the `os` to the time entity `o`. `plus` is not commutative, the\n first argument is always the entity which will accumulate the rest of the\n arguments.\n\n ```\n (j/plus (j/local-date 2015) (j/years 1))\n => \n ```"} plus java-time.core/plus) (def ^{:arglists (quote ([o & os])), :doc "Subtracts all of the `os` from the time entity `o`\n\n ```\n (j/minus (j/local-date 2015) (j/years 1))\n => \n ```"} minus java-time.core/minus) (defn ^{:doc "Entity `o` multiplied by the value `v`"} multiply-by ([o v] (java-time.core/multiply-by o v))) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<`), otherwise `false`.\n\n ```\n (j/< (local-date 2009) (local-date 2010) (local-date 2011))\n => true\n\n (j/< (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n => true\n ```"} < java-time.core/<) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>`), otherwise `false`.\n\n ```\n (j/> (local-date 2011) (local-date 2010) (local-date 2009))\n => true\n\n (j/> (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n => true\n ```"} > java-time.core/>) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the earliest to the\n latest (same semantics as `<=`), otherwise `false`.\n\n ```\n (j/<= (local-date 2009) (local-date 2010) (local-date 2011))\n ;=> true\n\n (j/<= (interval (instant 10000) (instant 1000000))\n (instant 99999999))\n ;=> true\n ```"} <= java-time.core/<=) +(def ^{:arglists (quote ([x] [x y] [x y & more])), :doc "Returns `true` if time entities are ordered from the latest to the\n earliest (same semantics as `>=`), otherwise `false`.\n\n ```\n (j/>= (local-date 2011) (local-date 2010) (local-date 2009))\n ;=> true\n\n (j/>= (instant 99999999)\n (interval (instant 10000) (instant 1000000)))\n ;=> true\n ```"} >= java-time.core/>=) +(def ^{:arglists (quote ([o v])), :doc "Entity `o` multiplied by the value `v`"} * java-time.core/*) +(def ^{:arglists (quote ([o & os])), :doc "Subtracts all of the `os` from the time entity `o`\n\n ```\n (j/- (j/local-date 2015) (j/years 1))\n => \n ```"} - java-time.core/-) +(def ^{:arglists (quote ([o & os])), :doc "Adds all of the `os` to the time entity `o`. `+` is not commutative, the\n first argument is always the entity which will accumulate the rest of the\n arguments.\n\n ```\n (j/+ (j/local-date 2015) (j/years 1))\n => \n ```"} + java-time.core/+) (defn ^{:doc "The `Chronology` of the entity", :tag java.time.chrono.Chronology} chronology ([o] (java-time.core/chronology o))) (defn ^{:doc "True if the year of this entity is a leap year."} leap? ([o] (java-time.core/leap? o))) (defn ^{:doc "Underlying temporal entity with the value of this property set to `v`"} with-value ([p v] (java-time.core/with-value p v))) diff --git a/src/java_time/core.clj b/src/java_time/core.clj index b1af748..963f392 100644 --- a/src/java_time/core.clj +++ b/src/java_time/core.clj @@ -1,5 +1,6 @@ (ns java-time.core - (:refer-clojure :exclude (zero? range max min abs)) + (:refer-clojure :exclude (zero? range max min abs < > <= >= * - + neg?)) + (:require [clojure.core :as cc]) (:import [java.time.temporal ValueRange] [java.time.chrono Chronology])) @@ -141,19 +142,7 @@ ([o k1 k2] [(as* o k1) (as* o k2)]) ([o k1 k2 & ks] - (concat (as o k1 k2) (mapv #(as* o %) ks)))) - -(defn max - "Latest/longest of the given time entities. Entities should be of the same - type" - [o & os] - (first (sort #(compare %2 %1) (cons o os)))) - -(defn min - "Earliest/shortest of the given time entities. Entities should be of the same - type" - [o & os] - (first (sort (cons o os)))) + (into (as o k1 k2) (map #(as* o %)) ks))) (defn before? "Returns `true` if time entities are ordered from the earliest to the @@ -197,13 +186,47 @@ (single-after? y (first more))) false))) -(def ^{:arglists '([x] [x y] [x y & more])} not-after? - "Like [[before?]], except returns `true` if the inputs are equal." - (complement after?)) +(defn not-after? + "Returns `true` if time entities are ordered from the earliest to the + latest (same semantics as `<=`), otherwise `false`. + + ``` + (not-after? (local-date 2009) (local-date 2010) (local-date 2011)) + ;=> true + + (not-after? (interval (instant 10000) (instant 1000000)) + (instant 99999999)) + ;=> true + ```" + ([x] true) + ([x y] (not (single-after? x y))) + ([x y & more] + (if (single-after? x y) + false + (if-some [n (next more)] + (recur y (first more) n) + (not (single-after? y (first more))))))) + +(defn not-before? + "Returns `true` if time entities are ordered from the latest to the + earliest (same semantics as `>=`), otherwise `false`. + + ``` + (not-before? (local-date 2011) (local-date 2010) (local-date 2009)) + ;=> true -(def ^{:arglists '([x] [x y] [x y & more])} not-before? - "Like [[after?]], except returns `true` if the inputs are equal." - (complement before?)) + (not-before? (instant 99999999) + (interval (instant 10000) (instant 1000000))) + ;=> true + ```" + ([x] true) + ([x y] (not (single-before? x y))) + ([x y & more] + (if (single-before? x y) + false + (if-some [n (next more)] + (recur y (first more) n) + (not (single-before? y (first more))))))) (defn plus "Adds all of the `os` to the time entity `o`. `plus` is not commutative, the @@ -240,20 +263,20 @@ Plusable (seq-plus [n xs] - (apply + n xs)) + (apply cc/+ n xs)) Minusable (seq-minus [n xs] - (apply - n xs)) + (apply cc/- n xs)) Multipliable (multiply-by [n v] - (* n (value v))) + (cc/* n (value v))) Amount - (zero? [n] (clojure.core/zero? n)) - (negative? [n] (neg? n)) - (negate [n] (- n)) + (zero? [n] (cc/zero? n)) + (negative? [n] (cc/neg? n)) + (negate [n] (cc/- n)) (abs [n] (Math/abs (long n)))) (extend-type nil @@ -268,3 +291,111 @@ :largest-min-value (fn [p] (.getLargestMinimum ^ValueRange (range p))) :smallest-max-value (fn [p] (.getSmallestMaximum ^ValueRange (range p))) :max-value (fn [p] (.getMaximum ^ValueRange (range p)))}) + +;; vars named after excluded clojure.core vars + +(def ^{:arglists '([x] [x y] [x y & more])} + <= + "Returns `true` if time entities are ordered from the earliest to the + latest (same semantics as `<=`), otherwise `false`. + + ``` + (j/<= (local-date 2009) (local-date 2010) (local-date 2011)) + ;=> true + + (j/<= (interval (instant 10000) (instant 1000000)) + (instant 99999999)) + ;=> true + ```" + not-after?) + +(def ^{:arglists '([x] [x y] [x y & more])} + >= + "Returns `true` if time entities are ordered from the latest to the + earliest (same semantics as `>=`), otherwise `false`. + + ``` + (j/>= (local-date 2011) (local-date 2010) (local-date 2009)) + ;=> true + + (j/>= (instant 99999999) + (interval (instant 10000) (instant 1000000))) + ;=> true + ```" + not-before?) + +(def ^{:arglists '([x] [x y] [x y & more])} + < + "Returns `true` if time entities are ordered from the earliest to the + latest (same semantics as `<`), otherwise `false`. + + ``` + (j/< (local-date 2009) (local-date 2010) (local-date 2011)) + => true + + (j/< (interval (instant 10000) (instant 1000000)) + (instant 99999999)) + => true + ```" + before?) + +(def ^{:arglists '([x] [x y] [x y & more])} + > + "Returns `true` if time entities are ordered from the latest to the + earliest (same semantics as `>`), otherwise `false`. + + ``` + (j/> (local-date 2011) (local-date 2010) (local-date 2009)) + => true + + (j/> (instant 99999999) + (interval (instant 10000) (instant 1000000))) + => true + ```" + after?) + +(def ^{:arglists '([o & os])} + + + "Adds all of the `os` to the time entity `o`. `+` is not commutative, the + first argument is always the entity which will accumulate the rest of the + arguments. + + ``` + (j/+ (j/local-date 2015) (j/years 1)) + => + ```" + plus) + +(def ^{:arglists '([o & os])} + - + "Subtracts all of the `os` from the time entity `o` + + ``` + (j/- (j/local-date 2015) (j/years 1)) + => + ```" + minus) + +(defn max + "Latest/longest of the given time entities. Entities should be of the same + type" + [o & os] + (first (sort #(compare %2 %1) (cons o os)))) + +(defn min + "Earliest/shortest of the given time entities. Entities should be of the same + type" + [o & os] + (first (sort (cons o os)))) + +;; these can't be aliased since they're protocol methods + +(defn neg? + "True if the amount is negative" + [a] + (negative? a)) + +(defn * + "Entity `o` multiplied by the value `v`" + [o v] + (multiply-by o v)) diff --git a/test/java_time/api_test.clj b/test/java_time/api_test.clj index 566abdd..73f4109 100644 --- a/test/java_time/api_test.clj +++ b/test/java_time/api_test.clj @@ -6,7 +6,9 @@ ;; share with java-time-test (require '[clojure.test :refer :all] - '[java-time.util :as jt.u]) + '[java-time.util :as jt.u] + '[java-time.test-utils :refer [is-antisymmetric + is-asymmetric]]) (import java.util.Locale) (def ^java.time.Clock clock (j/fixed-clock "2015-11-26T10:20:30.000000040Z" "UTC")) @@ -346,17 +348,17 @@ (is (not (j/duration? nil))) (testing "plus" (is (= (j/duration 100000001) - (j/plus (j/standard-days 1) (j/hours 3) (j/minutes 46) (j/seconds 40) (j/millis 1) (j/nanos 0)) - (j/plus (j/duration 1 :days) - (j/duration 3 :hours) - (j/duration 46 :minutes) - (j/duration 40 :seconds) - (j/duration 1 :millis) - (j/duration 0 :nanos))))) + (j/+ (j/standard-days 1) (j/hours 3) (j/minutes 46) (j/seconds 40) (j/millis 1) (j/nanos 0)) + (j/+ (j/duration 1 :days) + (j/duration 3 :hours) + (j/duration 46 :minutes) + (j/duration 40 :seconds) + (j/duration 1 :millis) + (j/duration 0 :nanos))))) (testing "minus" (is (= (j/duration "PT22H58M58.998999999S") - (j/minus (j/standard-days 1) (j/hours 1) (j/minutes 1) (j/seconds 1) (j/millis 1) (j/nanos 1))))) + (j/- (j/standard-days 1) (j/hours 1) (j/minutes 1) (j/seconds 1) (j/millis 1) (j/nanos 1))))) (testing "multiply" (is (= (j/hours 2) @@ -375,18 +377,17 @@ (testing "period" (testing "plus" (is (= (j/period 10 20 30) - (j/plus (j/years 10) (j/months 20) (j/days 30)) - (j/plus (j/period 10) - (j/period 0 20) - (j/period 0 0 30)) - (j/plus (j/period 10 :years) - (j/period 20 :months) - (j/period 30 :days))))) + (j/+ (j/years 10) (j/months 20) (j/days 30)) + (j/+ (j/period 10) + (j/period 0 20) + (j/period 0 0 30)) + (j/+ (j/period 10 :years) + (j/period 20 :months) + (j/period 30 :days))))) (testing "minus" (is (= (j/period 0 0 0) - (j/minus (j/period 10 20 30) - (j/years 10) (j/months 20) (j/days 30))))) + (j/- (j/period 10 20 30) (j/years 10) (j/months 20) (j/days 30))))) (testing "multiply" (is (= (j/days 2) @@ -401,196 +402,142 @@ (testing "year" (testing "plus" (is (= (j/year 5) - (j/plus (j/year 2) (j/years 3))))) + (j/+ (j/year 2) (j/years 3))))) (testing "minus" (is (= (j/year 0) - (j/minus (j/year 5) (j/years 5)))))) + (j/- (j/year 5) (j/years 5)))))) (testing "month" (testing "plus" (is (= (j/month :may) - (j/plus (j/month 2) 3) - (j/plus (j/month 2) (j/months 3))))) + (j/+ (j/month 2) 3) + (j/+ (j/month 2) (j/months 3))))) (testing "minus" (is (= (j/month :january) - (j/minus (j/month 5) 4) - (j/minus (j/month 5) (j/months 4)))))) + (j/- (j/month 5) 4) + (j/- (j/month 5) (j/months 4)))))) (testing "day of week" (testing "plus" (is (= (j/day-of-week :sunday) - (j/plus (j/day-of-week 1) 6) - (j/plus (j/day-of-week 1) (j/days 6))))) + (j/+ (j/day-of-week 1) 6) + (j/+ (j/day-of-week 1) (j/days 6))))) (testing "minus" (is (= (j/day-of-week :monday) - (j/minus (j/day-of-week 6) 5) - (j/minus (j/day-of-week 6) (j/days 5))))))) + (j/- (j/day-of-week 6) 5) + (j/- (j/day-of-week 6) (j/days 5))))))) + +(deftest aliases-test + (is (= j/before? j/<)) + (is (= j/not-after? j/<=)) + (is (= j/after? j/>)) + (is (= j/not-before? j/>=)) + (is (= j/plus j/+)) + (is (= j/minus j/-))) + +(defmacro is-< [a b c] + (assert ((every-pred symbol?) a b c)) + `(do (is (j/> ~a)) + (is (j/< ~a)) + (is-asymmetric (j/> ~b ~a)) + (is-asymmetric (j/< ~a ~b)) + (is-asymmetric (j/> ~c ~b ~a)) + (is-asymmetric (j/< ~a ~b ~c)) + (is (j/<= ~a)) + (is (j/>= ~a)) + (is-antisymmetric (j/<= ~a ~a)) + (is-antisymmetric (j/<= ~a ~b)) + (is-antisymmetric (j/>= ~a ~a)) + (is-antisymmetric (j/>= ~b ~a)) + (is-antisymmetric (j/<= ~a ~b ~c)) + (is-antisymmetric (j/>= ~c ~b ~a)) + (is-antisymmetric (j/<= ~a ~a ~b ~b ~c ~c)) + (is-antisymmetric (j/>= ~c ~c ~b ~b ~a ~a)))) (deftest ordering-test - (testing "times" (let [ldt (j/local-date-time clock) - ldt+5 (j/plus ldt (j/days 5))] - (is (j/after? ldt+5 ldt)) - (is (not (j/after? ldt ldt+5))) - (is (j/before? ldt ldt+5)) - (is (not (j/before? ldt+5 ldt))) - (is (j/not-after? ldt ldt)) - (is (j/not-after? ldt ldt+5)) - (is (not (j/not-after? ldt+5 ldt))) - (is (j/not-before? ldt ldt)) - (is (j/not-before? ldt+5 ldt)) - (is (not (j/not-before? ldt ldt+5)))) + ldt+5 (j/+ ldt (j/days 5)) + ldt+10 (j/+ ldt (j/days 10))] + (is-< ldt ldt+5 ldt+10)) (let [ld (j/local-date clock) - ld+5 (j/plus ld (j/days 5))] - (is (j/after? ld+5 ld)) - (is (not (j/after? ld ld+5))) - (is (j/before? ld ld+5)) - (is (not (j/before? ld+5 ld))) - (is (j/not-after? ld ld)) - (is (j/not-after? ld ld+5)) - (is (not (j/not-after? ld+5 ld))) - (is (j/not-before? ld ld)) - (is (j/not-before? ld+5 ld)) - (is (not (j/not-before? ld ld+5)))) + ld+5 (j/+ ld (j/days 5)) + ld+10 (j/+ ld (j/days 10))] + (is-< ld ld+5 ld+10)) (let [lt (j/local-time clock) - lt+5 (j/plus lt (j/minutes 5))] - (is (j/after? lt+5 lt)) - (is (not (j/after? lt lt+5))) - (is (j/before? lt lt+5)) - (is (not (j/before? lt+5 lt))) - (is (j/not-after? lt lt)) - (is (j/not-after? lt lt+5)) - (is (not (j/not-after? lt+5 lt))) - (is (j/not-before? lt lt)) - (is (j/not-before? lt+5 lt)) - (is (not (j/not-before? lt lt+5)))) + lt+5 (j/+ lt (j/minutes 5)) + lt+10 (j/+ lt (j/minutes 10))] + (is-< lt lt+5 lt+10)) (let [zdt (j/zoned-date-time clock) - zdt+5 (j/plus zdt (j/minutes 5))] - (is (j/after? zdt+5 zdt)) - (is (not (j/after? zdt zdt+5))) - (is (j/before? zdt zdt+5)) - (is (not (j/before? zdt+5 zdt))) - (is (j/not-after? zdt zdt)) - (is (j/not-after? zdt zdt+5)) - (is (not (j/not-after? zdt+5 zdt))) - (is (j/not-before? zdt zdt)) - (is (j/not-before? zdt+5 zdt)) - (is (not (j/not-before? zdt zdt+5)))) + zdt+5 (j/+ zdt (j/minutes 5)) + zdt+10 (j/+ zdt (j/minutes 10))] + (is-< zdt zdt+5 zdt+10)) (let [odt (j/offset-date-time clock) - odt+5 (j/plus odt (j/minutes 5))] - (is (j/after? odt+5 odt)) - (is (not (j/after? odt odt+5))) - (is (j/before? odt odt+5)) - (is (not (j/before? odt+5 odt))) - (is (j/not-after? odt odt)) - (is (j/not-after? odt odt+5)) - (is (not (j/not-after? odt+5 odt))) - (is (j/not-before? odt odt)) - (is (j/not-before? odt+5 odt)) - (is (not (j/not-before? odt odt+5)))) + odt+5 (j/+ odt (j/minutes 5)) + odt+10 (j/+ odt (j/minutes 10))] + (is-< odt odt+5 odt+10)) (let [ot (j/offset-time clock) - ot+5 (j/plus ot (j/minutes 5))] - (is (j/after? ot+5 ot)) - (is (not (j/after? ot ot+5))) - (is (j/before? ot ot+5)) - (is (not (j/before? ot+5 ot))) - (is (j/not-after? ot ot)) - (is (j/not-after? ot ot+5)) - (is (not (j/not-after? ot+5 ot))) - (is (j/not-before? ot ot)) - (is (j/not-before? ot+5 ot)) - (is (not (j/not-before? ot ot+5)))) + ot+5 (j/+ ot (j/minutes 5)) + ot+10 (j/+ ot (j/minutes 10))] + (is-< ot ot+5 ot+10)) (let [i (j/instant clock) - i+5 (j/plus i (j/minutes 5))] - (is (j/after? i+5 i)) - (is (not (j/after? i i+5))) - (is (j/before? i i+5)) - (is (not (j/before? i+5 i))) - (is (j/not-after? i i)) - (is (j/not-after? i i+5)) - (is (not (j/not-after? i+5 i))) - (is (j/not-before? i i)) - (is (j/not-before? i+5 i)) - (is (not (j/not-before? i i+5))))) + i+5 (j/+ i (j/minutes 5)) + i+10 (j/+ i (j/minutes 10))] + (is-< i i+5 i+10))) (testing "clocks" (let [fc (j/fixed-clock 0) - fc+1000 (j/fixed-clock 1000)] - (is (j/after? fc+1000 fc)) - (is (not (j/after? fc fc+1000))) - (is (j/before? fc fc+1000)) - (is (not (j/before? fc+1000 fc))) - (is (j/not-after? fc fc)) - (is (j/not-after? fc fc+1000)) - (is (not (j/not-after? fc+1000 fc))) - (is (j/not-before? fc fc)) - (is (j/not-before? fc+1000 fc)) - (is (not (j/not-before? fc fc+1000))))) + fc+1000 (j/fixed-clock 1000) + fc+2000 (j/fixed-clock 2000)] + (is-< fc fc+1000 fc+2000))) (testing "fields" (let [thursday (j/day-of-week :thursday) saturday (j/day-of-week :saturday) sunday (j/day-of-week :sunday)] + ;; no properties (is (j/after? saturday :thursday)) (is (not (j/after? thursday :saturday))) (is (j/before? saturday :sunday)) (is (not (j/before? sunday :saturday))) - (is (j/not-after? saturday saturday)) - (is (j/not-after? saturday sunday)) - (is (not (j/not-after? sunday saturday))) - (is (j/not-before? saturday saturday)) - (is (j/not-before? sunday saturday)) - (is (not (j/not-before? saturday sunday)))) + ;; has properties + (is-< thursday saturday sunday)) (let [january (j/month :january) february (j/month :february) march (j/month :march)] + ;; no properties (is (j/after? february :january)) (is (not (j/after? january :february))) (is (j/before? february :march)) (is (not (j/before? march :february))) - (is (j/not-after? january january)) - (is (j/not-after? february march)) - (is (not (j/not-after? march february))) - (is (j/not-before? january january)) - (is (j/not-before? february january)) - (is (not (j/not-before? january february)))) + ;; has properties + (is-< january february march)) (let [year-2009 (j/year 2009) - year-2010 (j/year 2010)] + year-2010 (j/year 2010) + year-2011 (j/year 2011)] + ;; no properties (is (j/after? year-2010 2009)) (is (not (j/after? year-2009 2010))) (is (j/before? year-2009 2010)) (is (not (j/before? year-2010 2009))) - (is (j/not-after? year-2010 year-2010)) - (is (j/not-after? year-2009 year-2010)) - (is (not (j/not-after? year-2010 year-2009))) - (is (j/not-before? year-2010 year-2010)) - (is (j/not-before? year-2010 year-2009)) - (is (not (j/not-before? year-2009 year-2010)))) + ;; has properties + (is-< year-2009 year-2010 year-2011)) (let [jan-1 (j/month-day 1 1) - apr-1 (j/month-day 4 1)] - (is (j/after? apr-1 jan-1)) - (is (not (j/after? jan-1 apr-1))) - (is (j/before? jan-1 apr-1)) - (is (not (j/before? apr-1 jan-1))) - (is (j/not-after? jan-1 jan-1)) - (is (j/not-after? jan-1 apr-1)) - (is (not (j/not-after? apr-1 jan-1))) - (is (j/not-before? jan-1 jan-1)) - (is (j/not-before? apr-1 jan-1)) - (is (not (j/not-before? jan-1 apr-1)))))) + apr-1 (j/month-day 4 1) + may-1 (j/month-day 5 1)] + (is-< jan-1 apr-1 may-1)))) (deftest mock-clock-test (testing "constructors" @@ -736,7 +683,7 @@ (deftest seq-test (is (= [(j/local-date 2015) (j/local-date 2016)] - (take 2 (j/iterate j/plus (j/local-date 2015) (j/years 1)))))) + (take 2 (j/iterate j/+ (j/local-date 2015) (j/years 1)))))) (deftest adjuster-test (testing "predefined adjusters" @@ -753,7 +700,7 @@ (j/local-date 2016 1 1)))) (testing "functions as adjusters" - (is (= (j/adjust (j/local-date 2015 1 1) j/plus (j/days 1)) + (is (= (j/adjust (j/local-date 2015 1 1) j/+ (j/days 1)) (j/local-date 2015 1 2))))) (deftest sugar-test @@ -925,50 +872,59 @@ (testing "ordering" (let [interval-1-2 (j/interval 1 2) interval-3-4 (j/interval 3 4) + interval-1-3 (j/interval 1 3) instant-1 (j/instant 1) + instant-2 (j/instant 2) instant-3 (j/instant 3)] - (is (j/before? interval-1-2 interval-3-4)) - (is (not (j/before? interval-3-4 interval-1-2))) - (is (j/before? interval-1-2 instant-3)) - (is (not (j/before? interval-3-4 instant-1))) - - (is (j/after? interval-3-4 interval-1-2)) - (is (not (j/after? interval-1-2 interval-3-4))) - (is (j/after? interval-3-4 instant-1)) - (is (not (j/after? interval-1-2 instant-3))) - - (is (j/not-before? interval-3-4 interval-3-4)) - (is (not (j/not-before? interval-1-2 interval-3-4))) + (is-asymmetric (j/before? interval-1-2 interval-3-4)) + (is-asymmetric (j/before? interval-1-2 instant-3)) + (is-asymmetric ((complement j/before?) interval-3-4 instant-1)) + + (is-asymmetric (j/after? interval-3-4 interval-1-2)) + (is-asymmetric (j/after? interval-3-4 instant-1)) + (is-asymmetric ((complement j/after?) interval-1-2 instant-3)) + + (is-antisymmetric (j/not-before? interval-3-4 interval-3-4)) + (is-antisymmetric ((complement j/not-before?) interval-1-2 interval-3-4)) + ;; not antisymmetric since they overlap (is (j/not-before? interval-3-4 instant-3)) - (is (j/not-before? interval-3-4 instant-1)) - (is (not (j/not-before? interval-1-2 instant-3))) + (is (j/not-before? instant-3 interval-3-4)) + (is-antisymmetric (j/not-before? interval-3-4 instant-1)) + (is-antisymmetric ((complement j/not-before?) interval-1-2 instant-3)) - (is (j/not-after? interval-1-2 interval-1-2)) - (is (j/not-after? interval-1-2 interval-3-4)) - (is (not (j/not-after? interval-3-4 interval-1-2))) + (is-antisymmetric (j/not-after? interval-1-2 interval-1-2)) + (is-antisymmetric (j/not-after? interval-1-2 interval-3-4)) + ;; not antisymmetric since they overlap (is (j/not-after? interval-1-2 instant-1)) - (is (j/not-after? interval-1-2 instant-3)) - (is (not (j/not-after? interval-3-4 instant-1)))) - (is (j/before? (j/interval 1000 2000) (j/instant 5000))) - (is (j/before? (j/interval 1000 5000) (j/instant 5000)) - "exclusive end") - (is (j/before? (j/interval 1000 5000) (j/interval 5000 6000)) - "exclusive end") - (is (j/before? (j/interval 1000 5000) (j/interval 5001 6000))) - - (is (j/after? (j/interval 1000 5000) (j/instant 100))) - (is (j/after? (j/interval 1000 5000) (j/instant 999))) + (is (j/not-after? instant-1 interval-1-2)) + (is (j/<= instant-1 interval-1-2 instant-2)) + (is (not (j/<= instant-1 interval-1-2 instant-2 interval-1-2 instant-1))) + ;; FIXME breaks transitivity!! + ;; (is (not (j/not-after? instant-1 interval-1-3 instant-2 interval-1-3 instant-1))) + (is (j/<= instant-1 interval-1-2 interval-1-2 instant-1)) + (is-antisymmetric (j/not-after? interval-1-2 instant-3)) + (is-antisymmetric ((complement j/not-after?) interval-3-4 instant-1))) + (is-asymmetric (j/before? (j/interval 1000 2000) (j/instant 5000))) + (testing "exclusive end" + (is-asymmetric (j/before? (j/interval 1000 5000) (j/instant 5000))) + (is-asymmetric (j/before? (j/interval 1000 5000) (j/interval 5000 6000)))) + (is-asymmetric (j/before? (j/interval 1000 5000) (j/interval 5001 6000))) + + (is-asymmetric (j/after? (j/interval 1000 5000) (j/instant 100))) + (is-asymmetric (j/after? (j/interval 1000 5000) (j/instant 999))) (is (not (j/after? (j/interval 1000 5000) (j/instant 1000))) "inclusive start") + (is (not (j/after? (j/instant 1000) (j/interval 1000 5000))) + "inclusive start") (is (not (j/after? (j/interval 1000 5000) (j/instant 2000)))) - (is (j/after? (j/interval 1000 5000) (j/interval 100 999))) - (is (j/after? (j/interval 1000 5000) (j/interval 100 1000)) - "exclusive end") + (is-asymmetric (j/after? (j/interval 1000 5000) (j/interval 100 999))) + (testing "exclusive end" + (is-asymmetric (j/after? (j/interval 1000 5000) (j/interval 100 1000)))) (testing "instant <= >= * - + ;; TODO below here needs unit tests chronology leap? with-value with-min-value with-max-value with-largest-min-value with-smallest-max-value] @@ -180,7 +181,8 @@ (concat [";; NOTE: This namespace is generated by java-time.dev.gen" `(~'ns ~nsym - (:refer-clojure :exclude ~'(zero? range iterate max min contains? format abs)) + (:refer-clojure :exclude ~'(zero? range iterate max min contains? format abs + < > <= >= * - + neg?)) (:require ~'[java-time core properties temporal amount zone single-field local chrono convert sugar seqs adjuster interval format joda clock pre-java8 mock]))] (apply import-vars (:macros impl-info)) diff --git a/test/java_time/test_utils.clj b/test/java_time/test_utils.clj new file mode 100644 index 0000000..019f726 --- /dev/null +++ b/test/java_time/test_utils.clj @@ -0,0 +1,241 @@ +(ns java-time.test-utils + (:refer-clojure :exclude [boolean?]) + (:require [clojure.math.combinatorics :as comb] + [clojure.test :refer [deftest is]])) + +(def boolean? #(or (true? %) (false? %))) + +(defn ^:private validate-_is-args [msg] + (assert (string? msg) (str "is: message must be a string: " (pr-str msg)))) + +(defn ^:dynamic _is [f args msg] + (validate-_is-args msg) + (is (apply f args) msg)) + +(defn ^:private reorder-vector [v order] + (assert ((every-pred vector?) v order)) + (assert (= (count v) (count order))) + (assert (next order)) + (assert (apply distinct? order)) + (reduce (fn [v' [from to]] + (assoc v' to (nth v from))) + v (map vector (range (count v)) order))) + +(deftest ^:private reorder-vector-test + (is (= '[a b c d] + (reorder-vector '[a b c d] [0 1 2 3]))) + (is (= '[a b d c] + (reorder-vector '[a b c d] [0 1 3 2]))) + (is (= '[d c b a] + (reorder-vector '[a b c d] [3 2 1 0])))) + +; aRb => !bRa +; aRb && bRc => !bRa && !cRb && !cRa && aRc +(defn is-asymmetric* [prop R args R-syn args-syn] + {:pre [((every-pred vector?) args args-syn) + (#{:asymmetric :antisymmetric} prop)] + :post [(boolean? %)]} + (let [nargs (count args) + _ (assert (= nargs (count args-syn))) + _ (assert (<= 2 nargs) "Must provide at least 2 arguments") + split-args+syn (fn [args+syn] + [(mapv first args+syn) + (mapv second args+syn)]) + args+syn (mapv vector args args-syn) + continue (volatile! true) + _ (doseq [;; for each combination of 2 or more args + nargs (range 2 (inc nargs)) + :while @continue + args+syn (map vec (comb/combinations args+syn nargs)) + :let [[args args-syn] (split-args+syn args+syn)] + ;; (R args...) is true + :while (or (_is R args (pr-str (list* R-syn args-syn))) + (vreset! continue false)) + :let [original-order (range nargs)] + order (comb/permutations original-order) + :while @continue + :when (not= order original-order) + :let [[args args-syn] (split-args+syn (reorder-vector args+syn order))] + :while (or (case prop + ;; and (R args..) is false for every *other* permutation of args + :asymmetric (_is (complement R) args (pr-str (list 'not (list* R-syn args-syn)))) + ;; and (R args..) is false for every *other* permutation of args, or one of the + ;; out-of-order arguments is equal + :antisymmetric (let [out-of-order-args (loop [out [] + order order] + (if (next order) + (let [[l r] order] + (recur (cond-> out + (> l r) (conj [l r])) + (next order))) + out)) + _ (assert (seq out-of-order-args)) + f (fn [& args] + (let [args (vec args)] + (if (apply R args) + (every? (fn [[l r]] + (= (nth args l) + (nth args r))) + out-of-order-args) + true))) + conjunction-syn (mapv (fn [[l r]] + (list '= (nth args-syn l) (nth args-syn r))) + out-of-order-args)] + (_is f args (pr-str (list 'or (list 'not (list* R-syn args-syn)) + (if (= 1 (count conjunction-syn)) + (first conjunction-syn) + (list* 'and conjunction-syn))))))) + (vreset! continue false))])] + @continue)) + +(defmacro is-asymmetric + "With two arguments, tests that (R a b) is true and (R b a) is false. + With three arguments (R a b c): + 1. tests the previous property for (R a b), (R b c), and (R a c) + 2. tests that (R a b c) is true. + 3. tests that these are false: (R a c b), (R b a c), (R b c a), (R c a b), (R c b a). + + Similar for four or more arguments." + [[R a b & args :as all]] + (assert (seq? all)) + (assert (<= 3 (count all)) + (str "Must provide 2 or more arguments: " (pr-str all))) + `(is-asymmetric* :asymmetric + ~R (into [~a ~b] ~(vec args)) + '~R (into '~[a b] '~(vec args)))) + +; aRb && bRa => a=b +(defmacro is-antisymmetric + "With two arguments (R a b), tests that: + 1. (R a b) is true + 2. if (R b a) then a=b + With three arguments (R a b c): + 1. tests the previous property for (R a b), (R b c), and (R a c) + 2. tests that (R a b c) is true. + 3. tests that + - if (R a c b) then b=c + - if (R b a c) then a=b + - if (R b c a) then c=a + - if (R c a b) then c=a + - if (R c b a) then a=b and b=c + + Similar for four or more arguments." + [[R a b & args :as all]] + (assert (seq? all)) + (assert (<= 3 (count all)) + (str "Must provide 2 or more arguments: " (pr-str all))) + `(is-asymmetric* :antisymmetric + ~R (into [~a ~b] ~(vec args)) + '~R (into '~[a b] '~(vec args)))) + +(defn ^:private with-expected-results* + [expected-results f] + (assert (even? (count expected-results))) + (let [actual-results (atom []) + _ (binding [_is (fn [f args msg] + (validate-_is-args msg) + (let [res (apply f args)] + (swap! actual-results conj (read-string msg) (if res :pass :fail)) + res))] + (f)) + actual-results @actual-results] + (or (= expected-results actual-results) + (throw (ex-info (str "Expected result: " + (pr-str expected-results) + "\n Actual result: " + (pr-str actual-results)) + {:expected-result expected-results + :actual-results actual-results}))))) + +(defmacro ^:private with-expected-results + [results & body] + `(with-expected-results* '~results #(do ~@body))) + +(defn ^:private is-ex-data* [expected-ex-data f] + (try (f) + (is false (str "No error thrown")) + (catch Exception e + (is (= expected-ex-data (ex-data e)))))) + +(defmacro ^:private is-ex-data [expected-ex-data body] + `(is-ex-data* ~expected-ex-data #(do ~body))) + +(deftest ^:private _is-test + (is (with-expected-results [(< 2 1) :pass] + (_is (constantly true) [] "(< 2 1)"))) + (is-ex-data + '{:expected-result [(< 2 1) :fail] + :actual-results [(< 2 1) :pass]} + (with-expected-results [(< 2 1) :fail] + (_is (constantly true) [] "(< 2 1)"))) + (is-ex-data + '{:expected-result [(< 2 1) :pass] + :actual-results [(< 2 1) :pass + (< 2 1) :pass]} + (with-expected-results [(< 2 1) :pass] + (_is (constantly true) [] "(< 2 1)") + (_is (constantly true) [] "(< 2 1)")))) + +(deftest ^:private is-asymmetric-test + (is (with-expected-results [(< 1 2) :pass + (not (< 2 1)) :pass] + (is-asymmetric (< 1 2)))) + (let [a 1 + b 2] + (is (with-expected-results [(< a b) :pass + (not (< b a)) :pass] + (is-asymmetric (< a b))))) + (is (with-expected-results [(> 1 2) :fail] + (is-asymmetric (> 1 2)))) + (is (with-expected-results [(<= 1 1) :pass + (not (<= 1 1)) :fail] + (is-asymmetric (<= 1 1)))) + (is (with-expected-results [(<= 1 1) :pass + (not (<= 1 1)) :fail] + (is-asymmetric (<= 1 1 1 1 1 1)))) + (is (with-expected-results + [(< 1 2) :pass + (not (< 2 1)) :pass + (< 1 3) :pass + (not (< 3 1)) :pass + (< 2 3) :pass + (not (< 3 2)) :pass + (< 1 2 3) :pass + (not (< 1 3 2)) :pass + (not (< 2 1 3)) :pass + (not (< 3 1 2)) :pass + (not (< 2 3 1)) :pass + (not (< 3 2 1)) :pass] + (is-asymmetric (< 1 2 3))))) + +(deftest ^:private is-antisymmetric-test + (is (with-expected-results [(<= 1 1) :pass + (or (not (<= 1 1)) (= 1 1)) :pass] + (is-antisymmetric (<= 1 1)))) + (let [a 1 + b 1] + (is (with-expected-results [(<= a b) :pass + (or (not (<= b a)) (= a b)) :pass] + (is-antisymmetric (<= a b))))) + (is (with-expected-results [(<= 1 2) :pass + (or (not (<= 2 1)) (= 1 2)) :pass] + (is-antisymmetric (<= 1 2)))) + (is (with-expected-results [(> 1 2) :fail] + (is-antisymmetric (> 1 2)))) + (is (with-expected-results [(> 2 1) :pass + (or (not (> 1 2)) (= 2 1)) :pass] + (is-antisymmetric (> 2 1)))) + (is (with-expected-results + [(<= 1 2) :pass + (or (not (<= 2 1)) (= 1 2)) :pass + (<= 1 3) :pass + (or (not (<= 3 1)) (= 1 3)) :pass + (<= 2 3) :pass + (or (not (<= 3 2)) (= 2 3)) :pass + (<= 1 2 3) :pass + (or (not (<= 1 3 2)) (= 2 3)) :pass + (or (not (<= 2 1 3)) (= 1 2)) :pass + (or (not (<= 3 1 2)) (= 2 3)) :pass + (or (not (<= 2 3 1)) (= 1 2)) :pass + (or (not (<= 3 2 1)) (and (= 1 2) (= 2 3))) :pass] + (is-antisymmetric (<= 1 2 3)))))