Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

Question about date? function

 [1/4] from: bkalef:sympatico:ca at: 13-Oct-2001 1:00

Should the date? function return false if an invalid date is entered? For example, when entering (YYYY-MM-DD);
>> date? 2001-12-25
== true So you would think that a False would be returned if a date was incorrect, however, unexpectedly;
>> date? 2001-13-25
** Syntax Error: Invalid date -- 2001-13-25 ** Near: (line 1) date? 2001-13-25 Since date? doesn't return a false but a Syntax Error. When I try the following I would expect it to gracefully allow me to enter an invalid date and allow me to write code around it to correct it.
>> if error? try [ date? 2001-13-25] [print "An invalid date was entered"]
** Syntax Error: Invalid date -- 2001-13-25 ** Near: (line 1) error? try [ date? 2001-13-25] Any thoughts?

 [2/4] from: carl:cybercraft at: 13-Oct-2001 20:02

On 13-Oct-01, Brock & Janet wrote:
> Should the date? function return false if an invalid date is > entered?
<<quoted lines omitted: 14>>
> ** Near: (line 1) error? try [ date? 2001-13-25] > Any thoughts?
I'd think the error occurs because it's an invalid datatype, same as entering...
>> 100s
** Syntax Error: Invalid integer -- 100s is invalid. When a script's running though, any dates entered would probably start out as a string, so just check for an error when converting it to a date...
>> if error? try [to-date "2001-13-25"][print "Date error!"]
Date error! -- Carl Read

 [3/4] from: bkalef:sympatico:ca at: 13-Oct-2001 4:48

Carl, Thanks. I see how this works. I guess the date? function isn't really processing the date and is just reporting back validity for the datatype query. You are correct about the date I am retrieving - it is coming in as a string from an ASK statement to start with and I could use try as indicated in your code below. This is my first 'real' and 'original' project with Rebol and my first real programming project outside of course-material and manipulating other Rebolers code for my own purposes. I'm trying to create a 'Scheduling' program/database for a 'rotating-shift oriented' workplace. I'm starting out text based and will add a /View GUI later. I've had enough of a challenge just getting the logic working properly without worrying about the GUI problems that will arise. I ran into this particular problem while trying to error-check data entry for a small function. My code is quickly getting large as I'm struggling to find adequate methods of code reuse. An example would be how to pass a parameter to a function that is later used in a data element path ie. record/:var-item-no. I would use a foreach statement to loop through each 'word in a series and each word would actually reference data elements from the data series. This would prompt the user for the specific element and then do the error checking, here's some sample code where code-reuse would be helpful..... until [sched-block/cell: ask "Enter Cell Prime: " find employees sched-block/cell] until [sched-block/p8am: ask "Enter Prostar 8am Prime: " find employees sched-block/p8am] until [sched-block/p9am: ask "Enter Prostar 9am Prime: " find employees sched-block/p9am] The 'until is doing the continuous error checking based on the result of the 'find statements success Brock

 [4/4] from: joel::neely::fedex::com at: 13-Oct-2001 8:53

Hi, Brock, Brock & Janet wrote:
> Carl, > Thanks. I see how this works. I guess the date? function isn't > really processing the date and is just reporting back validity > for the datatype query... >
Carl's already done a great job with the original question, so I'm just gilding the lilly... May I suggest thinking about DATE? a little differently from your wording above? The reason is that "validity" sounds (at least to my ear) more like some kind of before-the-fact syntax check -- asking if it would be OK to transform an argument into some type *in the future*. The result of DATE? (and his many cousins) is simply telling whether the argument value is *already* of a particular type, because that argument is *already* transformed from external form into some REBOL value. In other words,
>> integer? 123
== true is equivalent to
>> integer! = type? 123
== true and so on for all XXX?/XXX! pairs, so that
>> integer? "123"
== false will always be false (no matter what the sequence of characters supplied) because
>> type? "123"
== string! As Carl already pointed out, if you're trying to figure out whether a string *could be* converted to a date (integer, whatever...) in the obvious way, TRY is your friend (unless you want to write your own PARSE rules...). Let me add that if you want to encapsulate that for reuse throughout your script, you might try one of the following. looks-like-a-date: func [s [string!]][ not error? try [to-date s] ] ... just tests whether the string *can be* made into a DATE! value.
>> looks-like-a-date "01/13/25" == false >> looks-like-a-date "01/12/25" == true
On the other hand, try-date: func [s [string!]][ if error? try [return to-date s] [none] ] ... will return you the converted DATE! value or NONE to signal failure.
>> try-date "01/13/25" == none >> try-date "01/12/25" == 1-Dec-2025 >> try-date "001/12/25" == 1-Dec-2025 >> try-date "2001/12/25" == 25-Dec-2001
You can use it "as is" in repeated code cliches, such as ...
>> until [birthday: try-date ask "What's your birthday? "]
What's your birthday? 01/13/25 What's your birthday? 01/12/25 == 1-Dec-2025 ... or the slightly fancier ...
>> while [
[ none? birthday: try-date ask "What's your birthday? " [ ][ [ print "^-Please try yyyy-mm-dd format" [ ] print ["^-Ah," birthday "is a nice date!"] What's your birthday? 01/13/25 Please try yyyy-mm-dd format What's your birthday? 25/01/13 Ah, 25-Jan-2013 is a nice date! Following the DRY principle (Don't Repeat Yourself), you can turn all of the above into a re-usable function ... get-a-date: func [ prompt [string!] /local result ][ while [ none? result: try-date ask prompt ][ print "^-Please try yyyy-mm-dd format" ] result ] ... which, of course, does this ...
>> get-a-date "What's your birthday? "
What's your birthday? 1/13/25 Please try yyyy-mm-dd format What's your birthday? 2001/13/25 Please try yyyy-mm-dd format What's your birthday? 2001/12/25 == 25-Dec-2001 By now I'm sure I've beaten this poor issue to death, but I wanted to think through the kind of refactoring that I often use to "grow" reusable ideas/functions in REBOL, which (within certain bounds) is quite handy for this kind of stream-of-consciousness evolution. Of course, I apologize if I've insulted your intelligence or experience! ;-) Happy REBOLing! -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;

  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted