diff --git a/splitflap-doc/constructs.scrbl b/splitflap-doc/constructs.scrbl index 2d66aa6..2a7e6a5 100644 --- a/splitflap-doc/constructs.scrbl +++ b/splitflap-doc/constructs.scrbl @@ -237,25 +237,37 @@ bit more ergonomic. @defproc[(infer-moment [str string? ""]) moment?]{ Parses from @racket[_str] and returns a precise @racket[moment], inferring time information where -ommitted and using @racket[current-timezone] as the time zone for the moment. +ommitted. If @racket[_str] is @racketvalfont{""}, then the result of @racket[now/moment] is returned. -Otherwise @racket[_str] must be in the form @racket{YYYY-MM-DD [hh:mm[:ss]]} or an exception is -raised. If the seconds are ommitted, @racketvalfont{00} is assumed, and if the hours and minutes are -ommitted, @racketvalfont{00:00:00} (the very start of the date) is assumed. +Otherwise @racket[_str] must be in the form +@tt{@racketvarfont{YYYY}@litchar{-}@racketvarfont{MM}@litchar{-}@racketvarfont{DD}[ |@litchar{T}]@racketvarfont{hh}[@litchar{:}@racketvarfont{mm}[@litchar{:}@racketvarfont{ss}[@litchar{.}@racketvarfont{s}]]][@nonterm{offset}]]} or an +exception is raised. Omitted minutes and seconds are assumed to be @racketvalfont{00}, and if the time +is omitted entirely, @racketvalfont{00:00:00} (the very start of the date) is assumed. Fractional +seconds may have up to nine digits. + +The optional @nonterm{offset} is either @litchar{Z} (for UTC) or @litchar{+hh:mm} or +@litchar{-hh:mm}, and determines the time zone of the result. If the offset is omitted, +@racket[current-timezone] is used. @examples[#:eval mod-constructs (infer-moment "2012-08-31") (infer-moment "2012-08-31 13:34") (infer-moment "2015-10-02 01:03:15") + (infer-moment "2015-10-02T01:03:15.5") (parameterize ([current-timezone -14400]) (infer-moment "2015-10-02 01:03:15")) + (infer-moment "2015-10-02T01:03:15Z") + (infer-moment "2015-10-02T01:03:15-04:00") + (infer-moment "2012-09-14 12") (infer-moment)] -@history[#:changed "1.2" "Added no-argument form for current moment"] +@history[#:changed "1.2" "Added no-argument form for current moment" + #:changed "1.4.1" @elem{Added the @litchar{T} separator, fractional seconds, UTC offsets and + hours without minutes; trailing characters now raise an exception}] } diff --git a/splitflap-doc/notes.scrbl b/splitflap-doc/notes.scrbl index a8c16bf..804edf7 100644 --- a/splitflap-doc/notes.scrbl +++ b/splitflap-doc/notes.scrbl @@ -23,6 +23,18 @@ requires email addresses to conform to RFC 2822, which allows only ASCII. @section{Version History} +@subsection{Version 1.4.1} + +@itemlist[ + +@item{@racket[infer-moment] now accepts @litchar{T} between the date and time, an hour without +minutes, fractional seconds, and a UTC offset (@litchar{Z} or @litchar{±hh:mm}). Previously it +ignored anything it could not parse after the date: @racket{2012-09-14 12} and +@racket{2012-09-14T10:30} were both read as midnight. It now reads these as 12:00 and 10:30, and +raises an exception for strings with trailing characters.} + +] + @subsection{Version 1.4} @itemlist[ diff --git a/splitflap-lib/info.rkt b/splitflap-lib/info.rkt index 66387d8..a7be519 100644 --- a/splitflap-lib/info.rkt +++ b/splitflap-lib/info.rkt @@ -1,7 +1,7 @@ #lang info (define collection "splitflap") -(define version "1.4") +(define version "1.4.1") (define deps '(["base" #:version "8.1"] "gregor-lib")) diff --git a/splitflap-lib/private/validation.rkt b/splitflap-lib/private/validation.rkt index 9038b0b..aad1a6e 100644 --- a/splitflap-lib/private/validation.rkt +++ b/splitflap-lib/private/validation.rkt @@ -333,14 +333,26 @@ (define (n: v) (cond [(not v) 0] [(string? v) (string->number v)] [else v])) (define date/time-regex - #px"^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])(?:\\s+([01]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9]|60))?)?") + (pregexp (string-append "^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])" + "(?:(?:T|\\s+)([01]?[0-9]|2[0-3])(?::([0-5][0-9])" + "(?::([0-5][0-9]|60)(?:\\.([0-9]{1,9}))?)?)?" + "(Z|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))?)?$"))) + +(define (fraction->nanoseconds f) + (if f (* (string->number f) (expt 10 (- 9 (string-length f)))) 0)) + +(define (utc-offset zone sign hr min) + (cond [(not zone) (current-timezone)] + [(equal? zone "Z") 0] + [else ((if (equal? sign "-") - +) (+ (* 3600 (n: hr)) (* 60 (n: min))))])) (define (infer-moment [str ""]) (match str ["" (now/moment)] - [(pregexp date/time-regex (list _ y m d hr min sec)) - (moment (n: y) (n: m) (n: d) (n: hr) (n: min) (n: sec) 0)] - [_ (raise-argument-error 'Date "string in the format ‘YYYY-MM-DD [hh:mm[:ss]]’" str)])) + [(pregexp date/time-regex (list _ y m d hr min sec frac zone sign zhr zmin)) + (moment (n: y) (n: m) (n: d) (n: hr) (n: min) (n: sec) (fraction->nanoseconds frac) + #:tz (utc-offset zone sign zhr zmin))] + [_ (raise-argument-error 'Date "string in the format ‘YYYY-MM-DD[(T| )hh[:mm[:ss[.s]]][Z|±hh:mm]]’" str)])) (define (moment->string m type) (~t m (case type diff --git a/splitflap-tests/tests/construct-tests.rkt b/splitflap-tests/tests/construct-tests.rkt index 1c10d46..6a5ce68 100644 --- a/splitflap-tests/tests/construct-tests.rkt +++ b/splitflap-tests/tests/construct-tests.rkt @@ -238,6 +238,31 @@ (check-equal? (infer-moment "2022-04-08") (moment 2022 4 8)) (check-exn exn:fail:contract? (lambda () (infer-moment "2022"))) +(check-equal? (infer-moment "2022-04-08 13:34") + (moment 2022 4 8 13 34)) +(check-equal? (infer-moment "2022-04-08T13:34") + (moment 2022 4 8 13 34)) +(check-equal? (infer-moment "2022-04-08T13:34:15") + (moment 2022 4 8 13 34 15)) +(check-equal? (infer-moment "2022-04-08T13:34:15.25") + (moment 2022 4 8 13 34 15 250000000)) +(check-equal? (infer-moment "2022-04-08T13:34:15Z") + (moment 2022 4 8 13 34 15 #:tz 0)) +(check-equal? (infer-moment "2022-04-08 13:34-05:00") + (moment 2022 4 8 13 34 #:tz -18000)) +(check-equal? (infer-moment "2022-04-08T13:34:15.5+05:30") + (moment 2022 4 8 13 34 15 500000000 #:tz 19800)) +(check-equal? (infer-moment "2022-04-08 12") + (moment 2022 4 8 12)) +(check-equal? (infer-moment "2022-04-08T12Z") + (moment 2022 4 8 12 #:tz 0)) +(for ([bad (in-list '("2022-04-08T12:" + "2022-04-08T" + "2022-04-08 13:34 extra" + "2022-04-08Z" + "2022-04-08T13:34+5" + "2022-04-08T13:34:15."))]) + (check-exn exn:fail:contract? (lambda () (infer-moment bad)) bad)) ;; ~~ Persons ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~