eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
global_functions.h
Go to the documentation of this file.
1 const float Pi = 3.141593;
3 
5 
11 inline tm get_tm(int monthNbr = 0)
12 {
13  time_t t = time(0); // get time now
14  struct tm *tm_date;
15  tm_date = localtime(&t);
16  tm_date->tm_year += floor(monthNbr / 12);
17  monthNbr = monthNbr % 12;
18  while (monthNbr > 0)
19  {
20  if (tm_date->tm_mon == 11)
21  {
22  tm_date->tm_mon = 0;
23  tm_date->tm_year++;
24  }
25  else
26  {
27  tm_date->tm_mon++;
28  }
29  monthNbr--;
30  }
31  mktime(tm_date); //normalize the result
32 
33  return *tm_date;
34 }
35 
40 inline int tm2Mnbr(tm tmDate) {
41  double x;
42  time_t now;
43  time(&now);
44  x = difftime(mktime(&tmDate),now);
45  x = x / SECONDS_IN_MONTH + 0.5; //x = x / 60 / 60 / 24 / 30.4375 + 0.5;
46  return (int) x;
47 }
48 
49 /************* tm2DateStr ************/
50 // returns a string YYYY-MM-DD
51 inline string tm2DateStr(tm tmDate)
52 {
53  return (to_string(tmDate.tm_year + 1900) + "-" + to_string(tmDate.tm_mon + 1) + "-" + to_string(tmDate.tm_mday)); }
54 
60 inline void dateStr2tm(string dStr, tm * target_tm)
61 {
62  struct tm *xDate;
63  xDate = target_tm;
64  memset(xDate, 0, sizeof(struct tm));
65  strptime(dStr.c_str(), "%Y-%m-%d", xDate);
66 }
67 
73 inline int dateStr2Mnbr(string dStr)
74 {
75  struct tm xDate;
76  dateStr2tm(dStr, &xDate);
77  return tm2Mnbr(xDate);
78 }
79 
85 inline string mNbr2dateStr(int monthNbr = 0)
86 {
87  struct tm tm_date;
88  tm_date = get_tm(monthNbr);
89  return tm2DateStr(tm_date);
90 }
91 
96 inline bool is_valid_dateStr(string theDate) {
97  bool rc = true;
98  struct tm tmDate;
99 // if (theDate.length() != 10) rc = false;
100  if (!strptime(theDate.c_str(), "%Y-%m-%d", &tmDate)) rc = false;
101  return rc;
102 }
103 
107 inline float flSum(std::map<int, float>* theMap, int N) {
108  int k;
109  float theSum = 0;
110  for (k = 0; k <= N; k++) {theSum += (*theMap)[k];}
111  return theSum;
112 }
113 
119 void thousand_separator(float x, char* s) {
120  int j , i = 0, k = 0, ix = (x >= 0 )?(int)floor(x):(int)ceil(x);
121  //char s[50];
122  if( ix < 0)//test if the number is negative
123  {
124  strcpy(s,"-");
125  ix *= -1;
126  }
127  else s[0] = '0'; //strcpy(s,"");
128  int temp = ix;
129  int p = 1;
130  while( temp > 0) //counting number of digits
131  {
132  temp /= 10;
133  p *= 10;
134  i++;
135  }
136  j = i % 3;
137  p /= 10;
138 
139  while( i > 0 )//display integer number with 1000 seperator
140  {
141  s[k] = char ((ix/p) +'0');
142  ix %= p;
143  p /= 10;
144  i--;
145  k++;
146  j--;
147  if ((k % 3 == 0 && i > 0)||(j == 0 && i > 2) )
148  {
149  strcat(s,",");
150  k = 0;
151  }
152  }
153 }
154 
160 /*inline string currFormat(float x, string theCurr) {
161  char s[50], s2[30];
162  thousand_separator(x,s2);
163  strcpy(s, s2);
164  sprintf(s2,"%.02f", x - floor(x));
165  strcat(s,s2);
166 // sprintf(s,"%.02f", x);
167  if (theCurr == "EUR") strcat(s," &#8364;");
168  else strcat(s,theCurr.c_str());
169  return s;
170 }*/
171 inline string currFormat(float x, string theCurr) {
172  char s[50];
173  sprintf(s,"%.02f", x);
174  if (theCurr == "EUR") strcat(s," &#8364;");
175  else strcat(s,theCurr.c_str());
176  return s;
177 }
178 
179 
188 //using namespace std;
189 
190 string curr_format(float dv, string the_curr = "")
191 {
192 // const int width = CURR_LEN;
193  const string radix = ".";
194  const string thousands = ",";
195  string the_sign, unit;
196  if (the_curr == "EUR") unit = " &#8364;";
197  else if (the_curr == "USD") unit = " &#36;";
198  else if (the_curr == "GBP") unit = " &#163;";
199  else if (the_curr == "JPY") unit = " &#165;";
200  else unit = the_curr;
201  if (dv < 0)
202  {
203  dv = - dv;
204  the_sign = " -";
205  }
206  else
207  {
208  the_sign = "";
209  }
210  unsigned long v = (unsigned long) ((dv * 100.0) + .5);
211  string fmt,digit;
212  int i = -2;
213  do {
214  if(i == 0) {
215  fmt = radix + fmt;
216  }
217  if((i > 0) && (!(i % 3))) {
218  fmt = thousands + fmt;
219  }
220  digit = (v % 10) + '0';
221  fmt = digit + fmt;
222  v /= 10;
223  i++;
224  }
225  while((v) || (i < 1));
226  //cout << unit << setw(width) << fmt.c_str() << endl;
227  return unit + the_sign + fmt;
228 }
229 
230 
236 inline string date_format_tm(tm * the_tm)
237 {
238  return to_string(the_tm->tm_year + 1900) + "-" + to_string(the_tm->tm_mon + 1) + "-" + to_string(the_tm->tm_mday);
239 }
240 
246 inline string date_format_dateStr(string s)
247 {
248  struct tm the_tm;
249  dateStr2tm(s, &the_tm);
250  return to_string(the_tm.tm_year + 1900) + "-" + to_string(the_tm.tm_mon + 1) + "-" + to_string(the_tm.tm_mday);
251 }
252 
258 inline string round2str(float x, int nbr_digits = 0)
259 {
260  string s = "";
261  if (x < 0) s = "-" + s;
262  x = abs(x);
263  if (nbr_digits> 0)
264  {
265  s = s + to_string((int) x);
266  int x_dec = round((x - (int)x) * pow(10, nbr_digits));
267  s = s + "." + to_string(x_dec);
268  }
269  else
270  {
271  s = s + to_string((int)(x + 0.5));
272  }
273  return s;
274 }
275 
282 inline string months2yms(int m)
283 {
284  string s;
285  int yrs = (int)(m /12);
286  if (yrs >= 2) s = to_string(yrs) + " " + t_years;
287  else if (yrs >= 1) s = to_string(yrs) + " " + t_year;
288  else s = "";
289 
290  m = m - yrs * 12;
291 
292  if ((yrs != 0) && (m > 1)) s = s + " " + t_and + " ";
293 
294  if (m >= 2) s = s + to_string(m) + " " + t_months;
295  else if (m >= 1) s = s + to_string(m) + " " + t_month;
296 
297  if ((yrs == 0) && (m == 0)) s = "0 " + t_months;
298 
299  return s;
300 }
301 
311 inline string add0(int x, int n = 1)
312 {
313  string s = "";
314  int digits;
315  for (digits = 1; digits <= n; digits++)
316  {
317  if (x < pow(10,digits)) s = s + "0";
318  }
319  s = s + to_string(x);
320  return s;
321 }
string currFormat(float x, string theCurr)
const string t_month
General words.
Definition: texts_Eng_UK.h:11
string round2str(float x, int nbr_digits=0)
const string t_year
Definition: texts_Eng_UK.h:12
bool is_valid_dateStr(string theDate)
string date_format_dateStr(string s)
const float Pi
global variables
string months2yms(int m)
const string t_months
Definition: texts_Eng_UK.h:13
string add0(int x, int n=1)
tm get_tm(int monthNbr=0)
global functions
void thousand_separator(float x, char *s)
string curr_format(float dv, string the_curr="")
string tm2DateStr(tm tmDate)
int tm2Mnbr(tm tmDate)
string date_format_tm(tm *the_tm)
int dateStr2Mnbr(string dStr)
const string t_and
Definition: texts_Eng_UK.h:15
void dateStr2tm(string dStr, tm *target_tm)
const string t_years
Definition: texts_Eng_UK.h:14
#define SECONDS_IN_MONTH
Marco's:
Definition: efp.cpp:65
float flSum(std::map< int, float > *theMap, int N)
string mNbr2dateStr(int monthNbr=0)