Line data Source code
1 : /*
2 : * Purpose: Test datetime conversion as well as dbdata() & dbdatlen()
3 : * Functions: dbcmd dbdata dbdatecrack dbdatlen dbnextrow dbresults dbsqlexec
4 : */
5 :
6 : #include "common.h"
7 :
8 : static int failed = 0;
9 : static void set_failed(int line)
10 : {
11 0 : failed = 1;
12 0 : fprintf(stderr, "Failed check at line %d\n", line);
13 : }
14 : #define set_failed() set_failed(__LINE__)
15 :
16 : #ifdef MSDBLIB
17 : #define dateyear year
18 : #define datemonth month
19 : #define datedmonth day
20 : #define datedyear dayofyear
21 : #define datedweek weekday
22 : #define datehour hour
23 : #define dateminute minute
24 : #define datesecond second
25 : #define datemsecond millisecond
26 : #define datensecond nanosecond
27 : #endif
28 :
29 : #ifdef SYBMSDATETIME2
30 : static int
31 6 : ignore_msg_handler(DBPROCESS * dbproc TDS_UNUSED, DBINT msgno TDS_UNUSED, int state TDS_UNUSED, int severity TDS_UNUSED,
32 : char *text TDS_UNUSED, char *server TDS_UNUSED, char *proc TDS_UNUSED, int line TDS_UNUSED)
33 : {
34 6 : return 0;
35 : }
36 : #endif
37 :
38 : int
39 8 : main(int argc, char *argv[])
40 : {
41 : LOGINREC *login;
42 : DBPROCESS *dbproc;
43 : char datestring[256];
44 : DBDATEREC dateinfo;
45 : #ifdef SYBMSDATETIME2
46 : DBDATEREC2 dateinfo2;
47 : #endif
48 : DBDATETIME mydatetime;
49 8 : int output_count = 0;
50 :
51 8 : set_malloc_options();
52 :
53 8 : read_login_info(argc, argv);
54 8 : printf("Starting %s\n", argv[0]);
55 8 : dbinit();
56 :
57 8 : dberrhandle(syb_err_handler);
58 8 : dbmsghandle(syb_msg_handler);
59 :
60 8 : printf("About to logon\n");
61 :
62 8 : login = dblogin();
63 8 : DBSETLPWD(login, PASSWORD);
64 8 : DBSETLUSER(login, USER);
65 8 : DBSETLAPP(login, "t0012");
66 :
67 8 : dbproc = dbopen(login, SERVER);
68 8 : if (strlen(DATABASE)) {
69 8 : dbuse(dbproc, DATABASE);
70 : }
71 8 : dbloginfree(login);
72 8 : printf("After logon\n");
73 :
74 8 : printf("creating table\n");
75 8 : sql_cmd(dbproc);
76 8 : dbsqlexec(dbproc);
77 8 : while (dbresults(dbproc) == SUCCEED) {
78 : /* nop */
79 : }
80 :
81 : /* insert */
82 8 : sql_cmd(dbproc);
83 8 : dbsqlexec(dbproc);
84 8 : while (dbresults(dbproc) == SUCCEED) {
85 : /* nop */
86 : }
87 :
88 : /* insert */
89 8 : sql_cmd(dbproc);
90 8 : dbsqlexec(dbproc);
91 8 : while (dbresults(dbproc) == SUCCEED) {
92 : /* nop */
93 : }
94 :
95 : /* select */
96 8 : sql_cmd(dbproc);
97 8 : dbsqlexec(dbproc);
98 8 : dbresults(dbproc);
99 :
100 32 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
101 16 : ++output_count;
102 : /* Print the date info */
103 16 : dbconvert(dbproc, dbcoltype(dbproc, 1), dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
104 :
105 16 : printf("%s\n", datestring);
106 :
107 : /* Break up the creation date into its constituent parts */
108 16 : memcpy(&mydatetime, (DBDATETIME *) (dbdata(dbproc, 1)), sizeof(DBDATETIME));
109 16 : dbdatecrack(dbproc, &dateinfo, &mydatetime);
110 :
111 : /* Print the parts of the creation date */
112 16 : printf("\tYear = %d.\n", dateinfo.dateyear);
113 16 : printf("\tMonth = %d.\n", dateinfo.datemonth);
114 16 : printf("\tDay of month = %d.\n", dateinfo.datedmonth);
115 16 : printf("\tDay of year = %d.\n", dateinfo.datedyear);
116 16 : printf("\tDay of week = %d.\n", dateinfo.datedweek);
117 16 : printf("\tHour = %d.\n", dateinfo.datehour);
118 16 : printf("\tMinute = %d.\n", dateinfo.dateminute);
119 16 : printf("\tSecond = %d.\n", dateinfo.datesecond);
120 16 : printf("\tMillisecond = %d.\n", dateinfo.datemsecond);
121 16 : if (dateinfo.dateyear != 1898 && dateinfo.dateyear != 2001)
122 : set_failed();
123 16 : if (dateinfo.dateminute != 24 && dateinfo.dateminute != 30)
124 : set_failed();
125 16 : if (dateinfo.datehour != 19 && dateinfo.datehour != 10)
126 : set_failed();
127 : }
128 8 : dbresults(dbproc);
129 :
130 8 : if (output_count != 2)
131 : set_failed();
132 :
133 : #ifdef SYBMSDATETIME2
134 8 : dbmsghandle(ignore_msg_handler);
135 :
136 : /* select */
137 8 : sql_cmd(dbproc);
138 8 : dbsqlexec(dbproc);
139 8 : dbresults(dbproc);
140 :
141 18 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
142 2 : int type = dbcoltype(dbproc, 1);
143 :
144 2 : ++output_count;
145 : /* Print the date info */
146 2 : dbconvert(dbproc, type, dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
147 :
148 2 : printf("%s\n", datestring);
149 :
150 : /* network not high enough for this type ! */
151 2 : if (type == SYBCHAR || type == SYBVARCHAR)
152 : break;
153 :
154 : /* Break up the creation date into its constituent parts */
155 2 : if (dbanydatecrack(dbproc, &dateinfo2, dbcoltype(dbproc, 1), dbdata(dbproc, 1)) != SUCCEED)
156 : set_failed();
157 :
158 : /* Print the parts of the creation date */
159 2 : printf("\tYear = %d.\n", dateinfo2.dateyear);
160 2 : printf("\tMonth = %d.\n", dateinfo2.datemonth);
161 2 : printf("\tDay of month = %d.\n", dateinfo2.datedmonth);
162 2 : printf("\tDay of year = %d.\n", dateinfo2.datedyear);
163 2 : printf("\tDay of week = %d.\n", dateinfo2.datedweek);
164 2 : printf("\tHour = %d.\n", dateinfo2.datehour);
165 2 : printf("\tMinute = %d.\n", dateinfo2.dateminute);
166 2 : printf("\tSecond = %d.\n", dateinfo2.datesecond);
167 2 : printf("\tNanosecond = %d.\n", dateinfo2.datensecond);
168 2 : if (dateinfo2.dateyear != 1898 || dateinfo2.datensecond != 567000000)
169 : set_failed();
170 : }
171 : #endif
172 :
173 8 : dbclose(dbproc);
174 8 : dbexit();
175 :
176 8 : if (output_count < 2 || output_count > 3)
177 : set_failed();
178 :
179 8 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
180 8 : return failed ? 1 : 0;
181 : }
|