summaryrefslogtreecommitdiff
path: root/project2/common/tests/validDateCheck.cpp
blob: dabbdaa07f6b9ab54d148f023583fc5c7159bb07 (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
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
#include <pch.hpp>
#include "../logger.h"
#include "../scriptLoader.h"
#include "../commonObjects.h"
#include "../test.h"
#include "../variables.h"
#include "../scripts.h"

class ValidDateTest : public Test {
	public:
		ValidDateTest(ScriptNodePtr p) :
			SourceObject(p),
			Test(p),
			applyTo(p, "apply-to"),
			format(p, "format"),
			warnLev(p->value("warn", true).as<bool>() ? LOG_WARNING : LOG_INFO)
		{
		}

		~ValidDateTest()
		{
		}

		bool
		passes() const
		{
			struct tm tm, ftm;
			memset(&tm, 0, sizeof(struct tm));
			mktime(&tm);
			const char * at = applyTo();
			const char * f = format();
			const char * s = strptime(at, f, &tm);
			if (!s || *s) {
				Logger()->messagef(warnLev, "%s: check failed (parse) for '%s' against '%s'",
						__PRETTY_FUNCTION__, at, f);
				return false;
			}
			ftm = tm;
			if (mktime(&ftm) == -1) {
				Logger()->messagef(warnLev, "%s: check failed (normalise) for '%s' against '%s'",
						__PRETTY_FUNCTION__, at, f);
				return false;
			}
			if (tm.tm_year != ftm.tm_year ||
					tm.tm_mon != ftm.tm_mon ||
					tm.tm_mday != ftm.tm_mday ||
					tm.tm_hour != (ftm.tm_hour - ftm.tm_isdst) ||
					tm.tm_min != ftm.tm_min ||
					tm.tm_sec != ftm.tm_sec) {
				Logger()->messagef(LOG_INFO, "tm: %d %d %d %d %d %d",
						tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
				Logger()->messagef(LOG_INFO, "ftm: %d %d %d %d %d %d",
						ftm.tm_year, ftm.tm_mon, ftm.tm_mday, ftm.tm_hour, ftm.tm_min, ftm.tm_sec);
				Logger()->messagef(warnLev, "%s: check failed (verify) for '%s' against '%s'",
						__PRETTY_FUNCTION__, at, f);
				return false;
			}
			return true;
		}
		Variable applyTo;
		Variable format;
		int warnLev;
};

DECLARE_LOADER("validdatetest", ValidDateTest);