]> gitweb.factorcode.org Git - factor.git/commitdiff
calendar.elapsed: moving words to calendar.format.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 10 Dec 2020 20:55:10 +0000 (12:55 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 10 Dec 2020 20:55:10 +0000 (12:55 -0800)
basis/calendar/format/format-tests.factor
basis/calendar/format/format.factor
extra/calendar/elapsed/authors.txt [deleted file]
extra/calendar/elapsed/elapsed-tests.factor [deleted file]
extra/calendar/elapsed/elapsed.factor [deleted file]
extra/calendar/elapsed/tags.txt [deleted file]
extra/hacker-news/hacker-news.factor
extra/reddit/reddit.factor

index ba74698d99c4bad70c982d942fc4c0e3d938a18f..56f30653373c18ca4fe82fca14bfd0b432367f96 100644 (file)
@@ -1,4 +1,4 @@
-USING: accessors calendar calendar.format sequences tools.test ;
+USING: accessors calendar calendar.format kernel sequences tools.test ;
 IN: calendar.format.tests
 
 CONSTANT: testtime T{ timestamp
@@ -21,3 +21,17 @@ CONSTANT: testtime T{ timestamp
 { "03:01:59" } [
     3 hours 1 >>minute 59 >>second duration>hms
 ] unit-test
+
+[ -1 elapsed-time ] [ "negative seconds" = ] must-fail-with
+
+{ "0s" } [ 0 elapsed-time ] unit-test
+{ "59s" } [ 59 elapsed-time ] unit-test
+{ "1m" } [ 60 elapsed-time ] unit-test
+{ "1m 1s" } [ 61 elapsed-time ] unit-test
+{ "2y 1w 6d 2h 59m 23s" } [ 64033163 elapsed-time ] unit-test
+
+{ "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
+{ "about a minute ago" } [ 90 relative-time ] unit-test
+{ "4 minutes ago" } [ 270 relative-time ] unit-test
index 6c602008c4c20ada349b2e5d3ec093f9760a474f..617fff66f86770cf0cad5259f7aacad145c05abe 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors arrays calendar calendar.english combinators io
-io.streams.string kernel math math.parser math.parser.private
-present quotations sequences words ;
+USING: accessors arrays calendar calendar.english combinators
+formatting io io.streams.string kernel make math math.parser
+math.parser.private present quotations sequences words ;
 IN: calendar.format
 
 MACRO: formatted ( spec -- quot )
@@ -207,3 +207,59 @@ M: timestamp present timestamp>string ;
             ] unless-zero
         ] [ duration>hms write ] tri
     ] with-string-writer ;
+
+GENERIC: elapsed-time ( seconds -- string )
+
+M: integer elapsed-time
+    dup 0 < [ "negative seconds" throw ] when [
+        {
+            { 60 "s" }
+            { 60 "m" }
+            { 24 "h" }
+            {  7 "d" }
+            { 52 "w" }
+            {  f "y" }
+        } [
+            [ first [ /mod ] [ dup ] if* ] [ second ] bi swap
+            dup 0 > [ number>string prepend , ] [ 2drop ] if
+        ] each drop
+    ] { } make [ "0s" ] [ reverse " " join ] if-empty ;
+
+M: real elapsed-time
+    >integer elapsed-time ;
+
+M: duration elapsed-time
+    duration>seconds elapsed-time ;
+
+M: timestamp elapsed-time
+    now swap time- 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 /i "%d minutes" sprintf ] }
+        { [ dup 7200 < ] [ drop "about an hour" ] }
+        { [ dup 86400 < ] [ 3600 /i "%d hours" sprintf ] }
+        { [ dup 172800 < ] [ drop "1 day" ] }
+        [ 86400 /i "%d days" sprintf ]
+    } cond ;
+
+GENERIC: relative-time ( seconds -- string )
+
+M: real relative-time
+    [ 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 ;
diff --git a/extra/calendar/elapsed/authors.txt b/extra/calendar/elapsed/authors.txt
deleted file mode 100644 (file)
index e091bb8..0000000
+++ /dev/null
@@ -1 +0,0 @@
-John Benediktsson
diff --git a/extra/calendar/elapsed/elapsed-tests.factor b/extra/calendar/elapsed/elapsed-tests.factor
deleted file mode 100644 (file)
index e3faae7..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-! Copyright (C) 2010 John Benediktsson
-! See http://factorcode.org/license.txt for BSD license
-
-USING: calendar calendar.elapsed kernel tools.test ;
-
-[ -1 elapsed-time ] [ "negative seconds" = ] must-fail-with
-
-{ "0s" } [ 0 elapsed-time ] unit-test
-{ "59s" } [ 59 elapsed-time ] unit-test
-{ "1m" } [ 60 elapsed-time ] unit-test
-{ "1m 1s" } [ 61 elapsed-time ] unit-test
-{ "2y 1w 6d 2h 59m 23s" } [ 64033163 elapsed-time ] unit-test
-
-{ "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
-{ "about a minute ago" } [ 90 relative-time ] unit-test
-{ "4 minutes ago" } [ 270 relative-time ] unit-test
diff --git a/extra/calendar/elapsed/elapsed.factor b/extra/calendar/elapsed/elapsed.factor
deleted file mode 100644 (file)
index 830f232..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-! Copyright (C) 2010 John Benediktsson
-! See http://factorcode.org/license.txt for BSD license
-
-USING: calendar combinators formatting kernel make math
-math.parser sequences ;
-
-IN: calendar.elapsed
-
-GENERIC: elapsed-time ( seconds -- string )
-
-M: integer elapsed-time
-    dup 0 < [ "negative seconds" throw ] when [
-        {
-            { 60 "s" }
-            { 60 "m" }
-            { 24 "h" }
-            {  7 "d" }
-            { 52 "w" }
-            {  f "y" }
-        } [
-            [ first [ /mod ] [ dup ] if* ] [ second ] bi swap
-            dup 0 > [ number>string prepend , ] [ 2drop ] if
-        ] each drop
-    ] { } make [ "0s" ] [ reverse " " join ] if-empty ;
-
-M: real elapsed-time
-    >integer elapsed-time ;
-
-M: duration elapsed-time
-    duration>seconds elapsed-time ;
-
-M: timestamp elapsed-time
-    now swap time- 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 /i "%d minutes" sprintf ] }
-        { [ dup 7200 < ] [ drop "about an hour" ] }
-        { [ dup 86400 < ] [ 3600 /i "%d hours" sprintf ] }
-        { [ dup 172800 < ] [ drop "1 day" ] }
-        [ 86400 /i "%d days" sprintf ]
-    } cond ;
-
-GENERIC: relative-time ( seconds -- string )
-
-M: real relative-time
-    [ 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 ;
diff --git a/extra/calendar/elapsed/tags.txt b/extra/calendar/elapsed/tags.txt
deleted file mode 100644 (file)
index 6abb415..0000000
+++ /dev/null
@@ -1 +0,0 @@
-time
index 66f33f109e8784d34cba4492de9ea4c3168a19c9..276274bc7967883ed0768dfe28d8b6ec2b6a1ca1 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2012 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 
-USING: accessors assocs calendar calendar.elapsed
+USING: accessors assocs calendar calendar.format
 colors.constants colors.hex combinators concurrency.combinators
 formatting fry hashtables http.client io io.styles json.reader
 kernel make math math.parser sequences ui urls vocabs ;
index fcd5d9c92d17c509def4e137b3db1fccc7103cad..a85a00f8a66a167f08ded6b8633a675852110be6 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2011-2012 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: accessors assocs calendar calendar.elapsed
+USING: accessors assocs calendar calendar.format
 colors.constants colors.hex combinators formatting fry
 http.client io io.styles json json.reader kernel make math
 sequences urls ;