From 6b29a50cd4e6266d53d1cbea0ad659834e221d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Thu, 5 Jan 2017 13:31:59 +0100 Subject: [PATCH] calendar.parser: invalid date checking for the parsing words --- basis/calendar/parser/parser-tests.factor | 25 ++++++++++ basis/calendar/parser/parser.factor | 58 +++++++++++------------ 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/basis/calendar/parser/parser-tests.factor b/basis/calendar/parser/parser-tests.factor index f0ef2025e9..b97e2bdfed 100644 --- a/basis/calendar/parser/parser-tests.factor +++ b/basis/calendar/parser/parser-tests.factor @@ -44,6 +44,25 @@ IN: calendar.parser.tests time- 1 seconds before? ] unit-test +! cookie-string>timestamp-1 +{ "20160203102022" } [ + "Friday, 03-Feb-2016 10:20:22 GMT" cookie-string>timestamp-1 + timestamp>mdtm +] unit-test + +[ "Friday, 03-Feb-2016 24:20:22 GMT" cookie-string>timestamp-1 ] +[ not-in-interval? ] must-fail-with +[ "Friday, 33-Feb-2016 12:20:22 GMT" cookie-string>timestamp-1 ] +[ not-in-interval? ] must-fail-with + +! cookie-string>timestamp-2 +{ "19980903102022" } [ + "Friday Sep 3 10:20:22 1998 GMT" cookie-string>timestamp-2 timestamp>mdtm +] unit-test + +[ "Friday Sep 3 10:60:22 1998 GMT" cookie-string>timestamp-2 ] +[ not-in-interval? ] must-fail-with + ! hhmm>duration { T{ duration { hour 10 } { minute 20 } } @@ -161,3 +180,9 @@ IN: calendar.parser.tests "Tue, 22 Apr 2008 14:36:12 GMT " rfc822>timestamp timestamp>rfc822 ] unit-test + +[ "Tue, 99 Apr 2008 14:36:12 GMT" rfc822>timestamp ] +[ not-in-interval? ] must-fail-with + +[ "Wed, 29 Feb 2017 10:20:30 GMT" rfc822>timestamp ] +[ not-in-interval? ] must-fail-with diff --git a/basis/calendar/parser/parser.factor b/basis/calendar/parser/parser.factor index 6f5f341104..eb0a732408 100644 --- a/basis/calendar/parser/parser.factor +++ b/basis/calendar/parser/parser.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs calendar calendar.english combinators -continuations io io.streams.string kernel macros math math.functions -math.parser sequences ; +continuations generalizations io io.streams.string kernel macros math +math.functions math.parser sequences ; IN: calendar.parser : read-00 ( -- n ) 2 read string>number ; @@ -94,17 +94,19 @@ CONSTANT: rfc822-named-zones H{ ] } cond ; +: read-hh:mm:ss ( -- hh mm ss ) + ":" read-token checked-number + ":" read-token checked-number + read-sp checked-number ; + : (rfc822>timestamp) ( -- timestamp ) - timestamp new - "," read-token day-abbreviations3 member? check-timestamp drop - read1 CHAR: \s assert= - read-sp checked-number >>day - read-sp month-abbreviations index 1 + check-timestamp >>month - read-sp checked-number >>year - ":" read-token checked-number >>hour - ":" read-token checked-number >>minute - read-sp checked-number >>second - " " read-until drop parse-rfc822-gmt-offset >>gmt-offset ; + "," read-token day-abbreviations3 member? check-timestamp drop + read1 CHAR: \s assert= + read-sp checked-number + read-sp month-abbreviations index 1 + check-timestamp + read-sp checked-number -rot swap + read-hh:mm:ss + " " read-until drop parse-rfc822-gmt-offset ; : rfc822>timestamp ( str -- timestamp ) [ (rfc822>timestamp) ] with-string-reader ; @@ -114,30 +116,24 @@ CONSTANT: rfc822-named-zones H{ check-timestamp drop ; : (cookie-string>timestamp-1) ( -- timestamp ) - timestamp new - "," read-token check-day-name - read1 CHAR: \s assert= - "-" read-token checked-number >>day - "-" read-token month-abbreviations index 1 + check-timestamp >>month - read-sp checked-number >>year - ":" read-token checked-number >>hour - ":" read-token checked-number >>minute - read-sp checked-number >>second - readln parse-rfc822-gmt-offset >>gmt-offset ; + "," read-token check-day-name + read1 CHAR: \s assert= + "-" read-token checked-number + "-" read-token month-abbreviations index 1 + check-timestamp + read-sp checked-number -rot swap + read-hh:mm:ss + " " read-until drop parse-rfc822-gmt-offset ; : cookie-string>timestamp-1 ( str -- timestamp ) [ (cookie-string>timestamp-1) ] with-string-reader ; : (cookie-string>timestamp-2) ( -- timestamp ) - timestamp new - read-sp check-day-name - read-sp month-abbreviations index 1 + check-timestamp >>month - read-sp checked-number >>day - ":" read-token checked-number >>hour - ":" read-token checked-number >>minute - read-sp checked-number >>second - read-sp checked-number >>year - readln parse-rfc822-gmt-offset >>gmt-offset ; + read-sp check-day-name + read-sp month-abbreviations index 1 + check-timestamp + read-sp checked-number + read-hh:mm:ss + [ read-sp checked-number ] 5 ndip + " " read-until drop parse-rfc822-gmt-offset ; : cookie-string>timestamp-2 ( str -- timestamp ) [ (cookie-string>timestamp-2) ] with-string-reader ;