|
2671 | 2671 |
|
2672 | 2672 | (comment (format-inst (now-inst))) |
2673 | 2673 |
|
2674 | | -;;;; Date & time |
| 2674 | +;;;; M/secs |
2675 | 2675 |
|
2676 | 2676 | (defn secs->ms ^long [secs] (* (long secs) 1000)) |
2677 | 2677 | (defn ms->secs ^long [ms] (quot (long ms) 1000)) |
|
2718 | 2718 |
|
2719 | 2719 | (comment (macroexpand '(msecs :weeks 3))) |
2720 | 2720 |
|
2721 | | -#?(:clj |
2722 | | - (defn- -simple-date-format |
2723 | | - "Returns a SimpleDateFormat ThreadLocal proxy." |
2724 | | - [pattern locale timezone] |
2725 | | - (let [pattern |
2726 | | - (case pattern |
2727 | | - :iso8601 "yyyy-MM-dd'T'HH:mm:ss.SSSX" |
2728 | | - :rss2 "EEE, dd MMM yyyy HH:mm:ss z" |
2729 | | - pattern) |
2730 | | - |
2731 | | - locale |
2732 | | - (if (identical-kw? locale :jvm-default) |
2733 | | - nil ; (Locale/getDefault) |
2734 | | - locale) |
2735 | | - |
2736 | | - timezone |
2737 | | - (if (identical-kw? timezone :jvm-default) |
2738 | | - nil ; (TimeZone/getDefault) |
2739 | | - (if (identical-kw? timezone :utc) |
2740 | | - (TimeZone/getTimeZone "UTC") |
2741 | | - timezone))] |
2742 | | - |
2743 | | - (threadlocal |
2744 | | - (let [^SimpleDateFormat sdf |
2745 | | - (if locale |
2746 | | - (SimpleDateFormat. ^String pattern ^Locale locale) |
2747 | | - (SimpleDateFormat. ^String pattern))] |
2748 | | - (when timezone (.setTimeZone sdf ^TimeZone timezone)) |
2749 | | - sdf))))) |
2750 | | - |
2751 | | -#?(:clj |
2752 | | - (defn simple-date-format* |
2753 | | - ^java.text.SimpleDateFormat [pattern locale timezone] |
2754 | | - (.get ^ThreadLocal (-simple-date-format pattern locale timezone)))) |
2755 | | - |
2756 | | -#?(:clj |
2757 | | - (defn simple-date-format "Returns a thread-local `java.text.SimpleDateFormat`." |
2758 | | - ^java.text.SimpleDateFormat [pattern & [{:keys [locale timezone] :as opts}]] |
2759 | | - (.get ^ThreadLocal (-simple-date-format pattern locale timezone)))) |
2760 | | - |
2761 | | -(comment (qb 1e5 (.format (simple-date-format "yyyy-MMM-dd") (Date.)))) |
2762 | | - |
2763 | 2721 | ;;;; Strings |
2764 | 2722 |
|
2765 | 2723 | (def ^:const a-utf8-str |
|
3139 | 3097 | "Private, don't use." |
3140 | 3098 | [n-min-fd n-max-fd] |
3141 | 3099 | #?(:clj |
3142 | | - (let [nf-proxy |
| 3100 | + (let [tl:nf |
3143 | 3101 | (threadlocal |
3144 | 3102 | (let [nf (java.text.NumberFormat/getInstance java.util.Locale/US)] |
3145 | 3103 | (when (instance? java.text.DecimalFormat nf) |
|
3152 | 3110 | (.setDecimalSeparator \.) |
3153 | 3111 | (.setGroupingSeparator \,)))))))] |
3154 | 3112 |
|
3155 | | - (fn [n] (.format ^java.text.NumberFormat (.get nf-proxy) n))) |
| 3113 | + (fn [n] (.format ^java.text.NumberFormat (.get tl:nf) n))) |
3156 | 3114 |
|
3157 | 3115 | :cljs |
3158 | 3116 | (let [nf |
|
3632 | 3590 | (ReseedingSRNG. (java.security.SecureRandom/getInstanceStrong) 0) |
3633 | 3591 | (ReseedingSRNG. (java.security.SecureRandom/getInstance "SHA1SRNG") 0))) |
3634 | 3592 |
|
3635 | | - (def ^:private rsrng* (threadlocal (reseeding-srng))) |
3636 | | - |
3637 | | - (defn secure-rng |
3638 | | - "Returns an auto-reseeding thread-local `java.security.SecureRandom`. |
3639 | | - Favours security over performance. May block while waiting on entropy!" |
3640 | | - ^java.security.SecureRandom [] ((.get ^ThreadLocal rsrng*))) |
| 3593 | + (let [tl:rsrng (threadlocal (reseeding-srng))] |
| 3594 | + (defn secure-rng |
| 3595 | + "Returns an auto-reseeding thread-local `java.security.SecureRandom`. |
| 3596 | + Favours security over performance. May block while waiting on entropy!" |
| 3597 | + ^java.security.SecureRandom [] ((.get tl:rsrng)))) |
3641 | 3598 |
|
3642 | 3599 | (defn secure-rng-mock!!! |
3643 | 3600 | "Returns **INSECURE** `java.security.SecureRandom` mock instance backed by |
|
4993 | 4950 |
|
4994 | 4951 | (comment (let [rl (rolling-list 3), c (counter)] [(qb 1e6 (rl (c))) (vec (rl))])) ; 98.36 |
4995 | 4952 |
|
| 4953 | +;;;; Date & time |
| 4954 | + |
| 4955 | +#?(:clj |
| 4956 | + (let [get-tl |
| 4957 | + (fmemoize |
| 4958 | + (fn [pattern locale timezone] |
| 4959 | + (threadlocal |
| 4960 | + (let [pattern |
| 4961 | + (case pattern |
| 4962 | + :iso8601 "yyyy-MM-dd'T'HH:mm:ss.SSSX" |
| 4963 | + :rss2 "EEE, dd MMM yyyy HH:mm:ss z" |
| 4964 | + pattern) |
| 4965 | + |
| 4966 | + locale |
| 4967 | + (if (identical-kw? locale :jvm-default) |
| 4968 | + nil #_(Locale/getDefault) |
| 4969 | + locale) |
| 4970 | + |
| 4971 | + timezone |
| 4972 | + (if (identical-kw? timezone :jvm-default) |
| 4973 | + nil #_(TimeZone/getDefault) |
| 4974 | + (if (identical-kw? timezone :utc) |
| 4975 | + (TimeZone/getTimeZone "UTC") |
| 4976 | + timezone)) |
| 4977 | + |
| 4978 | + ^SimpleDateFormat sdf |
| 4979 | + (if locale |
| 4980 | + (SimpleDateFormat. ^String pattern ^Locale locale) |
| 4981 | + (SimpleDateFormat. ^String pattern))] |
| 4982 | + |
| 4983 | + (when timezone (.setTimeZone sdf ^TimeZone timezone)) |
| 4984 | + (do sdf)))))] |
| 4985 | + |
| 4986 | + (defn simple-date-format* |
| 4987 | + "Returns a thread-local `java.text.SimpleDateFormat`." |
| 4988 | + ^java.text.SimpleDateFormat [pattern locale timezone] |
| 4989 | + (.get ^ThreadLocal (get-tl pattern locale timezone))) |
| 4990 | + |
| 4991 | + (defn simple-date-format |
| 4992 | + "Returns a thread-local `java.text.SimpleDateFormat`." |
| 4993 | + ^java.text.SimpleDateFormat [pattern & [{:keys [locale timezone] :as opts}]] |
| 4994 | + (.get ^ThreadLocal (get-tl pattern locale timezone))))) |
| 4995 | + |
| 4996 | +(comment (qb 1e5 (.format (simple-date-format "yyyy-MMM-dd") (Date.)))) |
| 4997 | + |
4996 | 4998 | ;;;; Sorting |
4997 | 4999 |
|
4998 | 5000 | #?(:cljs (defn rcompare "Reverse comparator." [x y] (compare y x)) |
|
0 commit comments