1 #ifndef ERF_EPOCH_TIME_H_
2 #define ERF_EPOCH_TIME_H_
13 #define timegm _mkgmtime
23 AMREX_GPU_HOST AMREX_FORCE_INLINE
30 unsigned char ch =
static_cast<unsigned char>(dt[
idx]);
31 if (ch ==
'\0' || !std::isdigit(ch)) {
34 value = 10 * value + (dt[
idx] -
'0');
47 AMREX_GPU_HOST AMREX_FORCE_INLINE
49 getEpochTime (
const std::string& dateTime,
const std::string& dateTimeFormat)
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);
125 getTimestamp (
const double epoch_real,
const std::string& datetime_format,
bool add_long_frac=
true)
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;
AMREX_GPU_HOST AMREX_FORCE_INLINE std::time_t getEpochTime(const std::string &dateTime, const std::string &dateTimeFormat)
Definition: ERF_EpochTime.H:49
AMREX_GPU_HOST AMREX_FORCE_INLINE bool parse_fixed_width_int(const char *&dt, int width, int &value)
Definition: ERF_EpochTime.H:25
AMREX_FORCE_INLINE std::string getTimestamp(const double epoch_real, const std::string &datetime_format, bool add_long_frac=true)
Definition: ERF_EpochTime.H:125
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:365