NCBI C++ ToolKit
strftime.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 2020 Frediano Ziglio
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 /*
21  * Purpose: test tds_strftime.
22  * This is a wrapper to strftime for portability and extension.
23  */
24 #include "common.h"
25 #include <freetds/convert.h>
26 #include <freetds/time.h>
27 
28 #include <freetds/test_assert.h>
29 
30 static void
31 test(const TDSDATEREC* dr, int prec, const char *fmt, const char *expected, int line)
32 {
33  char out[256];
34  char *format = strdup(fmt);
35  assert(format != NULL);
36 
37  tds_strftime(out, sizeof(out), format, dr, prec);
38 
39  if (strcmp(out, expected) != 0) {
40  fprintf(stderr, "%d: Wrong results got '%s' expected '%s'\n", line, out, expected);
41  exit(1);
42  }
43 
44  free(format);
45 }
46 
47 int
48 main(int argc, char **argv)
49 {
50  TDSDATEREC dr;
51 
52  memset(&dr, 0, sizeof(dr));
53 
54 #define TEST(prec, fmt, exp) test(&dr, prec, fmt, exp, __LINE__)
55 
56  /* %z extension, second decimals */
57  TEST(3, "%z", "000");
58  TEST(0, "x%z", "x");
59  TEST(3, ".%z", ".000");
60  dr.decimicrosecond = 1234567;
61  TEST(5, ".%z", ".12345");
62  TEST(4, "%z", "1234");
63  TEST(4, "%z%%", "1234%");
64  TEST(4, "%z%H", "123400");
65 
66  /* not terminated format, should not overflow */
67  TEST(3, "%", "%");
68 
69  /* not portable %l, we should handle it */
70  TEST(0, "%l", "12");
71  TEST(0, "%%%l", "%12");
72  dr.hour = 16;
73  TEST(0, "%l", " 4");
74 
75  /* not portable %e, we should handle it */
76  dr.day = 23;
77  TEST(0, "%e", "23");
78  dr.day = 5;
79  TEST(0, "x%e", "x 5");
80  return 0;
81 }
std::ofstream out("events_result.xml")
main entry point for tests
static const char * expected[]
Definition: bcp.c:42
#define tds_strftime
#define NULL
Definition: ncbistd.hpp:225
exit(2)
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define strdup
Definition: ncbi_ansi_ext.h:70
static Format format
Definition: njn_ioutil.cpp:53
#define assert(x)
Definition: srv_diag.hpp:58
int main(int argc, char **argv)
Definition: strftime.c:48
#define TEST(prec, fmt, exp)
static void test(const TDSDATEREC *dr, int prec, const char *fmt, const char *expected, int line)
Definition: strftime.c:31
Used by tds_datecrack.
Definition: tds.h:180
void free(voidpf ptr)
Modified on Fri Sep 20 14:57:53 2024 by modify_doxy.py rev. 669887
HHS Vulnerability Disclosure