Unix Technical Forum

Datetime/Interval Question

This is a discussion on Datetime/Interval Question within the Informix forums, part of the Database Server Software category; --> Hi Informixers, in an ESQL/C-Program, I have to add a given amount of minutes (represented as an integer value) ...


Go Back   Unix Technical Forum > Database Server Software > Informix

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-20-2008, 06:49 AM
Richard Spitz
 
Posts: n/a
Default Datetime/Interval Question

Hi Informixers,

in an ESQL/C-Program, I have to add a given amount of minutes
(represented as an integer value) to an existing datetime variable
(datetime year to minute).

In SQL, this would be very easy with the UNITS keyword, like in
SELECT datetime_column + 135 UNITS MINUTE ...

The UNITS keyword doesn't seem to exist in ESQL/C. When I use the
"dtaddinv()" function, do I really first have to convert the
minutes into an interval hour to minute variable (or in case
there are more than 1440 minutes, an interval day to minute
variable) and then apply that function, or is there an easier
method to accomplish this?

Maybe someone has a ready-to-use function to convert a number
of minutes into a suitable interval variable?

Regards, Richard
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-20-2008, 06:49 AM
rkusenet
 
Posts: n/a
Default Re: Datetime/Interval Question


"Richard Spitz" <Richard.Spitz@med.uni-muenchen.de> wrote in message
news:uje5m0pt8akcncu2s2707e7s26cg98i55o@4ax.com...
> Hi Informixers,
>
> in an ESQL/C-Program, I have to add a given amount of minutes
> (represented as an integer value) to an existing datetime variable
> (datetime year to minute).
>
> In SQL, this would be very easy with the UNITS keyword, like in
> SELECT datetime_column + 135 UNITS MINUTE ...
>
> The UNITS keyword doesn't seem to exist in ESQL/C. When I use the
> "dtaddinv()" function, do I really first have to convert the
> minutes into an interval hour to minute variable (or in case
> there are more than 1440 minutes, an interval day to minute
> variable) and then apply that function, or is there an easier
> method to accomplish this?
>
> Maybe someone has a ready-to-use function to convert a number
> of minutes into a suitable interval variable?
>
> Regards, Richard



I have always found computing with date and time to be a big pain,
till I found a solution using Jonathan's Leffler's function.

For example to add 2 hours to current time, I would do the
following:

select CURRENT,unixtime_to_dt(to_unix_time(CURRENT)+7200)
from systables where tabid = 1

(expression) (expression)

2004-10-05 12:28:49.000 2004-10-05 14:28:49

The SQL uses two function to_unix_time which converts a datetime into unix
epoch time. Since it deals with seconds, I am adding 7200 for 2 hours. The
function unixtime_to_dt is the inverse of to_unix_time, that is, it converts
unix epoch time to an Informix datetime.

I find this approach extremely convenient. Somehow I find INTERVAL,
UNITS etc to add/subtract time inside a SQL unmaintainable.

You can search for Leffler's function in google or you can email me for the code.
BTW my code is idx 9.x specific because it uses int8.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-20-2008, 06:50 AM
Jonathan Leffler
 
Posts: n/a
Default Re: Datetime/Interval Question

Richard Spitz wrote:
> In an ESQL/C-Program, I have to add a given amount of minutes
> (represented as an integer value) to an existing datetime variable
> (datetime year to minute).
>
> In SQL, this would be very easy with the UNITS keyword, like in
> SELECT datetime_column + 135 UNITS MINUTE ...
>
> The UNITS keyword doesn't seem to exist in ESQL/C. When I use the
> "dtaddinv()" function, do I really first have to convert the
> minutes into an interval hour to minute variable (or in case
> there are more than 1440 minutes, an interval day to minute
> variable) and then apply that function, or is there an easier
> method to accomplish this?
>
> Maybe someone has a ready-to-use function to convert a number
> of minutes into a suitable interval variable?


Yes, you do have to create an interval type to deal with it.

You would normally use an INTERVAL MINUTE(9) TO MINUTE variable for
the number of minutes - so you don't have to convert more than 60
minutes into hours and minutes, or more than 1440 minutes into days,
hours and minutes.

I don't have the function pre-coded. At the IIUG, there is code in
intvl_conv that is the closest shot - it converts incoming intervals
into total number of seconds, or total number of minutes, or whatever,
as a decimal number with a fractional part too.

You probably need to do something like:

int iv_cvminutes(intvl_t *iv, int minutes)
{
int rc;
char buffer[24]; /* Big enough for 64-bit int */
sprintf(buffer, "%d", minutes);
iv->in_qual = TU_IENCODE(9, TU_MINUTE, TU_MINUTE);
rc = incvasc(buffer, iv);
return(rc);
}

I like to keep a local variable for the return value from incvasc() so
it is easier to track problems with a debugger -- you could eliminate
it and return the value from incvasc() directly if you prefer.

Note that generalizing this to handle any interval is easy (and you
could rewrite iv_cvminutes() in terms of this function if you preferred):

int iv_cvunits(intvl_t *iv, int units, int value)
{
int rc;
char buffer[24]; /* Big enough for 64-bit int */
sprintf(buffer, "%d", value);
iv->in_qual = TU_IENCODE(9, units, units);
rc = incvasc(buffer, iv);
return(rc);
}

Use: if ((rc = iv_cvunits(&my_interval, TU_MINUTES, i)) != 0)
...handle error...

Obviously, you need to supply headers, test code, etc. This also has
not been past the compiler - there could be bugs to be found.

--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-20-2008, 06:51 AM
Richard Spitz
 
Posts: n/a
Default Re: Datetime/Interval Question

Jonathan Leffler <jleffler@earthlink.net> wrote:

>> The UNITS keyword doesn't seem to exist in ESQL/C. When I use the
>> "dtaddinv()" function, do I really first have to convert the
>> minutes into an interval hour to minute variable (or in case
>> there are more than 1440 minutes, an interval day to minute
>> variable) and then apply that function, or is there an easier
>> method to accomplish this?
>>
>> Maybe someone has a ready-to-use function to convert a number
>> of minutes into a suitable interval variable?

>
>Yes, you do have to create an interval type to deal with it.
>
>You would normally use an INTERVAL MINUTE(9) TO MINUTE variable for
>the number of minutes - so you don't have to convert more than 60
>minutes into hours and minutes, or more than 1440 minutes into days,
>hours and minutes.


Thanks, also to Art S. Kagel for his helpful mail. As it turned
out, an INTERVAL MINUTE(4) TO MINUTE worked beautifully. It's a
bit awkward that there is no direct conversion function available
between "int" and "interval", I have to convert the "int" to a
string using "sprintf" and then use the "incvasc()" function to
get the interval, but that is just a little nuisance.

>I don't have the function pre-coded. At the IIUG, there is code in
>intvl_conv that is the closest shot - it converts incoming intervals
>into total number of seconds, or total number of minutes, or whatever,
>as a decimal number with a fractional part too.


Thanks for your code examples. The above was totally sufficient,
all I wanted was having to convert a number of minutes into
days, hours and minutes. I had misinterpreted the code example
for the incvasc() function in the ESQL/C manual.

Regards, Richard
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:15 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com