Index: dates.rkt ================================================================== --- dates.rkt +++ dates.rkt @@ -19,15 +19,32 @@ ;; Author contact information: ;; joel@jdueck.net ;; https://joeldueck.com ;; ------------------------------------------------------------------------- -;; Convenience functions for date strings +;; Convenience functions for YYYY-MM-DD date strings (require gregor racket/string) (provide (all-defined-out)) -;; Ignores everything after the first space +;; These functions ignore everything after the first space! +(define (ymd->dateformat ymd-string dateformat) + (~t (iso8601->date (car (string-split ymd-string))) dateformat)) + (define (ymd->english ymd-string) - (~t (iso8601->date (car (string-split ymd-string))) "MMMM d, yyyy")) + (ymd->dateformat ymd-string "MMMM d, yyyy")) + +(module+ test + (require rackunit) + (check-equal? (ymd->english "2018-08-12") "August 12, 2018") + (check-equal? (ymd->dateformat "2018-08-12" "d MMM YYYY") "12 Aug 2018") + + ;; How we handle weird input + (check-equal? (ymd->english "2018-08-12 everything after 1st space ignored") "August 12, 2018") + (check-equal? (ymd->english "2018-08 omitting the day") "August 1, 2018") + (check-equal? (ymd->english "2018 omitting month and day") "January 1, 2018") + (check-equal? (ymd->dateformat "2018-08-12") "123") + + ;; Stuff we just don't handle + (check-exn exn:gregor:parse? (lambda () (ymd->english "2018-xyz"))))