New upstream version 0.14.0

parents
lib
bin
classes
target
clj-time.jar
pom.xml
*.jar
*~
.lein-*
.nrepl-*
checkouts
.classpath
.project
.settings
*.iml
.idea
/pom.xml.asc
test/readme.clj
doc/*
language: clojure
cache:
directories:
- $HOME/.m2
before_script:
- rm -f test/readme.clj
- echo y | sudo lein upgrade
script: lein test-all
jdk:
- openjdk7
- oraclejdk7
- oraclejdk8
## Changes Between 0.13.0 and 0.14.0
* Add `from-epoch`.
* Clarify `today-at` is UTC.
* Add optional `clj-time.spec` namespace (requires Clojure 1.9 Alpha 17).
* Add `clj-time.types` namespace with type-based predicates.
## Changes Between 0.12.2 and 0.13.0
* Update Joda Time to 2.9.7 (#223).
## Changes Between 0.12.1 and 0.12.2
* More reflection warnings removed (#221).
## Changes Between 0.12.0 and 0.12.1
* Updated Joda Time (to 2.9.4) and several testing dependencies as well.
* `floor` now retains timezone information (#204).
* Reflection warning removed from `with-time-at-start-of-day` (#219).
## Changes Between 0.11.0 and 0.12.0 (June 6th, 2016)
### Joda Time 2.9.3
[Joda Time](http://www.joda.org/joda-time/) has been [upgraded to `2.9.3`](http://www.joda.org/joda-time/upgradeto293.html).
### java.jdbc coercions
`java.sql.Date` and `java.sql.Time` are now coerced automatically if you `require` `clj-time.jdbc` (previously only `java.sql.Timestamp` was coerced).
### Formatters
The `formatter` function now accepts keywords and formatter objects, as well as string formats, so you can easily select a standard format (with a keyword) or provide a specific formatter object.
### Clojure 1.8 by Default
The library now depends on Clojure `1.8.0` and is tested against `1.6.0`, `1.7.0`, and `1.9.0-master-SNAPSHOT`.
### Enhancements
* `first-day-of-the-month?` and `last-day-of-the-month?` -- aliases to improve consistency (so it's `the-month` everywhere).
* `min-date` and `max-date` -- to return the minimum or maximum of two or more dates.
* `nth-day-of-the-month` -- return the Nth day of the year/month, or date/time.
* `nth-day-of-the-month?` -- return true if a given date/time is on the Nth day of its month.
* `with-time-at-start-of-day` -- return the time at the start of the day, including timezone and DST support.
### Deprecations
* `today-at-midnight` -- deprecated in favor of `with-time-at-start-of-day`.
### Bug Fixes
[Issue 185](https://github.com/clj-time/clj-time/issues/185) is fixed by explicitly tying the `:rfc822` formatter to the US locale.
The examples in the README are now automatically tested (by midje-readme) so they are more likely to be valid.
## Changes Between 0.10.0 and 0.11.0
### Joda Time 2.8
[Joda Time](http://www.joda.org/joda-time/) has been [upgraded to `2.8.2`](http://www.joda.org/joda-time/upgradeto282.html).
### Clojure 1.7 by Default
The library now depends on Clojure `1.7.0`.
## Changes Between 0.9.0 and 0.10.0
### clj-time.instant
`clj-time.instant` is a new namespace that, when loaded, makes it possible
for the Clojure reader to serialize Joda Time data types.
### Joda Time 2.7
[Joda Time](http://www.joda.org/joda-time/) has been [upgraded to `2.7`](http://www.joda.org/joda-time/upgradeto270.html).
### `equal?`
A new predicate `equal?` has been added, which can be used like `before?` and
`after?` to assert two instants represent identical points in time regardless of
differences in time zone.
Contributed by [@gws](https://github.com/gws).
### `clj-time.jdbc`
A new namespace `clj-time.jdbc` has been added which extends protocols in
clojure.java.jdbc to allow for easy coercion to and from java.sql.Timestamp
driven by `clj-time.coerce`.
Contributed by [@gws](https://github.com/gws).
## Changes Between 0.8.0 and 0.9.0
### with-default-year
`clj-time.format/with-default-year` is a new function that accepts a formatter
and returns a formatter that uses provided year by default.
Contributed by lummax.
### InTimeUnitProtocol
`clj-time.core/InTimeUnitProtocol` is a new protocol that provides convenient
functions for time unit conversion:
``` clojure
(defprotocol InTimeUnitProtocol
"Interface for in-<time unit> functions"
(in-millis [this] "Return the time in milliseconds.")
(in-seconds [this] "Return the time in seconds.")
(in-minutes [this] "Return the time in minutes.")
(in-hours [this] "Return the time in hours.")
(in-days [this] "Return the time in days.")
(in-weeks [this] "Return the time in weeks")
(in-months [this] "Return the time in months")
(in-years [this] "Return the time in years"))
```
### Clojure 1.2-1.4 Support Dropped
`clj-time` now officially supports Clojure `1.5.1` and later versions.
### Joda Time 2.6
[Joda Time](http://www.joda.org/joda-time/) has been [upgraded to `2.6`](http://www.joda.org/joda-time/upgradeto260.html).
Copyright 2013 Mark McGranaghan, Sean Corfield and other contributors
https://github.com/clj-time/clj-time
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This diff is collapsed.
(defproject clj-time/clj-time "0.14.0"
:description "A date and time library for Clojure, wrapping Joda Time."
:url "https://github.com/clj-time/clj-time"
:mailing-list {:name "clj-time mailing list"
:archive "https://groups.google.com/forum/?fromgroups#!forum/clj-time"
:post "clj-time@googlegroups.com"}
:license {:name "MIT License"
:url "http://www.opensource.org/licenses/mit-license.php"
:distribution :repo}
:dependencies [[joda-time "2.9.7"]
[org.clojure/clojure "1.8.0" :scope "provided"]]
:min-lein-version "2.0.0"
:global-vars {*warn-on-reflection* true}
:profiles {:dev {:dependencies [[org.clojure/java.jdbc "0.6.1"]]
:plugins [[codox "0.8.10"]]}
:midje {:dependencies [[midje "1.9.0-alpha5"]]
:plugins [[lein-midje "3.2.1"]
[midje-readme "1.0.9"]]
:midje-readme {:require "[clj-time.core :as t] [clj-time.predicates :as pr] [clj-time.format :as f] [clj-time.coerce :as c]"}}
:1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]}
:1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]}
:master {:repositories [["snapshots" "https://oss.sonatype.org/content/repositories/snapshots/"]]
:dependencies [[org.clojure/clojure "1.9.0-master-SNAPSHOT"]]}
:spec {:dependencies [[org.clojure/clojure "1.9.0-alpha17"]
[org.clojure/test.check "0.9.0"]]
:test-paths ["test" "test_clj_1.9"]}}
:aliases {"test-all" ["with-profile" "dev,master,default,midje:dev,default,midje:dev,1.6,midje:dev,1.7,midje" "test"]})
(ns clj-time.coerce
"Utilites to coerce Joda DateTime instances to and from various other types.
For example, to convert a Joda DateTime to and from a Java long:
=> (to-long (date-time 1998 4 25))
893462400000
=> (from-long 893462400000)
#<DateTime 1998-04-25T00:00:00.000Z>"
(:refer-clojure :exclude [extend second])
(:require [clj-time.core :refer :all]
[clj-time.format :as time-fmt])
(:import [java.sql Timestamp]
[java.util Date]
[org.joda.time DateTime DateTimeZone DateMidnight YearMonth
LocalDate LocalDateTime]))
(defprotocol ICoerce
(^org.joda.time.DateTime
to-date-time [obj] "Convert `obj` to a Joda DateTime instance."))
(defn from-long
"Returns a DateTime instance in the UTC time zone corresponding to the given
number of milliseconds after the Unix epoch."
[^Long millis]
(DateTime. millis ^DateTimeZone utc))
(defn from-epoch
"Returns a DateTime instance in the UTC time zone
from given Unix epoch."
[^Long epoch]
(from-long (* epoch 1000)))
(defn from-string
"return DateTime instance from string using
formatters in clj-time.format, returning first
which parses"
[^String s]
(time-fmt/parse s))
(defn from-date
"Returns a DateTime instance in the UTC time zone corresponding to the given
Java Date object."
[^Date date]
(when date
(from-long (.getTime date))))
(defn from-sql-date
"Returns a DateTime instance in the UTC time zone corresponding to the given
java.sql.Date object."
[^java.sql.Date sql-date]
(when sql-date
(from-long (.getTime sql-date))))
(defn from-sql-time
"Returns a DateTime instance in the UTC time zone corresponding to the given
java.sql.Timestamp object."
[^java.sql.Timestamp sql-time]
(when sql-time
(from-long (.getTime sql-time))))
(defn to-long
"Convert `obj` to the number of milliseconds after the Unix epoch."
[obj]
(if-let [dt (to-date-time obj)]
(.getMillis dt)))
(defn to-epoch
"Convert `obj` to Unix epoch."
[obj]
(let [millis (to-long obj)]
(and millis (quot millis 1000))))
(defn to-date
"Convert `obj` to a Java Date instance."
[obj]
(if-let [dt (to-date-time obj)]
(Date. (.getMillis dt))))
(defn to-sql-date
"Convert `obj` to a java.sql.Date instance."
[obj]
(if-let [dt (to-date-time obj)]
(java.sql.Date. (.getMillis dt))))
(defn to-sql-time
"Convert `obj` to a java.sql.Timestamp instance."
[obj]
(if-let [dt (to-date-time obj)]
(java.sql.Timestamp. (.getMillis dt))))
(defn to-string
"Returns a string representation of obj in UTC time-zone
using (ISODateTimeFormat/dateTime) date-time representation."
[obj]
(if-let [^DateTime dt (to-date-time obj)]
(time-fmt/unparse (:date-time time-fmt/formatters) dt)))
(defn to-timestamp
"Convert `obj` to a Java SQL Timestamp instance."
[obj]
(if-let [dt (to-date-time obj)]
(Timestamp. (.getMillis dt))))
(defn to-local-date
"Convert `obj` to a org.joda.time.LocalDate instance"
[obj]
(if-let [dt (to-date-time obj)]
(LocalDate. (.getMillis (from-time-zone dt (default-time-zone))))))
(defn to-local-date-time
"Convert `obj` to a org.joda.time.LocalDateTime instance"
[obj]
(if-let [dt (to-date-time obj)]
(LocalDateTime. (.getMillis (from-time-zone dt (default-time-zone))))))
(defn in-time-zone
"Convert `obj` into `tz`, return org.joda.time.LocalDate instance."
[obj tz]
(if-let [dt (to-date-time obj)]
(-> dt
(to-time-zone tz)
.toLocalDate)))
(extend-protocol ICoerce
nil
(to-date-time [_]
nil)
Date
(to-date-time [date]
(from-date date))
java.sql.Date
(to-date-time [sql-date]
(from-sql-date sql-date))
java.sql.Timestamp
(to-date-time [sql-time]
(from-sql-time sql-time))
DateTime
(to-date-time [date-time]
date-time)
DateMidnight
(to-date-time [date-midnight]
(.toDateTime date-midnight))
YearMonth
(to-date-time [year-month]
(date-time (year year-month) (month year-month)))
LocalDate
(to-date-time [local-date]
(date-time (year local-date) (month local-date) (day local-date)))
LocalDateTime
(to-date-time [local-date-time]
(date-time (year local-date-time) (month local-date-time) (day local-date-time)
(hour local-date-time) (minute local-date-time) (second local-date-time)
(milli local-date-time)))
Integer
(to-date-time [integer]
(from-long (long integer)))
Long
(to-date-time [long]
(from-long long))
String
(to-date-time [string]
(from-string string))
Timestamp
(to-date-time [timestamp]
(from-date timestamp)))
This diff is collapsed.
This diff is collapsed.
(ns clj-time.instant
"An optional convenience namespaces that allows key JodaTime types
to be transparently serialized with the Clojure reader (via instant literals)."
)
(defmethod print-dup org.joda.time.base.AbstractInstant
[^org.joda.time.base.AbstractInstant d out]
(print-dup (.toDate d) out))
(defmethod print-dup org.joda.time.base.AbstractPartial
[^org.joda.time.base.AbstractPartial d out]
(print-dup (.toDate d) out))
(ns clj-time.internal.fn)
;;
;; API
;;
(defn fpartial
"Like clojure.core/partial but prepopulates last N arguments (first is passed in later)"
[f & args]
(fn [arg & more] (apply f arg (concat args more))))
(ns clj-time.jdbc
"clojure.java.jdbc protocol extensions supporting DateTime coercion.
To use in your project, just require the namespace:
=> (require 'clj-time.jdbc)
nil
Doing so will extend the protocols defined by clojure.java.jdbc, which will
cause java.sql.Timestamp objects in JDBC result sets to be coerced to
org.joda.time.DateTime objects, and vice versa where java.sql.Timestamp
objects would be required by JDBC."
(:require [clj-time.coerce :as tc]
[clojure.java.jdbc :as jdbc]))
; http://clojure.github.io/java.jdbc/#clojure.java.jdbc/IResultSetReadColumn
(extend-protocol jdbc/IResultSetReadColumn
java.sql.Timestamp
(result-set-read-column [v _2 _3]
(tc/from-sql-time v))
java.sql.Date
(result-set-read-column [v _2 _3]
(tc/from-sql-date v))
java.sql.Time
(result-set-read-column [v _2 _3]
(org.joda.time.DateTime. v)))
; http://clojure.github.io/java.jdbc/#clojure.java.jdbc/ISQLValue
(extend-protocol jdbc/ISQLValue
org.joda.time.DateTime
(sql-value [v]
(tc/to-sql-time v)))
(ns clj-time.local
"Functions for working with local time without having to shift
to/from utc, the preferred time zone of clj-time.core.
Get the current local time with (local-now).
(to-local-date-time obj) returns a local date-time instance
retaining the time fields.
The following all return 1986-10-14 04:03:27.246 with the
local time zone.
(to-local-date-time (clj-time.core/date-time 1986 10 14 4 3 27 246))
(to-local-date-time \"1986-10-14T04:03:27.246\")
(to-local-date-time \"1986-10-14T04:03:27.246Z\")
The dynamic var *local-formatters* contains a map of local formatters
for parsing and printing. It is initialized with all the formatters in
clj-time.format localized.
to-local-date-time for strings uses *local-formatters* to parse.
(format-local-time (local-now) :basic-date-time) formats an obj using
a formatter in *local-formatters* corresponding to the format-key
passed in.
"
(:require [clj-time.core :as time]
[clj-time.coerce :as coerce]
[clj-time.format :as fmt])
(:import [org.joda.time DateTime DateTimeZone]
[org.joda.time.format DateTimeFormatter]))
(def ^{:doc "Map of local formatters for parsing and printing." :dynamic true}
*local-formatters*
(into {} (map
(fn [[k ^DateTimeFormatter f]] [k (.withZone f ^DateTimeZone (time/default-time-zone))])
fmt/formatters)))
(defn local-now
"Returns a DateTime for the current instant in the default time zone."
[]
(DateTime/now ^DateTimeZone (time/default-time-zone)))
(defprotocol ILocalCoerce
(to-local-date-time [obj] "convert `obj` to a local Joda DateTime instance retaining time fields."))
(defn- as-local-date-time-from-time-zone
"Coerce to date-time in the default time zone retaining time fields."
[obj]
(-> obj coerce/to-date-time (time/from-time-zone (time/default-time-zone))))
(defn- as-local-date-time-to-time-zone
"Coerce to date-time in the default time zone."
[obj]
(-> obj coerce/to-date-time (time/to-time-zone (time/default-time-zone))))
(defn- from-local-string
"Return local DateTime instance from string using
formatters in *local-formatters*, returning first
which parses."
[s]
(first
(for [f (vals *local-formatters*)
:let [d (try (fmt/parse f s) (catch Exception _ nil))]
:when d] d)))
(extend-protocol ILocalCoerce
nil
(to-local-date-time [_]
nil)
java.util.Date
(to-local-date-time [date]
(as-local-date-time-to-time-zone date))
java.sql.Date
(to-local-date-time [sql-date]
(as-local-date-time-to-time-zone sql-date))
DateTime
(to-local-date-time [date-time]
(as-local-date-time-from-time-zone date-time))
Integer
(to-local-date-time [integer]
(as-local-date-time-from-time-zone (long integer)))
Long
(to-local-date-time [long]
(as-local-date-time-from-time-zone long))
String
(to-local-date-time [string]
(from-local-string string))
java.sql.Timestamp
(to-local-date-time [timestamp]
(as-local-date-time-to-time-zone timestamp)))
(defn format-local-time
"Format obj as local time using the local formatter corresponding
to format-key."
[obj format-key]
(when-let [dt (to-local-date-time obj)]
(when-let [fmt (format-key *local-formatters*)]
(fmt/unparse fmt dt))))
(ns clj-time.periodic
(:require [clj-time.core :as ct])
(:import [org.joda.time DateTime ReadablePeriod Period]))
(defn periodic-seq
"Returns a sequence of date-time values growing over specific period.
The 2 argument function takes as input the starting value and the growing value,
returning a lazy infinite sequence.
The 3 argument function takes as input the starting value, the upper bound value,
and the growing value, return a lazy sequence."
([^DateTime start ^ReadablePeriod period-like]
(let [^Period period (.toPeriod period-like)]
(map (fn [i]
(ct/plus start (.multipliedBy period i)))
(iterate inc 0))))
([^DateTime start ^DateTime end ^ReadablePeriod period-like]
(let [^Period period (.toPeriod period-like)]
(take-while (fn [^DateTime next]
(ct/before? next end))
(periodic-seq start period-like)))))
(ns clj-time.predicates
"Predicate functions to ask basic questions about a date
Was it Monday?
(monday? (clj-time.core/date-time 1999 9 9))
Is it January?
(january? (clj-time.core/date-time 2011 1 1))"
(:require [clj-time.core :as time]
[clj-time.coerce :as coerce]))
;; days of the week
(defn monday? [date-time]
(= (time/day-of-week date-time) 1))
(defn tuesday? [date-time]
(= (time/day-of-week date-time) 2))
(defn wednesday? [date-time]
(= (time/day-of-week date-time) 3))
(defn thursday? [date-time]
(= (time/day-of-week date-time) 4))
(defn friday? [date-time]
(= (time/day-of-week date-time) 5))
(defn saturday? [date-time]
(= (time/day-of-week date-time) 6))
(defn sunday? [date-time]
(= (time/day-of-week date-time) 7))
;; weekend / weekday checks
(defn weekend? [date-time]
(or (saturday? date-time) (sunday? date-time)))
(defn weekday? [date-time]
(not (weekend? date-time)))
;; months of the year
(defn january? [date-time]
(= (time/month date-time) 1))
(defn february? [date-time]
(= (time/month date-time) 2))
(defn march? [date-time]
(= (time/month date-time) 3))
(defn april? [date-time]
(= (time/month date-time) 4))
(defn may? [date-time]
(= (time/month date-time) 5))
(defn june? [date-time]
(= (time/month date-time) 6))
(defn july? [date-time]
(= (time/month date-time) 7))
(defn august? [date-time]
(= (time/month date-time) 8))
(defn september? [date-time]
(= (time/month date-time) 9))
(defn october? [date-time]
(= (time/month date-time) 10))
(defn november? [date-time]
(= (time/month date-time) 11))
(defn december? [date-time]
(= (time/month date-time) 12))
;;First and last day of month checks
(defn last-day-of-month? [date-time]
(time/equal? (time/last-day-of-the-month date-time) date-time))
(def last-day-of-the-month? last-day-of-month?)
(defn first-day-of-month? [date-time]
(time/equal? (time/first-day-of-the-month date-time) date-time))
(def first-day-of-the-month? first-day-of-month?)
(defn nth-day-of-the-month? [date-time n]
(time/equal? (time/nth-day-of-the-month date-time n) date-time))
(defn same-date?
"Compares two date times to see if they are the same date"
[this-date-time that-date-time]
(time/equal? (coerce/to-local-date this-date-time) (coerce/to-local-date that-date-time)))
(ns clj-time.spec
"This namespace requires Clojure 1.9 or later. It defines a set of predicates plus a set of spec defs with associated generators."
(:require [clojure.spec.alpha :as spec]
[clojure.spec.gen.alpha :as gen]
[clj-time.types :refer [date-time? local-date-time? local-date? time-zone?]]
[clj-time.core :refer [date-time]]
[clj-time.coerce :refer [to-date-time to-long]])
(:import [org.joda.time DateTime DateTimeZone LocalDate LocalDateTime]
[org.joda.time.base BaseDateTime]
[java.util TimeZone]))
(def all-time-zones
(delay
(set
(keep #(try (DateTimeZone/forTimeZone (TimeZone/getTimeZone ^String %))
(catch Throwable t nil))
(TimeZone/getAvailableIDs)))))
(defn ^:dynamic *time-zones*
"Dynamically bind this to choose which time zones to use in generators."
[]
(gen/one-of [(gen/return DateTimeZone/UTC)
(spec/gen @all-time-zones)]))
(spec/def ::past (spec/int-in (to-long (date-time 2001 1 1 00 00 00))
(to-long (date-time 2010 12 31 00 00 00))))
(spec/def ::past-and-future (spec/int-in (to-long (date-time 2011 1 1 00 00 00))
(to-long (date-time 2030 12 31 23 59 59))))
(spec/def ::future (spec/int-in (to-long (date-time 2031 1 1 0 00 00))
(to-long (date-time 2040 12 31 23 59 59))))
(defn ^:dynamic *period*
"Dynamically bind this to choose the range of your generated dates."
[]
(spec/gen ::past-and-future))
(spec/def ::time-zone
(spec/with-gen time-zone? *time-zones*))
(spec/def ::date-time
(spec/with-gen date-time?
#(gen/fmap (