Re: Integer checks
Posted in 2008
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL
On Sep 16, 2:42 pm, Obnoxio The Clown <obno...@serendipita.com> wrote: > 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 shouldn't be less than or equal to 23. It thought 2300 - 2359 would be vaild times. IF lv_hour <= 23 AND lv_minute < 60 AND lv_minute >= 0 I also don't think you need to check lv_minte >= 0 either since you check for a negative right off the bat (very clever to get rid of those right off) and there is no way that a non negative value for lv_time_in can give you a negative lv_minute. But nice work and don't you hate nit pickers. ;-)
On Sep 17, 4:24 pm, bozon <cur...@crowson1.com> wrote: > On Sep 16, 2:42 pm, Obnoxio The Clown <obno...@serendipita.com> wrote: > > > > > 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 > > shouldn't be less than or equal to 23. > > It thought 2300 - 2359 would be vaild times. > > IF lv_hour <= 23 > AND lv_minute < 60 > AND lv_minute >= 0 > > I also don't think you need to check lv_minte >= 0 either since you > check for a negative right off the bat (very clever to get rid of > those right off) and there is no way that a non negative value for > lv_time_in can give you a negative lv_minute. > > But nice work and don't you hate nit pickers. ;-) Man if I am going to pick nits I need to use better grammar. Maybe I need some sleep.
On Sep 17, 1:24 pm, bozon wrote: > On Sep 16, 2:42 pm, Obnoxio The Clown wrote: > > 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. > > 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 > > shouldn't be less than or equal to 23. > > [I] thought 2300 - 2359 would be [valid] times. > > IF lv_hour <= 23 > AND lv_minute < 60 > AND lv_minute >= 0 > > I also don't think you need to check [lv_minute >= 0] either since you > check for a negative right off the bat (very clever to get rid of > those right off) and there is no way that a non negative value for > lv_time_in can give you a negative lv_minute. > > But nice work and don't you hate nit pickers. ;-) [Editorial in square brackets] I think that's what threw me off track -- the test for lv_minute >= 0 is unnecessary given the previous test for negative numbers. Clever of OTC to confuse two people, devious personage that he is. And you're right about needing to allow for the last hour of the day (and obnoxiously pedantic people might note that 24:00 is a valid time, especially in a context such as "lasts all day, from 00:00 to 24:00"; using 23:59 there means there is a minute's worth of ambiguity). -=JL=-
On Sep 19, 8:33 am, Jonathan Leffler <jonathan.leff...@gmail.com> wrote: > On Sep 17, 1:24 pm, bozon wrote: > > > > > On Sep 16, 2:42 pm, Obnoxio The Clown wrote: > > > 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. > > > 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 > > > shouldn't be less than or equal to 23. > > > [I] thought 2300 - 2359 would be [valid] times. > > > IF lv_hour <= 23 > > AND lv_minute < 60 > > AND lv_minute >= 0 > > > I also don't think you need to check [lv_minute >= 0] either since you > > check for a negative right off the bat (very clever to get rid of > > those right off) and there is no way that a non negative value for > > lv_time_in can give you a negative lv_minute. > > > But nice work and don't you hate nit pickers. ;-) > > [Editorial in square brackets] > > I think that's what threw me off track -- the test for lv_minute >= 0 > is unnecessary given the previous test for negative numbers. Clever > of OTC to confuse two people, devious personage that he is. And > you're right about needing to allow for the last hour of the day (and > obnoxiously pedantic people might note that 24:00 is a valid time, > especially in a context such as "lasts all day, from 00:00 to 24:00"; > using 23:59 there means there is a minute's worth of ambiguity). > > -=JL=- Well said. I don't mind the >= 0 test later it is kind of an assert and documents what is going on.