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 10 : 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 10 : int output_count = 0;
50 :
51 10 : set_malloc_options();
52 :
53 10 : read_login_info(argc, argv);
54 10 : printf("Starting %s\n", argv[0]);
55 10 : dbinit();
56 :
57 10 : dberrhandle(syb_err_handler);
58 10 : dbmsghandle(syb_msg_handler);
59 :
60 10 : printf("About to logon\n");
61 :
62 10 : login = dblogin();
63 10 : DBSETLPWD(login, PASSWORD);
64 10 : DBSETLUSER(login, USER);
65 10 : DBSETLAPP(login, "t0012");
66 :
67 10 : dbproc = dbopen(login, SERVER);
68 10 : if (strlen(DATABASE)) {
69 10 : dbuse(dbproc, DATABASE);
70 : }
71 10 : dbloginfree(login);
72 10 : printf("After logon\n");
73 :
74 10 : printf("creating table\n");
75 10 : sql_cmd(dbproc);
76 10 : dbsqlexec(dbproc);
77 10 : while (dbresults(dbproc) == SUCCEED) {
78 : /* nop */
79 : }
80 :
81 : /* insert */
82 10 : sql_cmd(dbproc);
83 10 : dbsqlexec(dbproc);
84 10 : while (dbresults(dbproc) == SUCCEED) {
85 : /* nop */
86 : }
87 :
88 : /* insert */
89 10 : sql_cmd(dbproc);
90 10 : dbsqlexec(dbproc);
91 10 : while (dbresults(dbproc) == SUCCEED) {
92 : /* nop */
93 : }
94 :
95 : /* select */
96 10 : sql_cmd(dbproc);
97 10 : dbsqlexec(dbproc);
98 10 : dbresults(dbproc);
99 :
100 40 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
101 20 : ++output_count;
102 : /* Print the date info */
103 20 : dbconvert(dbproc, dbcoltype(dbproc, 1), dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
104 :
105 20 : printf("%s\n", datestring);
106 :
107 : /* Break up the creation date into its constituent parts */
108 20 : memcpy(&mydatetime, (DBDATETIME *) (dbdata(dbproc, 1)), sizeof(DBDATETIME));
109 20 : dbdatecrack(dbproc, &dateinfo, &mydatetime);
110 :
111 : /* Print the parts of the creation date */
112 20 : printf("\tYear = %d.\n", dateinfo.dateyear);
113 20 : printf("\tMonth = %d.\n", dateinfo.datemonth);
114 20 : printf("\tDay of month = %d.\n", dateinfo.datedmonth);
115 20 : printf("\tDay of year = %d.\n", dateinfo.datedyear);
116 20 : printf("\tDay of week = %d.\n", dateinfo.datedweek);
117 20 : printf("\tHour = %d.\n", dateinfo.datehour);
118 20 : printf("\tMinute = %d.\n", dateinfo.dateminute);
119 20 : printf("\tSecond = %d.\n", dateinfo.datesecond);
120 20 : printf("\tMillisecond = %d.\n", dateinfo.datemsecond);
121 20 : if (dateinfo.dateyear != 1898 && dateinfo.dateyear != 2001)
122 : set_failed();
123 20 : if (dateinfo.dateminute != 24 && dateinfo.dateminute != 30)
124 : set_failed();
125 20 : if (dateinfo.datehour != 19 && dateinfo.datehour != 10)
126 : set_failed();
127 : }
128 10 : dbresults(dbproc);
129 :
130 10 : if (output_count != 2)
131 : set_failed();
132 :
133 : #ifdef SYBMSDATETIME2
134 10 : dbmsghandle(ignore_msg_handler);
135 :
136 : /* select */
137 10 : sql_cmd(dbproc);
138 10 : dbsqlexec(dbproc);
139 10 : dbresults(dbproc);
140 :
141 24 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
142 4 : int type = dbcoltype(dbproc, 1);
143 :
144 4 : ++output_count;
145 : /* Print the date info */
146 4 : dbconvert(dbproc, type, dbdata(dbproc, 1), dbdatlen(dbproc, 1), SYBCHAR, (BYTE*) datestring, -1);
147 :
148 4 : printf("%s\n", datestring);
149 :
150 : /* network not high enough for this type ! */
151 4 : if (type == SYBCHAR || type == SYBVARCHAR)
152 : break;
153 :
154 : /* Break up the creation date into its constituent parts */
155 4 : if (dbanydatecrack(dbproc, &dateinfo2, dbcoltype(dbproc, 1), dbdata(dbproc, 1)) != SUCCEED)
156 : set_failed();
157 :
158 : /* Print the parts of the creation date */
159 4 : printf("\tYear = %d.\n", dateinfo2.dateyear);
160 4 : printf("\tMonth = %d.\n", dateinfo2.datemonth);
161 4 : printf("\tDay of month = %d.\n", dateinfo2.datedmonth);
162 4 : printf("\tDay of year = %d.\n", dateinfo2.datedyear);
163 4 : printf("\tDay of week = %d.\n", dateinfo2.datedweek);
164 4 : printf("\tHour = %d.\n", dateinfo2.datehour);
165 4 : printf("\tMinute = %d.\n", dateinfo2.dateminute);
166 4 : printf("\tSecond = %d.\n", dateinfo2.datesecond);
167 4 : printf("\tNanosecond = %d.\n", dateinfo2.datensecond);
168 4 : if (dateinfo2.dateyear != 1898 || dateinfo2.datensecond != 567000000)
169 : set_failed();
170 : }
171 : #endif
172 :
173 10 : dbclose(dbproc);
174 10 : dbexit();
175 :
176 10 : if (output_count < 2 || output_count > 3)
177 : set_failed();
178 :
179 10 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
180 10 : return failed ? 1 : 0;
181 : }
|