#include <string>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <cmath>
#include <cstdio>
Go to the source code of this file.
|
| AMREX_GPU_HOST AMREX_FORCE_INLINE bool | parse_fixed_width_int (const char *&dt, int width, int &value) |
| |
| AMREX_GPU_HOST AMREX_FORCE_INLINE std::time_t | getEpochTime (const std::string &dateTime, const std::string &dateTimeFormat) |
| |
| AMREX_FORCE_INLINE std::string | getTimestamp (const double epoch_real, const std::string &datetime_format, bool add_long_frac=true) |
| |
◆ getEpochTime()
| AMREX_GPU_HOST AMREX_FORCE_INLINE std::time_t getEpochTime |
( |
const std::string & |
dateTime, |
|
|
const std::string & |
dateTimeFormat |
|
) |
| |
Convert a formatted date-time string to epoch time.
- Parameters
-
| [in] | dateTime | String containing the date and time. |
| [in] | dateTimeFormat | Format string describing the layout of dateTime. |
- Returns
- Epoch time as std::time_t, or -1 on failure.
52 const char* dt = dateTime.c_str();
53 const char* fmt = dateTimeFormat.c_str();
63 tmTime.tm_year = parsed_value;
64 tmTime.tm_year -= 1900;
68 tmTime.tm_mon = parsed_value;
73 tmTime.tm_mday = parsed_value;
77 tmTime.tm_hour = parsed_value;
81 tmTime.tm_min = parsed_value;
85 tmTime.tm_sec = parsed_value;
101 if (*fmt !=
'\0' || *dt !=
'\0') {
105 if (tmTime.tm_mon < 0 || tmTime.tm_mon > 11 ||
106 tmTime.tm_mday < 1 || tmTime.tm_mday > 31 ||
107 tmTime.tm_hour < 0 || tmTime.tm_hour > 23 ||
108 tmTime.tm_min < 0 || tmTime.tm_min > 59 ||
109 tmTime.tm_sec < 0 || tmTime.tm_sec > 60) {
113 return timegm(&tmTime);
AMREX_GPU_HOST AMREX_FORCE_INLINE bool parse_fixed_width_int(const char *&dt, int width, int &value)
Definition: ERF_EpochTime.H:25
Referenced by ERF::ReadParameters().
◆ getTimestamp()
| AMREX_FORCE_INLINE std::string getTimestamp |
( |
const double |
epoch_real, |
|
|
const std::string & |
datetime_format, |
|
|
bool |
add_long_frac = true |
|
) |
| |
Convert epoch time to a formatted timestamp string.
- Parameters
-
| [in] | epoch_real | Epoch time in seconds. |
| [in] | datetime_format | Format string for the resulting timestamp. |
| [in] | add_long_frac | Whether to append fractional seconds to the output. |
- Returns
- Formatted timestamp string.
127 std::time_t epoch_nearest_sec;
128 long long frac_usec = 0;
142 constexpr
long long usec_per_sec = 1000000;
143 const long long total_usec = std::llround(epoch_real * 1.e6);
144 long long whole_sec = total_usec / usec_per_sec;
145 frac_usec = total_usec % usec_per_sec;
146 if (frac_usec < 0) { whole_sec -= 1; frac_usec += usec_per_sec; }
147 epoch_nearest_sec =
static_cast<std::time_t
>(whole_sec);
149 epoch_nearest_sec =
static_cast<std::time_t
>(epoch_real);
152 std::tm *time_info = std::gmtime(&epoch_nearest_sec);
155 std::strftime(buffer,
sizeof(buffer), datetime_format.c_str(), time_info);
156 std::string str_nearest_sec(buffer);
159 snprintf(buffer, 80,
".%06lld", frac_usec);
160 return str_nearest_sec + std::string(buffer);
162 return str_nearest_sec;
Referenced by ERF::Evolve(), ERF::Write3DPlotFile(), ERF::writeJobInfo(), and ERF::WriteSubvolume().
◆ parse_fixed_width_int()
| AMREX_GPU_HOST AMREX_FORCE_INLINE bool parse_fixed_width_int |
( |
const char *& |
dt, |
|
|
int |
width, |
|
|
int & |
value |
|
) |
| |
Parse an integer of fixed width from a string.
- Parameters
-
| [in,out] | dt | String to parse from; advanced by width on success. |
| [in] | width | Number of characters to parse. |
| [out] | value | Parsed integer value. |
- Returns
- True if parsing was successful, false otherwise.
30 unsigned char ch =
static_cast<unsigned char>(dt[
idx]);
31 if (ch ==
'\0' || !std::isdigit(ch)) {
34 value = 10 * value + (dt[
idx] -
'0');
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:365
Referenced by getEpochTime().