Re: Q: Date conversion (internal -> string)
Posted in 1995
RTFM: rdatestr()? The Version 5 and prior manuals don't mention that this pays any attention to DBDATE; the 6 and 7 manuals do. I don't think the code has changed significantly; the documentation team has simply been updated on what it actually does. Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> PS: cc -c w.shakespeare.c w.shakespeare.c:line 1: syntax error. Try: (2*B) || ~(2*B) lint w.shakespeare.c w.shakespeare.c:line 1: constant used in conditional context Try: (1) :-) PPS: I attach a shell archive which process DBDATE and produces a format string. >From: andreas@hacon.de (Andreas Dunker) >Date: Fri, 14 Jul 1995 11:49:44 GMT >X-Informix-List-Id: <news.15433> > >a question about informix ESQL/C: > >how do I convert an internal date into a string depending on the >environment variable DBDATE? Or do I have to check DBDATE and build >my own format string to use with "rfmtdate". : "@(#)shar.sh 1.9" #! /bin/sh # # This is a shell archive. # Remove everything above this line and run sh on the resulting file. # If this archive is complete, you will see this message at the end: # "All files extracted" # # Created: Fri Jul 14 08:24:47 PDT 1995 by johnl at Informix Software Ltd. # Files archived in this archive: # datefmt.c # #-------------------- if [ -f datefmt.c -a "$1" != "-c" ] then echo shar: datefmt.c already exists else echo 'x - datefmt.c (2588 characters)' sed -e 's/^X//' >datefmt.c <<'SHAR-EOF' X/* X@(#)File: datefmt.c X@(#)Version: 1.3 X@(#)Last changed: 95/06/28 X@(#)Purpose: Convert DBDATE into date format X@(#)Author: J Leffler X@(#)Product: :PRODUCT: X*/ X X#include <ctype.h> X#include <stdlib.h> X#include <string.h> X X#define DEFDBDATE "mdy2/" X#define DB_YEAR 0x01 X#define DB_MONTH 0x02 X#define DB_DAY 0x04 X#define DB_DELIM 0x08 X#define DB_DONE (DB_YEAR|DB_MONTH|DB_DAY|DB_DELIM) X X#define NIL(x) ((x)0) X X#ifndef lint Xstatic char sccs[] = "@(#)datefmt.c 1.3 95/06/28"; X#endif X X#ifdef __STDC__ Xchar *fmt_dbdate(void) X#else Xchar *fmt_dbdate() X#endif /* __STDC__ */ X{ X char *dbdate; X static char retval[11]; X char *p; X char *d; X char c; X int state; X int i; X int n; X X if ((dbdate = getenv("DBDATE")) == NIL(char *)) X dbdate = DEFDBDATE; X if (strlen(dbdate) != 5) X return(NIL(char *)); X p = retval; X d = dbdate; X state = 0; X while ((c = *d++) != '\\0') X { X switch (c) X { X case 'y': X case 'Y': X if (state & DB_YEAR) X return(NIL(char *)); X switch (*d++) X { X case '2': X n = 2; X break; X case '4': X n = 4; X break; X default: X return(NIL(char *)); X } X for (i = 0; i < n; i++) X *p++ = 'y'; X *p++ = ' '; X state |= DB_YEAR; X break; X case 'm': X case 'M': X if (state & DB_MONTH) X return(NIL(char *)); X *p++ = 'm'; X *p++ = 'm'; X *p++ = ' '; X state |= DB_MONTH; X break; X case 'd': X case 'D': X if (state & DB_DAY) X return(NIL(char *)); X *p++ = 'd'; X *p++ = 'd'; X *p++ = ' '; X state |= DB_DAY; X break; X default: X if (state & DB_DELIM) X return(NIL(char *)); X if (!ispunct(c)) X return(NIL(char *)); X state |= DB_DELIM; X if ((state & DB_DONE) != DB_DONE) X return(NIL(char *)); X *--p = '\\0'; X for (p = retval; *p != '\\0'; p++) X if (*p == ' ') X *p = c; X break; X } X } X if ((state & DB_DONE) != DB_DONE) X return(NIL(char *)); X return(retval); X} X X#ifdef TEST Xmain() X{ X static char *dbd[] = X { X "DBDATE=dmy4/", X "DBDATE=mdy4/", X "DBDATE=my4d/", X "DBDATE=dy4m/", X "DBDATE=y4dm/", X "DBDATE=y4md~", X "DBDATE=dmy2.", X "DBDATE=y2md-", X "DBDATE=y1md-", X "DBDATE=y2Md-", X "DBDATE=y2mD-", X "DBDATE=Y2md-", X 0 X }; X char **s; X char *fmt; X X printf("DBDATE=%s; date format = %s\\n", getenv("DBDATE"), fmt_dbdate()); X X for (s = dbd; *s != NIL(char *); s++) X { X putenv(*s); X fmt = fmt_dbdate(); X printf("DBDATE=%s; date format = %s\\n", getenv("DBDATE"), X fmt ? fmt : "<NULL POINTER>"); X } X return(0); X} X#endif /* TEST */ SHAR-EOF chmod 444 datefmt.c if [ `wc -c <datefmt.c` -ne 2588 ] then echo shar: datefmt.c unpacked with wrong size fi # end of overwriting check fi echo All files extracted exit 0