Skip to content

Commit

Permalink
[project @ Provide support for formatting the day as an ordinal (e.g.…
Browse files Browse the repository at this point in the history
… 1st, 22nd)]
  • Loading branch information
danielwhite committed Oct 22, 2009
1 parent 95c32b9 commit 364f61d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/local-time.texinfo
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ FORMAT is a list containing one or more of strings, characters, and keywords. S
*microseconds
@item :nsec
*nanoseconds
@item :ordinal-day
day of month as an ordinal (e.g. 1st, 23rd)
@item :long-weekday
long form of weekday (e.g. Sunday, Monday)
@item :short-weekday
Expand Down
17 changes: 17 additions & 0 deletions src/local-time.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,21 @@ elements."
(* (or offset-minute 0) 60))
offset))))))

(defun ordinalize (day)
"Return an ordinal string representing the position of DAY in a sequence (1st, 2nd, 3rd, 4th, etc)."
(declare (type (integer 1 31) day))
(flet ((suffix ()
(if (<= 11 day 13)
"th"
(multiple-value-bind (quotient remainder) (floor day 10)
(declare (ignore quotient))
(case remainder
(1 "st")
(2 "nd")
(3 "rd")
(t "th"))))))
(format nil "~d~a" day (suffix))))

(defun %construct-timestring (timestamp format timezone)
"Constructs a string representing TIMESTAMP given the FORMAT of the string and the TIMEZONE. See the documentation of FORMAT-TIMESTRING for the structure of FORMAT."
(declare (type timestamp timestamp)
Expand Down Expand Up @@ -1550,6 +1565,8 @@ elements."
(princ (1+ (mod (1- hour) 12)) result))
((eql fmt :ampm)
(princ (if (< hour 12) "am" "pm") result))
((eql fmt :ordinal-day)
(princ (ordinalize day) result))
((or (stringp fmt) (characterp fmt))
(princ fmt result))
(t
Expand Down
16 changes: 15 additions & 1 deletion tests/tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,21 @@

"04:03"
(format-timestring nil test-timestamp
:format '((:hour 2) #\: (:min 2))))))
:format '((:hour 2) #\: (:min 2)))

"5th"
(format-timestring nil test-timestamp
:format '(:ordinal-day)))))

(test format-timestring/ordinals
(flet ((format-ordinal (day)
(format-timestring nil (encode-timestamp 0 0 0 0 day 1 2008)
:format '(:ordinal-day))))
(is-every string=
"31st" (format-ordinal 31)
"11th" (format-ordinal 11)
"22nd" (format-ordinal 22)
"3rd" (format-ordinal 3))))

(test format-timestring/errors
(with-output-to-string (*standard-output*)
Expand Down

0 comments on commit 364f61d

Please sign in to comment.