Skip to content

Commit

Permalink
Remove usages of clj-time
Browse files Browse the repository at this point in the history
We barely used it, and can use java.time in all cases instead.
  • Loading branch information
tobias committed Apr 13, 2024
1 parent 3286744 commit c061d2b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
1 change: 0 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
cheshire/cheshire {:mvn/version "5.10.1"}
clj-http/clj-http {:mvn/version "3.12.3"}
clj-stacktrace/clj-stacktrace {:mvn/version "0.2.8"}
clj-time/clj-time {:mvn/version "0.15.2"}
com.cemerick/friend {:mvn/version "0.2.3"
:exclusions [;; not used, excluded to address CVE-2007-1652, CVE-2007-1651
org.openid4java/openid4java-nodeps
Expand Down
5 changes: 2 additions & 3 deletions src/clojars/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
[buddy.core.codecs :as buddy.codecs]
[buddy.core.hash :as buddy.hash]
[cemerick.friend.credentials :as creds]
[clj-time.coerce :as time.coerce]
[clj-time.core :as time]
[clojars.config :refer [config]]
[clojars.maven :as mvn]
[clojars.time :as time]
[clojars.util :refer [assoc-some filter-some]]
[clojure.edn :as edn]
[clojure.set :as set]
Expand Down Expand Up @@ -166,7 +165,7 @@
:where [:and
[:= :password_reset_code reset-code]
[:>= :password_reset_code_created_at
(-> 1 time/days time/ago time.coerce/to-sql-date)]]
(Timestamp/from (time/days-ago 1))]]
:limit 1})))

(defn find-user-by-id
Expand Down
35 changes: 35 additions & 0 deletions src/clojars/time.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns clojars.time
(:import
(java.time
Instant)
(java.time.temporal
ChronoUnit)))

(set! *warn-on-reflection* true)

(defn now
"Returns the current time as a j.t.Instant."
^Instant []
(Instant/now))

(defmacro with-now
"Use in tests to set the value returned by `now`."
[now & body]
`(with-redefs [now (constantly ~now)]
~@body))

(defn days-ago
([days]
(days-ago (now) days))
([^Instant instant ^long days]
(.minus instant days ChronoUnit/DAYS)))

(defn days-from
([days]
(days-from (now) days))
([^Instant instant ^long days]
(.plus instant days ChronoUnit/DAYS)))

(defn parse-instant
^Instant [^String instant-string]
(Instant/parse instant-string))
20 changes: 10 additions & 10 deletions src/clojars/tools/process_stats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
"generate usage statistics from web log"
(:gen-class)
(:require
[clj-time.format :as timef]
[clojars.file-utils :as fu]
[clojars.time :as time]
[clojars.util :as util]
[clojure.java.io :as io]
[net.cgrand.regex :as re])
(:import
java.io.BufferedReader
java.util.regex.Pattern))
(java.io
BufferedReader)
(java.util.regex
Pattern)))

(def time-clf (timef/formatter "dd/MMM/YYYY:HH:mm:ss Z"))
(def time-cdn (timef/formatters :date-time-no-ms))
(set! *warn-on-reflection* true)

;; net.cgrand/regex currently doesn't allow Patterns
;; but they're too handy so let's enable them anyway
Expand Down Expand Up @@ -67,7 +68,7 @@
segment \.
[#"\w+" :as :ext])))

(defn is-legacy? [line]
(defn is-legacy? [^String line]
(.contains line " \"-\" "))

(defn parse-path [s]
Expand All @@ -86,17 +87,16 @@
{:status (util/parse-long (:status m))
:method (:method m)
:size (util/parse-long (:size m))
:time (when (:time m) (try (timef/parse time-cdn (:time m))
(catch IllegalArgumentException _)))})))
:time (when (:time m)
(try (time/parse-instant (:time m))
(catch IllegalArgumentException _)))})))

(defn valid-download? [m]
(and m
(= (:status m) 200)
(= (:method m) "GET")
(= (:ext m) "jar")))

(def as-year-month (partial timef/unparse (timef/formatters :year-month)))

(defn compute-stats [lines]
(->> lines
(map parse-line)
Expand Down
7 changes: 3 additions & 4 deletions test/clojars/unit/db_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
(:require
[buddy.core.codecs :as buddy.codecs]
[buddy.core.hash :as buddy.hash]
[clj-time.core :as time]
[clojars.db :as db]
[clojars.test-helper :as help]
[clojars.time :as time]
[clojure.test :refer [are deftest is use-fixtures]]
[matcher-combinators.test])
(:import
Expand Down Expand Up @@ -41,9 +41,8 @@
:user name
:password_reset_code reset-code}
(db/find-user-by-password-reset-code help/*db* reset-code)))

(time/do-at (-> 2 time/days time/from-now)
(is (not (db/find-user-by-password-reset-code help/*db* reset-code)))))))
(time/with-now (time/days-from 2)
(is (not (db/find-user-by-password-reset-code help/*db* reset-code)))))))

(deftest renamed-users-can-be-found
(let [email "test@example.com"
Expand Down
4 changes: 1 addition & 3 deletions test/clojars/unit/tools/process_stats_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns clojars.unit.tools.process-stats-test
(:require
[clj-time.core :as time]
[clojars.tools.process-stats :as stats]
[clojure.java.io :as io]
[clojure.test :refer [deftest is]]))
Expand All @@ -27,8 +26,7 @@
(is (= 200 (:status m)))
(is (= 2377 (:size m)))
(is (= "GET" (:method m)))
(is (= 14 (time/day (:time m))))
(is (= 40 (time/minute (:time m))))
(is (= (.toInstant #inst "2012-04-14T06:40:59Z") (:time m)))
(is (= "haddock" (:name m)))
(is (= "captain.archibald" (:group m)))
(is (= "0.1.0" (:version m)))
Expand Down

0 comments on commit c061d2b

Please sign in to comment.