]> gitweb.factorcode.org Git - factor.git/commitdiff
calendar.elapsed: Support relative times in the future with "3 hours hence" etc.
authorDoug Coleman <doug.coleman@gmail.com>
Thu, 30 Jul 2015 16:10:10 +0000 (09:10 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Thu, 30 Jul 2015 16:10:10 +0000 (09:10 -0700)
Add unix-time>relative-time word for convenience.

extra/calendar/elapsed/elapsed-tests.factor
extra/calendar/elapsed/elapsed.factor

index adffeeb0c8def63d5b641a8e3ebcb60243bfafa1..881c61451db8eb25440d1d0f2b46561e5d5c54e7 100644 (file)
@@ -13,8 +13,6 @@ IN: calendar.elapsed.test
 { "1m 1s" } [ 61 elapsed-time ] unit-test
 { "2y 1w 6d 2h 59m 23s" } [ 64033163 elapsed-time ] unit-test
 
-[ -1 relative-time ] [ "negative seconds" = ] must-fail-with
-
 { "just now" } [ 0 relative-time ] unit-test
 { "less than a minute ago" } [ 10 relative-time ] unit-test
 { "about a minute ago" } [ 60 relative-time ] unit-test
index efc121a6b90e5da5ae2d0f88946c3fc1452a44c2..9f9c375ab31566297157357e75fdecf3167e29a4 100644 (file)
@@ -26,22 +26,42 @@ M: real elapsed-time
 M: duration elapsed-time
     duration>seconds elapsed-time ;
 
+! XXX: Anything up to 2 hours is "about an hour"
+: relative-time-offset ( seconds -- string )
+    abs {
+        { [ dup 1 < ] [ drop "just now" ] }
+        { [ dup 60 < ] [ drop "less than a minute" ] }
+        { [ dup 120 < ] [ drop "about a minute" ] }
+        { [ dup 2700 < ] [ 60 / "%d minutes" sprintf ] }
+        { [ dup 7200 < ] [ drop "about an hour" ] }
+        { [ dup 86400 < ] [ 3600 /i "%d hours" sprintf ] }
+        { [ dup 172800 < ] [ drop "1 day" ] }
+        [ 86400 / "%d days" sprintf ]
+    } cond ;
+
 GENERIC: relative-time ( seconds -- string )
 
 M: real relative-time
-    dup 0 < [ "negative seconds" throw ] when {
-        { [ dup 1 < ] [ drop "just now" ] }
-        { [ dup 60 < ] [ drop "less than a minute ago" ] }
-        { [ dup 120 < ] [ drop "about a minute ago" ] }
-        { [ dup 2700 < ] [ 60 / "%d minutes ago" sprintf ] }
-        { [ dup 5400 < ] [ drop "about an hour ago" ] }
-        { [ dup 86400 < ] [ 3600 / "%d hours ago" sprintf ] }
-        { [ dup 172800 < ] [ drop "1 day ago" ] }
-        [ 86400 / "%d days ago" sprintf ]
-    } cond ;
+    [ relative-time-offset ] [
+        dup abs 1 < [
+            drop
+        ] [
+            0 < "hence" "ago" ? " " glue
+        ] if
+    ] bi ;
 
 M: duration relative-time
     duration>seconds relative-time ;
 
 M: timestamp relative-time
     now swap time- relative-time ;
+
+
+GENERIC: unix-time>relative-time ( unix-seconds -- string )
+
+M: real unix-time>relative-time
+    seconds since-1970 unix-time>relative-time ;
+
+M: timestamp unix-time>relative-time
+    [ now ] dip
+    time- duration>seconds relative-time ;