Re: ESQL/C Datetime conversions - PS (fwd)
Posted in 1996
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL
> > mpeter writes: > * > * > > > * > > A colleague is trying to read a datetime and then use some esql/c function > * > > (cvt_datetime()???) to convert that given a mask. Does anyone have some > * > > examples of this sort of thing they could forward? Essentially the > * > > functionality required is the ability to handle ANY datetime (or interval) > * > > and an associate mask - so foo(CURRENT MONTH TO DAY,"MM-DD") would return > * > > 05-20 for today. > * > > > * > > * Hi , > * I have a function which accepts the datetime (year to seconds) ,mask > * and the format . > * > * cnvt(YEAR_TO_MONTH,"yyyy/mm",date); > * > * And it returns in the given format . > * If u are really interested ,drop a mail . I will forward the code .. > * > * e.g : > * cnvt(YEAR_TO_HOUR,"dd-mm-yyyy hh","1996-05-23 09:30:20") > * > * returns 23-05-1996 09 Following is the function ... I hope this will be useful for u ... Bye. Peter. mpeter.muc@sni.de /************************************************************************ cnvt(int comp,char * format,char * val,char * result) comp - Format of the passed datetime value format - mask val - datetime value ( It is in comp format ) result - converted result **** Comp can be used to specify the output format if the passed datetime value is always in YEAR TO SECONDS format. For this the for loop starting value cnt should be from 1. **** '+' in the mask is used to ignore the delimiters. e.g yyyy+mm gives 199605 *******************************************************************/ #include <stdio.h> #include "cnvt.h" cnvt(int ,char * ,char * ,char *); trunc(char * ,char *); main() { char * dval = "04-05 09:30:20"; char res[50]; cnvt(26,"hh/tt",dval,res); printf("Result is : %s\\n",res); } cnvt(int comp,char * format,char * val,char * result) { int pos = 0; int cnt = 0; int rel ,len = 0; char del[2] ; char yy[5],mm[3],dd[3],tt[3],hh[3],ss[3]; if ( comp > 66 ) { printf("Invalid conversion ...\\n"); exit(0); } rel = comp / 10; comp = comp % 10; if ( rel > comp ) { printf("this order is not supported ...\\n"); exit(1); } strcpy(yy,"0000"); strcpy(mm,"00"); strcpy(dd,"00"); strcpy(hh,"00"); strcpy(tt,"00"); strcpy(ss,"00"); for(cnt=rel;cnt <= comp ; cnt++) /* cnt=1 if the datetime is Y-S */ { switch(cnt) { case 1 : trunc(val+pos,yy); pos += 2; break; case 2 : trunc(val+pos,mm); break; case 3 : trunc(val+pos,dd); break; case 4 : trunc(val+pos,hh); break; case 5 : trunc(val+pos,tt); break; case 6 : trunc(val+pos,ss); break; } pos += 3; } *result = '\\0'; while(1) { switch(toupper(*format++)) { case 'Y' : rel = 1; len++; continue; case 'M' : rel = 2; len++; continue; case 'D' : rel = 3; len++; continue; case 'H' : rel = 4; len++; continue; case 'T' : rel = 5; len++; continue; case 'S' : rel = 1; len++; continue; default : del[0] = *(format-1); } if(len != 0 && len%2 ) { printf("\\n Inalid format ....\\n"); exit(1); } switch(rel) { case 1 : /* if ( len == 2 ) yy += 2; */ strncat(result,yy,len); break; case 2 : strcat(result,mm); break; case 3 : strcat(result,dd); break; case 4 : strcat(result,hh); break; case 5 : strcat(result,tt); break; case 6 : strcat(result,ss); break; } if ( del[0] == '\\0' ) break; if ( del[0] != '+' ) strncat (result , del ,1); len = 0; rel = 0; } } trunc(char * str,char * res) { while(*res++ = *str++) if (! isdigit(*(res-1) )) break; *(res-1) = '\\0'; } /************************************************************************ cnvt.h - Header file contains format definitions ************************************************************************/ # define YEAR_TO_YEAR 11 # define YEAR_TO_MONTH 12 # define YEAR_TO_DAY 13 # define YEAR_TO_HOUR 14 # define YEAR_TO_MINUTE 15 # define YEAR_TO_SECOND 16 # define MONTH_TO_MONTH 22 # define MONTH_TO_DAY 23 # define MONTH_TO_HOUR 24 # define MONTH_TO_MINUTE 25 # define MONTH_TO_SECOND 26 # define DAY_TO_DAY 33 # define DAY_TO_HOUR 34 # define DAY_TO_MINUTE 35 # define DAY_TO_SECOND 36 # define HOUR_TO_HOUR 44 # define HOUR_TO_MINUTE 45 # define HOUR_TO_SECOND 46 # define MINUTE_TO_MINUTE 55 # define MINUTE_TO_SECOND 56 # define SECOND_TO_SECOND 66