Replace localtime() with thread-safe fmt::localtime()

This commit is contained in:
Richard Berger
2021-05-07 19:53:18 -04:00
parent b6b70b2033
commit 86d4ec7a45
3 changed files with 18 additions and 16 deletions

View File

@ -22,6 +22,7 @@
#include "neighbor.h"
#include "suffix.h"
#include "update.h"
#include "fmt/chrono.h"
#include <ctime>
@ -283,14 +284,13 @@ void Bond::write_file(int narg, char **arg)
"DATE: {}\n", table_file, date);
fp = fopen(table_file.c_str(),"a");
} else {
char datebuf[16];
time_t tv = time(nullptr);
strftime(datebuf,15,"%Y-%m-%d",localtime(&tv));
std::tm current_date = fmt::localtime(tv);
utils::logmesg(lmp,"Creating table file {} with "
"DATE: {}\n", table_file, datebuf);
"DATE: {:%Y-%m-%d}\n", table_file, current_date);
fp = fopen(table_file.c_str(),"w");
if (fp) fmt::print(fp,"# DATE: {} UNITS: {} Created by bond_write\n",
datebuf, update->unit_style);
if (fp) fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} Created by bond_write\n",
current_date, update->unit_style);
}
if (fp == nullptr)
error->one(FLERR,"Cannot open bond_write file {}: {}",

View File

@ -27,6 +27,7 @@
#include "group.h"
#include "memory.h"
#include "error.h"
#include "fmt/chrono.h"
using namespace LAMMPS_NS;
@ -309,8 +310,8 @@ void DumpDCD::write_dcd_header(const char *remarks)
uint32_t out_integer;
float out_float;
char title_string[200];
time_t cur_time;
struct tm *tmbuf;
time_t t;
std::tm current_time;
int ntimestep = update->ntimestep;
@ -345,10 +346,11 @@ void DumpDCD::write_dcd_header(const char *remarks)
strncpy(title_string,remarks,80);
title_string[79] = '\0';
fwrite(title_string,80,1,fp);
cur_time=time(nullptr);
tmbuf=localtime(&cur_time);
t = time(nullptr);
current_time = fmt::localtime(t);
std::string s = fmt::format("REMARKS Created {:%d %B,%Y at %H:%M}", current_time);
memset(title_string,' ',81);
strftime(title_string,80,"REMARKS Created %d %B,%Y at %H:%M",tmbuf);
strncpy(title_string, s.c_str(), 80);
fwrite(title_string,80,1,fp);
fwrite_int32(fp,164);
fwrite_int32(fp,4);

View File

@ -30,6 +30,7 @@
#include "neighbor.h"
#include "suffix.h"
#include "update.h"
#include "fmt/chrono.h"
#include <cfloat> // IWYU pragma: keep
#include <climits> // IWYU pragma: keep
@ -1738,14 +1739,13 @@ void Pair::write_file(int narg, char **arg)
table_file, date);
fp = fopen(table_file.c_str(),"a");
} else {
char datebuf[16];
time_t tv = time(nullptr);
strftime(datebuf,15,"%Y-%m-%d",localtime(&tv));
utils::logmesg(lmp,"Creating table file {} with DATE: {}\n",
table_file, datebuf);
std::tm current_date = fmt::localtime(tv);
utils::logmesg(lmp,"Creating table file {} with DATE: {:%Y-%m-%d}\n",
table_file, current_date);
fp = fopen(table_file.c_str(),"w");
if (fp) fmt::print(fp,"# DATE: {} UNITS: {} Created by pair_write\n",
datebuf, update->unit_style);
if (fp) fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} Created by pair_write\n",
current_date, update->unit_style);
}
if (fp == nullptr)
error->one(FLERR,"Cannot open pair_write file {}: {}",