summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Benediktsson <mrjbq7@gmail.com>2024-05-01 20:53:09 -0700
committerJohn Benediktsson <mrjbq7@gmail.com>2024-05-01 20:58:26 -0700
commit7cc710ab0f34496022db7998c7c9d0b4841caa6d (patch)
tree6d596f849fc5b83b8ab9e17fa7bd74bc0865f8b7
parent37103d3014eef005b8601bcb670fa72e17acb936 (diff)
calendar.format: make duration>human-readable more human readableclean-linux-x86-32
-rw-r--r--basis/calendar/format/format-tests.factor7
-rw-r--r--basis/calendar/format/format.factor49
2 files changed, 43 insertions, 13 deletions
diff --git a/basis/calendar/format/format-tests.factor b/basis/calendar/format/format-tests.factor
index f502f98568..cde4e5e037 100644
--- a/basis/calendar/format/format-tests.factor
+++ b/basis/calendar/format/format-tests.factor
@@ -90,3 +90,10 @@ Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
{ "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
+
+{ "1 minute" } [ 60 seconds duration>human-readable ] unit-test
+{ "1 hour" } [ 1 hours duration>human-readable ] unit-test
+{ "3 hours" } [ 3 hours duration>human-readable ] unit-test
+{ "2 minutes and 3 seconds" } [ 123 seconds duration>human-readable ] unit-test
+{ "20 minutes and 34 seconds" } [ 1234 seconds duration>human-readable ] unit-test
+{ "3 hours, 25 minutes and 45 seconds" } [ 12345 seconds duration>human-readable ] unit-test
diff --git a/basis/calendar/format/format.factor b/basis/calendar/format/format.factor
index e3da029bcc..a85f24af72 100644
--- a/basis/calendar/format/format.factor
+++ b/basis/calendar/format/format.factor
@@ -205,20 +205,43 @@ M: timestamp present timestamp>string ;
: duration>human-readable ( duration -- string )
[
- [
- duration>years >integer
- [
- [ number>string write ]
- [ 1 > " years, " " year, " ? write ] bi
- ] unless-zero
- ] [
- duration>days >integer 365 mod
+ {
[
- [ number>string write ]
- [ 1 > " days, " " day, " ? write ] bi
- ] unless-zero
- ] [ duration>hms write ] tri
- ] with-string-writer ;
+ duration>years >integer
+ [
+ [ number>string ]
+ [ 1 > " years" " year" ? append , ] bi
+ ] unless-zero
+ ] [
+ duration>days >integer 365 mod
+ [
+ [ number>string ]
+ [ 1 > " days" " day" ? append , ] bi
+ ] unless-zero
+ ] [
+ duration>hours >integer 24 mod
+ [
+ [ number>string ]
+ [ 1 > " hours" " hour" ? append , ] bi
+ ] unless-zero
+ ] [
+ duration>minutes >integer 60 mod
+ [
+ [ number>string ]
+ [ 1 > " minutes" " minute" ? append , ] bi
+ ] unless-zero
+ ] [
+ duration>seconds >integer 60 mod
+ [
+ number>string " seconds" append ,
+ ] unless-zero
+ ]
+ } cleave
+ ] { } make [ "0 seconds" ] [
+ unclip-last-slice over empty? [ nip ] [
+ [ ", " join ] [ " and " glue ] bi*
+ ] if
+ ] if-empty ;
GENERIC: elapsed-time ( seconds -- string )