calendar.elapsed: make generic for more convenience.

db4
John Benediktsson 2015-07-30 08:56:55 -07:00
parent 8ea621e090
commit 4e672304b7
1 changed files with 17 additions and 4 deletions

View File

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