     [com.sixsq.slipstream.ssclj.usage.record-keeper :as rc]
     [com.sixsq.slipstream.ssclj.db.impl :as db]
     [com.sixsq.slipstream.ssclj.db.database-binding :as dbdb]
-    [com.sixsq.slipstream.ssclj.usage.utils :as u]
     [clj-time.core :as time]))
 
-(defn days-from-now
-  [n]
-  (str (time/plus (time/today-at-midnight) (time/days n))))
+(defn days-ago-at-hour
+  ([n h]
+   (str (time/minus (time/today-at h 0 0 0) (time/days n))))
+  ([n]
+   (days-ago-at-hour n 0)))
 
 (defn- daily-usage
   [username cloud day-number metrics-map]
   {:user            username
    :cloud           cloud
    :frequency       "daily"
-   :start_timestamp (days-from-now day-number)
-   :end_timestamp   (days-from-now (inc day-number))
+   :start_timestamp (days-ago-at-hour day-number)
+   :end_timestamp   (days-ago-at-hour (inc day-number))
    :usage           (->> metrics-map
                          (map (fn [[k v]] {k {:unit_minutes v}}))
                          (into {}))})

  {:user            username
   :cloud           cloud
   :frequency       "monthly"
   :start_timestamp (days-from-now (* 30 day-number))
   :end_timestamp   (days-from-now (* 30 (inc day-number)))
   :usage           (->> {:ram 97185920 :disk 950.67 :cpu 9250}
                         (map (fn [[k v]] {k {:unit_minutes v}}))
                         (into {}))})

  {:user            username
   :cloud           cloud
   :frequency       "weekly"
   :start_timestamp (days-ago-at-hour (* 7 day-number))
   :end_timestamp   (days-ago-at-hour (* 7 (inc day-number)))
   :usage           (->> {:ram 571859200 :disk 5000.67 :cpu 52500}
                         (map (fn [[k v]] {k {:unit_minutes v}}))
                         (into {}))})
