ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ERF_EpochTime.H File Reference
#include <string>
#include <sstream>
#include <iomanip>
#include <ctime>
Include dependency graph for ERF_EpochTime.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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 amrex::Real epoch_real, const std::string &datetime_format)
 

Function Documentation

◆ getEpochTime()

AMREX_GPU_HOST AMREX_FORCE_INLINE std::time_t getEpochTime ( const std::string &  dateTime,
const std::string &  dateTimeFormat 
)
16 {
17  std::tm tmTime{};
18  const char* dt = dateTime.c_str();
19  const char* fmt = dateTimeFormat.c_str();
20 
21  // Walk both the format string and the date string together
22  while (*fmt && *dt) {
23  if (*fmt == '%') {
24  ++fmt;
25  switch (*fmt) {
26  case 'Y': // 4-digit year
27  tmTime.tm_year = (dt[0]-'0')*1000 + (dt[1]-'0')*100
28  + (dt[2]-'0')*10 + (dt[3]-'0');
29  tmTime.tm_year -= 1900;
30  dt += 4;
31  break;
32  case 'm': // 2-digit month
33  tmTime.tm_mon = (dt[0]-'0')*10 + (dt[1]-'0');
34  tmTime.tm_mon -= 1; // struct tm expects 0-11
35  dt += 2;
36  break;
37  case 'd': // 2-digit day
38  tmTime.tm_mday = (dt[0]-'0')*10 + (dt[1]-'0');
39  dt += 2;
40  break;
41  case 'H': // 2-digit hour
42  tmTime.tm_hour = (dt[0]-'0')*10 + (dt[1]-'0');
43  dt += 2;
44  break;
45  case 'M': // 2-digit minute
46  tmTime.tm_min = (dt[0]-'0')*10 + (dt[1]-'0');
47  dt += 2;
48  break;
49  case 'S': // 2-digit second
50  tmTime.tm_sec = (dt[0]-'0')*10 + (dt[1]-'0');
51  dt += 2;
52  break;
53  default:
54  // Unsupported format specifier
55  return -1;
56  }
57  } else {
58  // Literal character, must match
59  if (*fmt != *dt) {
60  return -1; // mismatch
61  }
62  ++dt;
63  }
64  ++fmt;
65  }
66 
67  return timegm(&tmTime);
68 }

Referenced by ERF::ReadParameters().

Here is the caller graph for this function:

◆ getTimestamp()

AMREX_FORCE_INLINE std::string getTimestamp ( const amrex::Real  epoch_real,
const std::string &  datetime_format 
)
73 {
74  auto epoch_nearest_sec = static_cast<std::time_t>(epoch_real);
75  std::tm *time_info = std::gmtime(&epoch_nearest_sec);
76 
77  char buffer[80];
78  std::strftime(buffer, sizeof(buffer), datetime_format.c_str(), time_info);
79  std::string str_nearest_sec(buffer);
80 
81  double frac_sec = epoch_real - epoch_nearest_sec;
82  snprintf(buffer, 80, "%.6f", frac_sec);
83  AMREX_ASSERT(buffer[0] == '0');
84  std::string str_frac_sec(buffer);
85 
86  return str_nearest_sec + str_frac_sec.substr(1);
87 }

Referenced by ERF::Evolve().

Here is the caller graph for this function: