From 86d4ec7a45698e4168318f821894a95cfbfa216d Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 7 May 2021 19:53:18 -0400 Subject: [PATCH] Replace localtime() with thread-safe fmt::localtime() --- src/bond.cpp | 10 +++++----- src/dump_dcd.cpp | 12 +++++++----- src/pair.cpp | 12 ++++++------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/bond.cpp b/src/bond.cpp index 049d5da823..22a7b27eda 100644 --- a/src/bond.cpp +++ b/src/bond.cpp @@ -22,6 +22,7 @@ #include "neighbor.h" #include "suffix.h" #include "update.h" +#include "fmt/chrono.h" #include @@ -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 {}: {}", diff --git a/src/dump_dcd.cpp b/src/dump_dcd.cpp index 3bbf929845..0b02f1f1f6 100644 --- a/src/dump_dcd.cpp +++ b/src/dump_dcd.cpp @@ -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); diff --git a/src/pair.cpp b/src/pair.cpp index 42ec13374b..7f41114fc0 100644 --- a/src/pair.cpp +++ b/src/pair.cpp @@ -30,6 +30,7 @@ #include "neighbor.h" #include "suffix.h" #include "update.h" +#include "fmt/chrono.h" #include // IWYU pragma: keep #include // 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 {}: {}",