summaryrefslogtreecommitdiff
path: root/src/time.cpp
blob: 631e0f85f76ae7a3d10c3eb6d73652f7f0741f47 (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
#include <erebos/time.h>

#include <stdexcept>

using namespace erebos;

using std::runtime_error;
using std::string;

ZonedTime::ZonedTime(string str)
{
	intmax_t t;
	unsigned int h, m;
	char sign[2];
	if (sscanf(str.c_str(), "%jd %1[+-]%2u%2u", &t, sign, &h, &m) != 4)
		throw runtime_error("invalid zoned time");

	time = std::chrono::system_clock::time_point(std::chrono::seconds(t));
	zone = std::chrono::minutes((sign[0] == '-' ? -1 : 1) * (60 * h + m));
}

ZonedTime::operator string() const
{
	char buf[32];
	unsigned int az = std::chrono::abs(zone).count();
	snprintf(buf, sizeof(buf), "%jd %c%02u%02u",
			(intmax_t) std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count(),
			zone < decltype(zone)::zero() ? '-' : '+', az / 60, az % 60);
	return string(buf);
}

ZonedTime ZonedTime::now()
{
	return ZonedTime(std::chrono::system_clock::now());
}