diff --git a/collects/mzlib/date.rkt b/collects/mzlib/date.rkt index 597815e190..4767898f09 100644 --- a/collects/mzlib/date.rkt +++ b/collects/mzlib/date.rkt @@ -7,6 +7,7 @@ (provide/contract [current-date (-> date?)] + [date->seconds (date? . -> . exact-integer?)] [date->string ((date?) (boolean?) . ->* . string?)] [date-display-format (parameter/c (symbols 'american 'chinese 'german 'indian 'irish 'julian 'iso-8601 'rfc2822))] [find-seconds ((integer-in 0 61) @@ -251,6 +252,15 @@ (lambda () (force d)))) +(define (date->seconds date) + (find-seconds + (date-second date) + (date-minute date) + (date-hour date) + (date-day date) + (date-month date) + (date-year date))) + (define (find-seconds sec min hour day month year) (define (signal-error msg) (error 'find-secs (string-append diff --git a/collects/scribblings/reference/time.scrbl b/collects/scribblings/reference/time.scrbl index dd9b7b9905..4a405ca46a 100644 --- a/collects/scribblings/reference/time.scrbl +++ b/collects/scribblings/reference/time.scrbl @@ -15,7 +15,6 @@ The value of @racket[(current-seconds)] increases as time passes seconds can be compared with a time returned by @racket[file-or-directory-modify-seconds].} - @defproc[(seconds->date [secs-n exact-integer?]) date?]{ Takes @racket[secs-n], a platform-specific time in seconds returned by @@ -147,6 +146,10 @@ day only if @racket[time?]. See also @racket[date-display-format].} Parameter that determines the date string format. The initial format is @racket['american].} +@defproc[(date->seconds [date date?]) exact-integer?]{ +Finds the representation of a date in platform-specific seconds. If +the platform cannot represent the specified date, an error is +signaled, otherwise an integer is returned. } @defproc[(find-seconds [second (integer-in 0 61)] [minute (integer-in 0 59)] diff --git a/collects/tests/racket/date.rktl b/collects/tests/racket/date.rktl index 074d38f5a3..5c1e368a2a 100644 --- a/collects/tests/racket/date.rktl +++ b/collects/tests/racket/date.rktl @@ -20,11 +20,14 @@ (test-find 0 0 0 1 4 2005) ; date->string -(let ([d (seconds->date (find-seconds 1 2 3 4 5 2006))]) +(let* ([secs (find-seconds 1 2 3 4 5 2006)] + [d (seconds->date secs)]) (define (test-string fmt time? result) (test (parameterize ([date-display-format fmt]) (date->string d time?)) fmt result)) + (test secs date->seconds d) + (test-string 'american #f "Thursday, May 4th, 2006") (test-string 'american #t "Thursday, May 4th, 2006 3:02:01am") (test-string 'chinese #f "2006/5/4 星期四")