Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created December 5, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelrinaldi/494cecf90496350cfc0f9f470a9a9b30 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/494cecf90496350cfc0f9f470a9a9b30 to your computer and use it in GitHub Desktop.
(ns api-fetchers.utils
(:require [clj-time.format :as format-time]
[taoensso.timbre :as timbre :refer [info]])
(:import java.text.SimpleDateFormat
[java.time Duration Instant]
java.util.zip.ZipInputStream))
(defn unzip
"Returns a `ZipInputStream` with the contents of `input-stream`"
[input-stream]
(-> input-stream
(ZipInputStream.)
(.getNextEntry)))
(defn clean-up
[tmp-file]
(info "Removing" (str (.getAbsolutePath tmp-file) "..."))
(.delete tmp-file)
(info "Tempfile removed."))
(defn format-date
"Converts `date` from string to a Java `Date` instance.
Optionally pass in a date pattern as a second argument."
([date] (format-date date "yyyy-MM-dd"))
([date format]
(-> (SimpleDateFormat. format) (.parse date))))
(defn format-date-iso
"Converts `date` from string to a ISO 8601 compliant date format.
Optionally pass in a date pattern as a second argument."
([date] (format-date-iso date "yyyy-MM-dd"))
([date format]
(-> (format-time/parse (format-time/formatter format) date) (.toString))))
(defn now []
(Instant/now))
(defn duration [start end]
(-> (Duration/between start end)
(.toMillis)
(Integer/valueOf)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment