open-license-manager
2014-04-14 3ba1ee60337c5e74027acb6f89a16e5ff3aa345c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * StringUtils.cpp
 *
 *  Created on: Apr 8, 2014
 *      Author: devel
 */
 
#include <cctype> //toupper
#include "StringUtils.h"
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
 
namespace license {
using namespace std;
 
string trim_copy(const string& string_to_trim) {
    std::string::const_iterator it = string_to_trim.begin();
    while (it != string_to_trim.end() && isspace(*it))
        it++;
 
    std::string::const_reverse_iterator rit = string_to_trim.rbegin();
    while (rit.base() != it && isspace(*rit))
        rit++;
 
    return std::string(it, rit.base());
}
 
string toupper_copy(const string& lowercase) {
    string cp(lowercase);
    std::transform(cp.begin(), cp.end(), cp.begin(), (int (*)(int))toupper);
    return cp;
}
 
time_t seconds_from_epoch(const char* timeString) {
    int year, month, day;
    tm tm;
    if (strlen(timeString) == 8) {
        int nfield = sscanf(timeString, "%4d%2d%2d", &year, &month, &day);
        if (nfield != 3) {
            throw invalid_argument("Date not recognized");
        }
    } else if (strlen(timeString) == 10) {
        int nfield = sscanf(timeString, "%4d-%2d-%2d", &year, &month, &day);
        if (nfield != 3) {
            int nfield = sscanf(timeString, "%4d/%2d/%2d", &year, &month, &day);
            if (nfield != 3) {
                throw invalid_argument("Date not recognized");
            }
        }
    } else{
        throw invalid_argument("Date not recognized");
    }
    tm.tm_isdst = -1;
    tm.tm_year = year - 1900;
    tm.tm_mon = month - 1;
    tm.tm_mday = day;
    tm.tm_hour = 0;
    tm.tm_min = 0;
    tm.tm_sec = 0;
    tm.tm_yday = -1;
    tm.tm_wday = -1;
    return mktime(&tm);
}
} /* namespace license */