Last active
July 1, 2024 14:08
-
-
Save henryw374/2291e787087eeea513f9a8e5a5bd6f69 to your computer and use it in GitHub Desktop.
mutable-clock java.time clock clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns mutable-clock | |
(:import (java.time Clock Duration Instant ZoneId))) | |
(definterface MutableClock | |
(setZone [zone]) | |
(setInstant [instant])) | |
(defn mutable-clock [_instant _zone] | |
(let [instant-atom (atom _instant) | |
zone-atom (atom _zone)] | |
(proxy [Clock mutable_clock.MutableClock] [] | |
(getZone [] @zone-atom) | |
(instant [] @instant-atom) | |
(setZone [zone] (reset! zone-atom zone)) | |
(setInstant [instant] (reset! instant-atom instant))))) | |
(defn set-zone [mutable-clock zone] | |
(.setZone mutable-clock zone)) | |
(defn set-instant [mutable-clock instant] | |
(.setInstant mutable-clock instant)) | |
(defn advance-clock [mutable-clock ^Duration duration] | |
(set-instant mutable-clock | |
(.plus ^Instant (.instant mutable-clock) duration))) | |
(comment | |
(require '[clojure.reflect :as r]) | |
(r/reflect MutableClock) | |
(def x (-> (mutable-clock (Instant/now) | |
(ZoneId/systemDefault)))) | |
(.instant x) | |
(.getZone x) | |
(set-zone x (ZoneId/of "Europe/Paris")) | |
(set-instant x (Instant/now)) | |
(advance-clock x (Duration/ofDays 1)) | |
(definterface Foo | |
(bar [])) | |
(-> (proxy [Clock mutable_clock.Foo] [] | |
(bar []) | |
(getZone [])) | |
(.getZone)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment