]> gitweb.factorcode.org Git - factor.git/commitdiff
calendar.elapsed: make generic for more convenience.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 30 Jul 2015 15:56:55 +0000 (08:56 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 30 Jul 2015 15:56:55 +0000 (08:56 -0700)
extra/calendar/elapsed/elapsed.factor

index 47a30ad21e6b71da20b6a992c2dbc6726e4481a8..efc121a6b90e5da5ae2d0f88946c3fc1452a44c2 100644 (file)
@@ -1,12 +1,14 @@
 ! Copyright (C) 2010 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: combinators formatting kernel make math math.parser
-sequences ;
+USING: calendar combinators formatting kernel make math
+math.parser sequences ;
 
 IN: calendar.elapsed
 
-: elapsed-time ( seconds -- string )
+GENERIC: elapsed-time ( seconds -- string )
+
+M: real elapsed-time
     dup 0 < [ "negative seconds" throw ] when [
         {
             { 60 "s" }
@@ -21,7 +23,12 @@ IN: calendar.elapsed
         ] each drop
     ] { } make [ "0s" ] [ reverse " " join ] if-empty ;
 
-: relative-time ( seconds -- string )
+M: duration elapsed-time
+    duration>seconds elapsed-time ;
+
+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" ] }
@@ -32,3 +39,9 @@ IN: calendar.elapsed
         { [ dup 172800 < ] [ drop "1 day ago" ] }
         [ 86400 / "%d days ago" sprintf ]
     } cond ;
+
+M: duration relative-time
+    duration>seconds relative-time ;
+
+M: timestamp relative-time
+    now swap time- relative-time ;