blob: 7f291da873ef81a304525485b492c8aaf3244a4c (
plain)
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
|
#include "conversions.h"
#include <boost/numeric/conversion/cast.hpp>
#include <stdexcept>
#define SHORT(x) boost::numeric_cast< ::Ice::Short >(x)
namespace Slicer {
std::string
dateToString(const IceUtil::Optional<::TMDb::Date> & in)
{
if (!in) {
return std::string();
}
struct tm tm;
memset(&tm, 0, sizeof(struct tm));
tm.tm_mday = in->Day;
tm.tm_mon = in->Month;
tm.tm_year = in->Year;
mktime(&tm);
char buf[BUFSIZ];
auto len = strftime(buf, BUFSIZ, "%Y-%m-%d", &tm);
return std::string(buf, len);
}
IceUtil::Optional<::TMDb::Date>
stringToDate(const std::string & in)
{
if (in.empty()) {
return IceUtil::None;
}
struct tm tm;
memset(&tm, 0, sizeof(struct tm));
auto end = strptime(in.c_str(), "%Y-%m-%d", &tm);
mktime(&tm);
if (!end || *end) {
throw std::runtime_error("Invalid date string: " + in);
}
return ::TMDb::Date { SHORT(1900 + tm.tm_year), SHORT(1 + tm.tm_mon), SHORT(tm.tm_mday) };
}
}
|