2
* File: helperfunctions.h
5
* Created on October 14, 2010, 3:17 PM
8
#ifndef HELPERFUNCTIONS_H
9
#define HELPERFUNCTIONS_H
24
/// returns a string with the content given in x.
26
inline std::string stringify(T const& x)
31
std::cout<< "couldn't convert to string!"<<std::endl;
32
//throw BadConversion(std::string("stringify(")+ typeid(x).name() + ")");
35
/// returns the value of s, with the give type T.
37
inline T convertTo(std::string const& s,bool failIfLeftoverChars = false)
39
std::istringstream i(s);
42
if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
43
std::cout<<"couldn't convert from string!"<<std::endl;
44
//err_msg( "couldn't convert from string!");
45
//throw BadConversion("couldn't convert from string!");
48
/// appends a number to a string in the scheme: abc_1.
49
std::string append_num(std::string s,int num)
51
std::stringstream buffer;
56
///returns a string containing a 3 digit number followed by Hz, KHz, MHz respectively.
57
std::string append_hertz(int freq) {
59
if (freq >= 1000000) {
60
frequs.append(stringify(freq / 1000000));
62
} else if (freq >= 1000) {
63
frequs.append(stringify(freq / 1000));
66
frequs.append(stringify(freq));
72
///returns a timestamp of the format 13102011152700 converted to GMT from CET or CEST respectively. Corrects for summer/wintertime. Uses defined CET and CEST offset value.
74
std::string create_GMT(std::string date) {
75
//create locale and time_conversion object
76
std::locale loc; // "C" locale
78
std::setlocale(LC_ALL, "EN_EN");
81
// setlocale(LC_ALL, "EN_EN");
83
const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);
84
//create manipulator streams and reformat date-part
85
std::ios::iostate state;
86
std::stringstream iss1;
87
iss1 << date.substr(5, 2) << "/" << date.substr(8, 2) << "/" << date.substr(0, 4);
88
std::istringstream iss2(date.substr(11));
89
std::istringstream iss3(date.substr(0, 4));
92
//create iterators, used to read in the corresponding parts of the streams
93
std::istreambuf_iterator<char> itbegin1(iss1); // beginning of iss
94
std::istreambuf_iterator<char> itend1; // end-of-stream
95
std::istreambuf_iterator<char> itbegin2(iss2); // beginning of iss
96
std::istreambuf_iterator<char> itend2; // end-of-stream
97
std::istreambuf_iterator<char> itbegin3(iss3); // beginning of iss
98
std::istreambuf_iterator<char> itend3; // end-of-stream
100
//create time_struct and fill it with the local time and date
102
tmget.get_date(itbegin1, itend1, iss1, state, &when);
103
tmget.get_year(itbegin3, itend3, iss3, state, &when);
104
tmget.get_time(itbegin2, itend2, iss2, state, &when);
106
int sec = convertTo<int>(date.substr(17, 2));
107
int min = convertTo<int>(date.substr(14, 2));
108
int hour = convertTo<int>(date.substr(11, 2));
109
int mon = convertTo<int>(date.substr(5, 2));
110
int year = convertTo<int>(date.substr(0, 4));
111
int mday = convertTo<int>(date.substr(8, 2));
112
int wday = weekday(mon, mday, when.tm_year);
113
std::cout<<year<<mon<<mday<<hour<<min<<sec<<std::endl;
114
//determine if Summertime
115
if (mon < 3 || mon > 10) {
117
} else if (mon > 3 && mon < 10) {
120
if ((mday - wday) > 24) {
124
} else if (mon == 10)
125
if ((mday - wday) > 24) {
130
//correct for false hours/days/month
137
if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
139
} else if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10) {
141
} else if (mon == 2) {
142
if (!year % 4 && year % 100) {
147
} else if (mon == 0) {
151
std::cout << "This date sucks!" << std::endl;
154
std::cout<<year<<mon<<mday<<hour<<min<<sec<<std::endl;
156
iss1 << std::setfill('0') << std::setw(4) << year << std::setw(2) << mon << std::setw(2) << mday << std::setw(2) << hour << std::setw(2) << min << std::setw(2) << sec;
160
///determines the weekday from a given date, ranging from 0 to 6,
161
int weekday(int month, int day, int year) {
162
//determine the weekday. Didn't write it, just found it...
163
int ix = 0, tx = 0, vx = 0;
187
if (year > 1900) // 1900 was not a leap year
189
ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
191
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
193
return ((tx + 1) % 7);
201
class data_exception: public exception
203
virtual const char* what() const throw()
205
return "Data exception: Element not found";
209
#endif /* HELPERFUNCTIONS_H */