Permalink
Browse files

Added #:format argument to {time|seconds}->ago-string.

  • Loading branch information...
1 parent ad46fd0 commit e7d54da54022492e14c718373e188b499233d9d7 @davegurnell davegurnell committed Mar 13, 2009
Showing with 39 additions and 33 deletions.
  1. +3 −1 info.ss
  2. +6 −3 scribblings/time.scrbl
  3. +10 −2 time-test.ss
  4. +20 −27 time.ss
View
@@ -10,7 +10,9 @@
(define release-notes
'((p "Changes and additions:")
(ul (li "added " (tt "dotted-identifier?") ", " (tt "simple-dotted-identifier?") ", "
- (tt "dotted-identifier-count") " and " (tt "dotted-identifier-split") "."))))
+ (tt "dotted-identifier-count") " and " (tt "dotted-identifier-split") ".")
+ (li "added optional " (tt "#:format") " argument to control the text formatting in "
+ (tt "time->ago-string") " and " (tt "seconds->ago-string") ";"))))
(define scribblings
'(("scribblings/unlib.scrbl" (multi-page))))
@@ -63,11 +63,14 @@ Returns a symbol representing the day of the week on @scheme[date].}
Returns @scheme[#t] if @scheme[date] is a Monday, Tuesday, Wednesday or Friday.}
-@defproc[(time->ago-string [then (U time-tai time-utc)] [now (U time-tai time-utc) (current-time (U time-tai time-utc))]) string?]{
+@defproc[(time->ago-string [then (U time-tai time-utc)]
+ [now (U time-tai time-utc) (current-time (time-type then))]
+ [#:format format-string string? "~a ~a ago"]) string?]{
+Given the time of an event the past (and, optionally, another argument representing the current time), returns a textual description of the time passed since the event. Raises exn:fail:unlib if @scheme[now] is before @scheme[then]. See @scheme[seconds->ago-string] for examples.
-Given the time of an event the past (and, optionally, another argument representing the current time), returns a textual description of the time passed since the event. Raises exn:fail:unlib if @scheme[now] is before @scheme[then]. See @scheme[seconds->ago-string] for examples.}
+The optional @scheme[format-string] should have two wildcards in it: one for the number (1, 2, 59) and one for the unit (seconds, minutes, days). The format string is ignored if the result is @scheme["yesterday"].}
-@defproc[(seconds->ago-string [secs integer?] [now integer? (current-seconds)]) string?]{
+@defproc[(seconds->ago-string [secs integer?] [now integer? (current-seconds)] [#:format format-string string? "~a ~a ago"]) string?]{
Like @scheme[time->ago-string] but @scheme[then] and @scheme[now] are integer values like those output by @scheme[current-seconds].
View
@@ -130,7 +130,9 @@
(check-equal? (seconds->ago-string (- test-timestamp 3600) test-timestamp) "1 hour ago")
(check-equal? (seconds->ago-string (- test-timestamp 86399) test-timestamp) "23 hours ago")
(check-equal? (seconds->ago-string (- test-timestamp 86400) test-timestamp) "yesterday")
- (check-equal? (seconds->ago-string (- test-timestamp 172800) test-timestamp) "2 days ago")))
+ (check-equal? (seconds->ago-string (- test-timestamp 172800) test-timestamp) "2 days ago")
+ (check-equal? (seconds->ago-string (- test-timestamp 172800) test-timestamp #:format "~a ~a old") "2 days old")
+ (check-equal? (seconds->ago-string (- test-timestamp 86400) test-timestamp #:format "~a ~a old") "yesterday")))
(test-case "time->ago-string"
(let ([test-timestamp (current-seconds)])
@@ -149,7 +151,13 @@
(make-time time-type 0 (- test-timestamp 3600))
(make-time time-type 0 test-timestamp))
"1 hour ago"
- (format "check 3, type ~a" time-type)))
+ (format "check 3, type ~a" time-type))
+ (check-equal? (time->ago-string
+ (make-time time-type 0 (- test-timestamp 3600))
+ (make-time time-type 0 test-timestamp)
+ #:format "~a ~a old")
+ "1 hour old"
+ (format "check 4, type ~a" time-type)))
(list time-tai time-utc))))
(test-case "time->ago-string : mixture of time-tai and time-utc"
View
@@ -118,7 +118,7 @@
[else (raise-exn exn:fail:contract
(format "Month out of range: ~a" month))]))
-; integer [integer] -> string
+; integer [integer] [#:format string] -> string
;
; Takes an integer seconds value (like the value returned by current-seconds) and,
; optionally, a second argument representing the current seconds, and returns a string like:
@@ -127,14 +127,14 @@
; n minute(s) ago
; n hour(s) ago
; n day(s) ago
-(define (seconds->ago-string then [now (current-seconds)])
+(define (seconds->ago-string then [now (current-seconds)] #:format [format-string "~a ~a ago"])
; (integer string -> string)
(define (make-answer number unit)
(if (= number 1)
(if (equal? unit "day")
"yesterday"
- (format "~a ~a ago" number unit))
- (format "~a ~as ago" number unit)))
+ (format format-string number unit))
+ (format format-string number (format "~as" unit))))
; integer
(define difference (- now then))
(when (< difference 0)
@@ -145,7 +145,7 @@
[(< difference 86400) (make-answer (floor (/ difference 3600)) "hour")]
[else (make-answer (floor (/ difference 86400)) "day")]))
-; (U time-tai time-utc) [(U time-tai time-utc)] -> string
+; (U time-tai time-utc) [(U time-tai time-utc)] [#:format string] -> string
;
; Takes a time-tai or time-utc (and, optionally, another argument of the same type representing
; the current time) and returns a string like:
@@ -154,18 +154,11 @@
; n minute(s) ago
; n hour(s) ago
; n day(s) ago
-(define time->ago-string
- (case-lambda
- [(then)
- (let ([now (if (time-tai? then)
- (current-time time-tai)
- (current-time time-utc))])
- (seconds->ago-string (time-second then) (time-second now)))]
- [(then now)
- (if (eq? (time-type then) (time-type now))
- (seconds->ago-string (time-second then) (time-second now))
- (raise-exn exn:fail:contract
- (format "Arguments have different time types: ~a ~a" then now)))]))
+(define (time->ago-string then [now (current-time (time-type then))] #:format [format-string "~a ~a ago"])
+ (if (eq? (time-type then) (time-type now))
+ (seconds->ago-string (time-second then) (time-second now) #:format format-string)
+ (raise-exn exn:fail:contract
+ (format "Arguments have different time types: ~a ~a" then now))))
; -> integer
; Returns the time zone offset of the current locale in seconds.
@@ -200,14 +193,14 @@
(provide/contract
[copy-date (->* (date?)
- (#:nanosecond (or/c (integer-in 0 999999999) false/c)
- #:second (or/c (integer-in 0 59) false/c)
- #:minute (or/c (integer-in 0 59) false/c)
- #:hour (or/c (integer-in 0 23) false/c)
- #:day (or/c (integer-in 1 31) false/c)
- #:month (or/c (integer-in 1 12) false/c)
- #:year (or/c integer? false/c)
- #:zone-offset (or/c (integer-in min-zone-offset max-zone-offset) false/c))
+ (#:nanosecond (or/c (integer-in 0 999999999) #f)
+ #:second (or/c (integer-in 0 59) #f)
+ #:minute (or/c (integer-in 0 59) #f)
+ #:hour (or/c (integer-in 0 23) #f)
+ #:day (or/c (integer-in 1 31) #f)
+ #:month (or/c (integer-in 1 12) #f)
+ #:year (or/c integer? #f)
+ #:zone-offset (or/c (integer-in min-zone-offset max-zone-offset) #f))
date?)]
[time->date (-> time/c date?)]
[time-tai? procedure?]
@@ -218,7 +211,7 @@
[date-week-day? (-> date? boolean?)]
[leap-year? (-> integer? boolean?)]
[days-in-month (->* (month/c) (integer?) integer?)]
- [seconds->ago-string (->* (integer?) (integer?) string?)]
- [time->ago-string (->* (time/c) (time/c) string?)]
+ [seconds->ago-string (->* (integer?) (integer? #:format string?) string?)]
+ [time->ago-string (->* (time/c) (time/c #:format string?) string?)]
[current-time-zone-offset (-> integer?)]
[current-year (-> integer?)])

0 comments on commit e7d54da

Please sign in to comment.