When saving datetimes to databases like MySQL, it is best to save them as UTC instead of using your local timezone. That way, if your company, or IT department, or servers move to a new timezone, you won’t have to worry about adjusting every time stored in the database to match the new timezone. The trick is that you always return UTC times in your APIs, and then expect the clients or tools using your APIs to convert the provided UTC time to their local timezone for display.
This leads to a common question. How do I get the current datetime in UTC when using Clojure?
1 2 3 4 5 6 7 8 9 10 11 12 |
(defn utc-now "Get now in UTC for use in a datetime for a database." [] (let [*formated* (new java.text.SimpleDateFormat "yyyy-MM-dd hh:mm:ss")] (.setTimeZone *formated* (java.util.TimeZone/getTimeZone "UTC")) (.format *formated* (java.util.Date.)))) |
The formatted string with the UTC time will be yyyy-MM-dd hh:mm:ss.