Re: Integer checks
Posted in 2008
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL
Obnoxio The Clown wrote: > monkeys paw wrote: > >> Hi all, I need an integer to conform to military time, so >> i have to check that the tens digit is no more than 59, >> i.e. 2159 is a valid time, 2162 is not. >> >> I've not seen much "arithmetic" done in 4gl so could someone >> suggest the best way to check an int for this condition. >> >> > > What's wrong with a DATETIME HOUR TO SECOND ? > > Anyway, no 4GL compiler handy, plus there's all sorts of assumptions here: FUNCTION validate_time (lv_time_in) DEFINE lv_time_in SMALLINT DEFINE lv_hour, lv_minute SMALLINT IF lv_time_in < 0 THEN ERROR "Negative time not allowed" RETURN FALSE END IF LET lv_hour = lv_time_in / 100 LET lv_minute = lv_time_in - (lv_hour * 100) IF lv_hour < 23 AND lv_minute < 60 AND lv_minute >= 0 THEN RETURN TRUE ELSE ERROR "Invalid time" RETURN FALSE END IF END FUNCTION -- Cheers, Obnoxio the Clown http://obotheclown.blogspot.com
This code snippet works perfectly, no compile probs even! The handling of the modulus had escaped me, thanks for jarring the cranium, OTC. > Obnoxio The Clown wrote: >> monkeys paw wrote: >> >>> Hi all, I need an integer to conform to military time, so >>> i have to check that the tens digit is no more than 59, >>> i.e. 2159 is a valid time, 2162 is not. >>> >>> I've not seen much "arithmetic" done in 4gl so could someone >>> suggest the best way to check an int for this condition. >>> >> >> What's wrong with a DATETIME HOUR TO SECOND ? >> >> > > Anyway, no 4GL compiler handy, plus there's all sorts of assumptions here: > > FUNCTION validate_time (lv_time_in) > DEFINE lv_time_in SMALLINT > DEFINE lv_hour, lv_minute SMALLINT > > IF lv_time_in < 0 > THEN > ERROR "Negative time not allowed" > RETURN FALSE > END IF > > LET lv_hour = lv_time_in / 100 > LET lv_minute = lv_time_in - (lv_hour * 100) > > IF lv_hour < 23 > AND lv_minute < 60 > AND lv_minute >= 0 > THEN > RETURN TRUE > ELSE > ERROR "Invalid time" > RETURN FALSE > END IF > END FUNCTION >
monkeys paw wrote: > This code snippet works perfectly, no compile probs even! > Am I allowed to preen? Just a little? :o) -- Cheers, Obnoxio the Clown http://obotheclown.blogspot.com
On Sep 16, 2:22 pm, Obnoxio The Clown <obno...@serendipita.com> wrote: > monkeys paw wrote: > > This code snippet works perfectly, no compile probs even! > > Am I allowed to preen? Just a little? :o) Just a little. But only a little. You forgot to check that the value was not negative, so -9999 passes your test for hours (< 23) -- you did check for non-negative minutes, but if I chose -100, that would have a remainder of zero, so the minutes would be valid and the hours would be OK too. Just a tiny bit of help from one nit-picker to another. -=JL=-
Jonathan Leffler wrote: > On Sep 16, 2:22 pm, Obnoxio The Clown <obno...@serendipita.com> wrote: > >> monkeys paw wrote: >> >>> This code snippet works perfectly, no compile probs even! >>> >> Am I allowed to preen? Just a little? :o) >> > > Just a little. But only a little. > > You forgot to check that the value was not negative, so -9999 passes > your test for hours (< 23) -- you did check for non-negative minutes, > but if I chose -100, that would have a remainder of zero, so the > minutes would be valid and the hours would be OK too. > > Just a tiny bit of help from one nit-picker to another. > Ahem: IF lv_time_in < 0 THEN ERROR "Negative time not allowed" RETURN FALSE END IF -- Cheers, Obnoxio the Clown http://obotheclown.blogspot.com