interval problem in 4GL
Posted in 1999
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL
I am trying to use the interval datatype to convert between a datetime value and a unix timestamp (the number of seconds since 1st January 1970). For instance, the timestamp for "27 April 1999 16:00" is 925228800. I want to return the value to an integer variable. I have tried something like: # # Convert a date variable to a unix timestamp # FUNCTION date_to_timestamp (db_date) DEFINE db_date DATETIME YEAR TO SECOND, base_date DATETIME YEAR TO SECOND, timestamp INTERVAL SECOND(9) TO SECOND LET base_date = datetime(1970-01-01) year to day LET timestamp = db_date - base_date RETURN timestamp END FUNCTION This doesn't work. It always just returns a null. Can anyone help? Neil Smith -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
neil_smith@my-dejanews.com wrote: > > I am trying to use the interval datatype to convert between a datetime value > and a unix timestamp (the number of seconds since 1st January 1970). For > instance, the timestamp for "27 April 1999 16:00" is 925228800. > > I want to return the value to an integer variable. I have tried something > like: You almost had it. See my mods: > > # > # Convert a date variable to a unix timestamp > # > FUNCTION date_to_timestamp (db_date) > > DEFINE > db_date DATETIME YEAR TO SECOND, > base_date DATETIME YEAR TO SECOND, > timestamp INTERVAL SECOND(9) TO SECOND, ctime char(20), utime integer > # You need to keep the precision the same > LET base_date = datetime(1970-01-01 00:00:00) year to second > LET timestamp = db_date - base_date # Cannot convert directly from timestamp to integer so the # return program would be more complex. Let's convert it here # by making an intermediate numeric string. LET ctime = timestamp LET utime = ctime RETURN utime # Returns an integer which is what you want anyway > > END FUNCTION I tried this as a stored procedure and it works as advertised, the 4GL version should work too. Worst case make it an SPL. Art S. Kagel