LCOV - code coverage report
Current view: top level - src/odbc/unittests - date.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 58 62 93.5 %
Date: 2026-07-19 14:02:14 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : #include <odbcss.h>
       3             : 
       4             : static int
       5          62 : DoTest(int n)
       6             : {
       7             :         SQLCHAR output[256];
       8             : 
       9             :         SQLSMALLINT colType;
      10             :         SQLULEN colSize;
      11             :         SQLSMALLINT colScale, colNullable;
      12             :         SQLLEN dataSize;
      13             :         const char *expect;
      14             : 
      15             :         TIMESTAMP_STRUCT ts;
      16             : 
      17          62 :         switch (n) {
      18          20 :         case 0:
      19             :         case 1:
      20          20 :                 odbc_command("select convert(datetime, '2002-12-27 18:43:21')");
      21          20 :                 expect = "2002-12-27 18:43:21.000";
      22          20 :                 break;
      23             : 
      24          10 :         case 2:
      25             :                 /* datetime2 introduced with MSSQL 2008 */
      26          10 :                 if (!odbc_db_is_microsoft() || odbc_db_version_int() < 0x0a000000u)
      27             :                         return -1;
      28             :                 /* Recent feature added to trunk - Full precision for datetime2 */
      29           4 :                 odbc_command("select convert(datetime2, '2002-12-27 18:43:21')");
      30           4 :                 expect = "2002-12-27 18:43:21.0000000";
      31           4 :                 break;
      32             : 
      33          10 :         case 3:
      34          10 :                 SQLSetConnectAttr(odbc_conn, SQL_COPT_TDSODBC_DATETIME_FORMAT, T("**%d-%m-%Y %H:%M:%S**"), SQL_NTS);
      35          10 :                 odbc_command("select convert(datetime, '2002-12-27 18:43:21')");
      36          10 :                 expect = "**27-12-2002 18:43:21**";
      37          10 :                 break;
      38             : 
      39          10 :         case 4:
      40             :                 /* MS datetime2 wire format introduced in TDS 7.3
      41             :                  * (earlier TDS versions use NVARCHAR wire format here) */
      42          10 :                 if (odbc_tds_version() < 0x703)
      43             :                         return 0;
      44           4 :                 SQLSetConnectAttr(odbc_conn, SQL_COPT_TDSODBC_DATETIME_FORMAT, T("**%d-%m-%Y %H:%M:%S.%z**"), SQL_NTS);
      45           4 :                 odbc_command("select convert(datetime2(7), '2002-12-27 18:43:21')");
      46           4 :                 expect = "**27-12-2002 18:43:21.0000000**";
      47           4 :                 break;
      48             : 
      49           4 :         case 5:
      50             :                 /* MS date-only and time-only wire formats introduced in TDS 7.3 */
      51           4 :                 if (odbc_tds_version() < 0x703)
      52             :                         return 0;
      53           4 :                 SQLSetConnectAttr(odbc_conn, SQL_COPT_TDSODBC_DATE_FORMAT, T("**%d-%m-%Y**"), SQL_NTS);
      54           4 :                 odbc_command("select convert(date, '2002-12-27 18:43:21')");
      55           4 :                 expect = "**27-12-2002**";
      56           4 :                 break;
      57             : 
      58           4 :         case 6:
      59           4 :                 if (odbc_tds_version() < 0x703)
      60             :                         return 0;
      61           4 :                 SQLSetConnectAttr(odbc_conn, SQL_COPT_TDSODBC_TIME_FORMAT, T("**%H:%M:%S**"), SQL_NTS);
      62           4 :                 odbc_command("select convert(time, '2002-12-27 18:43:21')");
      63           4 :                 expect = "**18:43:21**";
      64           4 :                 break;
      65             : 
      66           4 :         default:
      67           4 :                 printf("Done.\n");
      68           4 :                 return 0;
      69             :         }
      70             : 
      71          46 :         CHKFetch("SI");
      72          46 :         CHKDescribeCol(1, (SQLTCHAR *) output, sizeof(output) / sizeof(SQLWCHAR), NULL, &colType, &colSize, &colScale, &colNullable,
      73             :                        "S");
      74             : 
      75             :         /* Case 0 tests binding SQL_C_TIMESTAMP to result; other cases are testing binding to char. */
      76          46 :         if (n == 0) {
      77          10 :                 memset(&ts, 0, sizeof(ts));
      78          10 :                 CHKGetData(1, SQL_C_TIMESTAMP, &ts, sizeof(ts), &dataSize, "S");
      79          10 :                 sprintf((char *) output, "%04d-%02d-%02d %02d:%02d:%02d.000", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
      80             :         } else {
      81          36 :                 CHKGetData(1, SQL_C_CHAR, output, sizeof(output), &dataSize, "S");
      82             :         }
      83             : 
      84          46 :         printf("Date returned: \"%s\"\n", output);
      85          46 :         if (strcmp((char *) output, expect) != 0) {
      86           0 :                 fprintf(stderr, "Invalid returned date; expected \"%s\".\n", expect);
      87           0 :                 return 1;
      88             :         }
      89             : 
      90          46 :         if ((int) colSize < dataSize) {
      91           0 :                 fprintf(stderr, "Column described as size %d, but data length was %d\n", (int) colSize, (int) dataSize);
      92           0 :                 return 1;
      93             :         }
      94          46 :         CHKFetch("No");
      95          46 :         CHKCloseCursor("SI");
      96          46 :         return -1;
      97             : }
      98             : 
      99          10 : TEST_MAIN()
     100             : {
     101          10 :         int i = 0;
     102             :         int exit_status;
     103             : 
     104          10 :         odbc_use_version3 = false;
     105          10 :         odbc_connect();
     106          72 :         while ((exit_status = DoTest(i++)) == -1)
     107          52 :                 continue;
     108          10 :         odbc_disconnect();
     109             : 
     110          10 :         return exit_status;
     111             : }

Generated by: LCOV version 1.13