Skip to content

Commit 73444ee

Browse files
committed
Implement TR31_USE_SSCANF_DATETIME
This CMake option and C preprocessor definition ensures that sscanf() will be used for date/time parsing even when strptime() is available. It also makes it much easier to test the sscanf() date/time parsing.
1 parent 0d4425f commit 73444ee

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

.github/workflows/ubuntu-build.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ jobs:
4242
- name: Test
4343
run: ctest --test-dir build --output-on-failure
4444

45+
build-ubuntu-sscanf:
46+
name: Ubuntu (sscanf)
47+
runs-on: ubuntu-latest
48+
49+
steps:
50+
- name: Install dependencies
51+
run: |
52+
sudo apt-get update
53+
sudo apt-get install -y libmbedtls-dev
54+
55+
- name: Checkout
56+
uses: actions/checkout@v4
57+
with:
58+
submodules: recursive
59+
- run: git describe --always --dirty
60+
61+
- name: Configure CMake with sscanf() for date/time
62+
run: |
63+
cmake -B build \
64+
-DCMAKE_BUILD_TYPE="Debug" \
65+
-DTR31_USE_SSCANF_DATETIME=ON
66+
67+
- name: Build
68+
run: cmake --build build -j 4
69+
70+
- name: Test
71+
run: ctest --test-dir build --output-on-failure -j 4
72+
4573
build-ubuntu-legacy-release:
4674
strategy:
4775
fail-fast: false

src/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ else()
9393
# set as cache entry to persist across multiple builds
9494
set(TR31_ENABLE_DATETIME_CONVERSION OFF CACHE INTERNAL "Date/time conversion availability")
9595
endif()
96+
# option to use sscanf() for date/time parsing even when strptime() is
97+
# available
98+
option(TR31_USE_SSCANF_DATETIME "Use sscanf() for date/time parsing")
99+
if(HAVE_STRPTIME AND NOT TR31_USE_SSCANF_DATETIME)
100+
message(STATUS "Using strptime() for date/time parsing")
101+
else()
102+
message(STATUS "Using sscanf() for date/time parsing")
103+
endif()
96104

97105
include(GNUInstallDirs) # provides CMAKE_INSTALL_* variables and good defaults for install()
98106

src/tr31_config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
#cmakedefine HAVE_TIMEGM
3434
#cmakedefine HAVE_MKGMTIME
3535
#cmakedefine TR31_ENABLE_DATETIME_CONVERSION
36+
#cmakedefine TR31_USE_SSCANF_DATETIME
3637

3738
#endif

src/tr31_strings.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#ifdef HAVE_TIME_H
4141
#include <time.h>
4242
#endif
43-
#ifndef HAVE_STRPTIME
43+
#if !defined(HAVE_STRPTIME) || defined(TR31_USE_SSCANF_DATETIME)
4444
#include <stdio.h> // For sscanf()
4545
#endif
4646
#endif // TR31_ENABLE_DATETIME_CONVERSION
@@ -512,7 +512,7 @@ static int tr31_opt_block_iso8601_get_string(const struct tr31_opt_ctx_t* opt_bl
512512
{
513513
#ifdef TR31_ENABLE_DATETIME_CONVERSION
514514
char* iso8601_str;
515-
#ifdef HAVE_STRPTIME
515+
#if defined(HAVE_STRPTIME) && !defined(TR31_USE_SSCANF_DATETIME)
516516
char* ptr;
517517
#else
518518
int r;
@@ -537,7 +537,7 @@ static int tr31_opt_block_iso8601_get_string(const struct tr31_opt_ctx_t* opt_bl
537537
// See ANSI X9.143:2021, 6.3.6.13, table 21
538538
// See ANSI X9.143:2021, 6.3.6.14, table 22
539539
memset(&ztm, 0, sizeof(ztm));
540-
#ifdef HAVE_STRPTIME
540+
#if defined(HAVE_STRPTIME) && !defined(TR31_USE_SSCANF_DATETIME)
541541
switch (opt_block->data_length) {
542542
case 0x13 - 4: // YYYYMMDDhhmmssZ
543543
ptr = strptime(iso8601_str, "%Y%m%d%H%M%SZ", &ztm);

0 commit comments

Comments
 (0)