-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas_dates.cpp
64 lines (58 loc) · 1.61 KB
/
as_dates.cpp
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
#include "as_dates.h"
#include <iomanip>
#include <sstream>
#include <stdexcept>
namespace R {
variant_vector as_dates(const variant_vector& dates, std::string format) {
variant_vector tm_dates;
for (const auto& date : dates) {
r_string d;
switch (date.index()) {
case _str:
d = std::get<_str>(date);
break;
case _int:
d = std::to_string(std::get<_int>(date));
break;
default:
throw std::invalid_argument(std::string(__func__) + " invalid argument " + index_to_string[date.index()]);
}
std::istringstream ss(d);
std:tm tm;
std::time_t t = std::time(nullptr);
tm = *std::localtime(&t); // ensure no tm elements are undefined, or mktime will fail
ss >> std::get_time(&tm, format.c_str());
r_date tm_date;
tm_date.format = format;
tm_date.tm = tm;
tm_dates.push_back(tm_date);
}
return tm_dates;
}
variant_vector as_dates(variant_vector&& dates, std::string format) {
variant_vector tm_dates;
for (const auto& date : dates) {
r_string d;
switch (date.index()) {
case _str:
d = std::get<_str>(date);
break;
case _int:
d = std::to_string(std::get<_int>(date));
break;
default:
throw std::invalid_argument(std::string(__func__) + " invalid argument " + index_to_string[date.index()]);
}
std::istringstream ss(d);
std:tm tm;
std::time_t t = std::time(nullptr);
tm = *std::localtime(&t); // ensure no tm elements are undefined, or mktime will fail
ss >> std::get_time(&tm, format.c_str());
r_date tm_date;
tm_date.format = format;
tm_date.tm = tm;
tm_dates.push_back(tm_date);
}
return tm_dates;
}
}