Skip to content

Commit

Permalink
Nicer dates in search result - closes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarmo committed Mar 19, 2016
1 parent 299d085 commit bbf7145
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
35 changes: 35 additions & 0 deletions sushy/utils.hy
Expand Up @@ -2,6 +2,7 @@
[collections [OrderedDict]]
[base64 [urlsafe-b64encode]]
[bottle [request response]]
[calendar [timegm]]
[datetime [datetime]]
[dateutil.parser [parse :as parse-date]]
[functools [wraps]]
Expand Down Expand Up @@ -37,6 +38,14 @@
"20:30-21:29" "at dinnertime"
"21:30-22:29" "at night"
"22:30-23:59" "late night"})

(def *readable-intervals*
{31556926 "year"
2592000 "month"
604800 "week"
86400 "day"
3600 "hour"
60 "minute"})

(setv *utc* (timezone "UTC"))

Expand Down Expand Up @@ -200,13 +209,39 @@


(defn fuzzy-time [date]
; describes a date as a time of day
(let [[when (.strftime date "%H:%M")]]
(.get
*time-intervals*
(.next (ifilter (fn [x] (let [[(, l u) (.split x "-")]] (and (<= l when) (<= when u))))
(sorted (.keys *time-intervals*))))
"sometime")))


(defn time-chunks [begin-interval &optional [end-interval nil]]
; breaks down a time interval into a sequence of time chunks
(let [[chunks (apply sorted [(.keys *readable-intervals*)] {"reverse" true})]
[the-end (if end-interval end-interval (datetime.now))]
[interval (- (timegm (.timetuple the-end)) (timegm (.timetuple begin-interval)))]
[values []]]
(for [i chunks]
(setv (, d r) (divmod interval i))
(.append values (, (int d) (.get *readable-intervals* i)))
(setv interval r))
(filter (fn [x] (pos? (get x 0))) values)))


(defn string-plurals [chunk]
(let [[(, v s) chunk]]
(.join " " (map str (, v (if (> v 1) (+ s "s") s))))))


(defn time-since [begin-interval &optional [end-interval nil]]
(let [[chunks (list (map string-plurals (time-chunks begin-interval end-interval)))]]
(if (not (len (list chunks)))
"sometime"
(.join ", " (take 2 chunks)))))


(defmacro timeit [block iterations]
`(let [[t (time)]]
Expand Down
5 changes: 2 additions & 3 deletions themes/blog/views/search.tpl
@@ -1,5 +1,6 @@
<%
from itertools import islice
from sushy.utils import time_since
if defined("query"):
headers['title'] = "Search results for '%s'" % query
items = list(islice(results,0,20))
Expand All @@ -12,7 +13,6 @@ if defined("query"):
<table class="hover alternating">
<thead>
<tr>
<th>Score</th>
<th>Page</th>
<th>Content</th>
<th>Modified</th>
Expand All @@ -21,10 +21,9 @@ if defined("query"):
<tbody>
%for i in items:
<tr>
<td>{{!i["score"]}}</td>
<td><a href="{{!page_route_base}}/{{!i["name"]}}">{{!i["title"]}}</td>
<td>{{!i["content"]}}</td>
<td>{{!i["mtime"]}}</td>
<td>{{!time_since(i["mtime"])}} ago</td>
</tr>
%end
</tbody>
Expand Down

0 comments on commit bbf7145

Please sign in to comment.