Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions splitflap-doc/constructs.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -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}]

}

Expand Down
12 changes: 12 additions & 0 deletions splitflap-doc/notes.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down
2 changes: 1 addition & 1 deletion splitflap-lib/info.rkt
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
20 changes: 16 additions & 4 deletions splitflap-lib/private/validation.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions splitflap-tests/tests/construct-tests.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Loading