1919 */
2020
2121#include "tr31.h"
22+ #include "tr31_config.h"
2223#include "tr31_strings.h"
2324
2425#include "crypto_mem.h"
@@ -1361,20 +1362,30 @@ static int populate_opt_blocks(const struct tr31_tool_options_t* options, struct
13611362
13621363 if (strcmp (options -> export_opt_block_TC_str , "now" ) == 0 ) {
13631364 time_t lt ; // Calendar/Unix/POSIX time in local time
1364- struct tm * ztm ; // Time structure in UTC
1365+ struct tm ztm ; // Time structure in UTC
1366+ struct tm * tm_ptr ; // Result of gmtime functions
13651367 size_t ret ;
13661368
13671369 lt = time (NULL );
13681370 if (lt == (time_t )- 1 ) {
13691371 fprintf (stderr , "Failed to obtain current date/time: %s\n" , strerror (errno ));
13701372 return 1 ;
13711373 }
1372- ztm = gmtime (& lt );
1373- if (ztm == NULL ) {
1374+ #ifdef HAVE_GMTIME_R
1375+ tm_ptr = gmtime_r (& lt , & ztm );
1376+ if (!tm_ptr ) {
13741377 fprintf (stderr , "Failed to convert current date/time to UTC\n" );
13751378 return 1 ;
13761379 }
1377- ret = strftime (iso8601_now , sizeof (iso8601_now ), "%Y%m%d%H%M%SZ" , ztm );
1380+ #else
1381+ tm_ptr = gmtime (& lt );
1382+ if (!tm_ptr ) {
1383+ fprintf (stderr , "Failed to convert current date/time to UTC\n" );
1384+ return 1 ;
1385+ }
1386+ ztm = * tm_ptr ;
1387+ #endif
1388+ ret = strftime (iso8601_now , sizeof (iso8601_now ), "%Y%m%d%H%M%SZ" , & ztm );
13781389 if (!ret ) {
13791390 fprintf (stderr , "Failed to convert current date/time to ISO 8601\n" );
13801391 return 1 ;
@@ -1396,20 +1407,30 @@ static int populate_opt_blocks(const struct tr31_tool_options_t* options, struct
13961407
13971408 if (strcmp (options -> export_opt_block_TS_str , "now" ) == 0 ) {
13981409 time_t lt ; // Calendar/Unix/POSIX time in local time
1399- struct tm * ztm ; // Time structure in UTC
1410+ struct tm ztm ; // Time structure in UTC
1411+ struct tm * tm_ptr ; // Result of gmtime functions
14001412 size_t ret ;
14011413
14021414 lt = time (NULL );
14031415 if (lt == (time_t )- 1 ) {
14041416 fprintf (stderr , "Failed to obtain current date/time: %s\n" , strerror (errno ));
14051417 return 1 ;
14061418 }
1407- ztm = gmtime (& lt );
1408- if (ztm == NULL ) {
1419+ #ifdef HAVE_GMTIME_R
1420+ tm_ptr = gmtime_r (& lt , & ztm );
1421+ if (!tm_ptr ) {
14091422 fprintf (stderr , "Failed to convert current date/time to UTC\n" );
14101423 return 1 ;
14111424 }
1412- ret = strftime (iso8601_now , sizeof (iso8601_now ), "%Y%m%d%H%M%SZ" , ztm );
1425+ #else
1426+ tm_ptr = gmtime (& lt );
1427+ if (!tm_ptr ) {
1428+ fprintf (stderr , "Failed to convert current date/time to UTC\n" );
1429+ return 1 ;
1430+ }
1431+ ztm = * tm_ptr ;
1432+ #endif
1433+ ret = strftime (iso8601_now , sizeof (iso8601_now ), "%Y%m%d%H%M%SZ" , & ztm );
14131434 if (!ret ) {
14141435 fprintf (stderr , "Failed to convert current date/time to ISO 8601\n" );
14151436 return 1 ;
0 commit comments