aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Data/src/Time.cpp
blob: de54b96e749fa8ca3693270d3ba3d112b337a90d (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Time.cpp
//
// Library: Data
// Package: DataCore
// Module:  Time
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Data/Time.h"
#include "Poco/Data/DynamicDateTime.h"
#include "Poco/DateTime.h"
#include "Poco/Dynamic/Var.h"


using Poco::DateTime;
using Poco::Dynamic::Var;


namespace Poco {
namespace Data {


Time::Time()
{
	DateTime dt;
	assign(dt.hour(), dt.minute(), dt.second());
}


Time::Time(int hour, int minute, int second)
{
	assign(hour, minute, second);
}


Time::Time(const DateTime& dt)
{
	assign(dt.hour(), dt.minute(), dt.second());
}


Time::~Time()
{
}


void Time::assign(int hour, int minute, int second)
{
	if (hour < 0 || hour > 23) 
		throw InvalidArgumentException("Hour must be between 0 and 23.");

	if (minute < 0 || minute > 59) 
		throw InvalidArgumentException("Minute must be between 0 and 59.");

	if (second < 0 || second > 59) 
		throw InvalidArgumentException("Second must be between 0 and 59.");

	_hour = hour;
	_minute = minute;
	_second = second;
}


bool Time::operator < (const Time& time) const
{
	int hour = time.hour();

	if (_hour < hour) return true;
	else if (_hour > hour) return false;
	else // hours equal
	{
		int minute = time.minute();
		if (_minute < minute) return true;
		else 
		if (_minute > minute) return false;
		else // minutes equal
		if (_second < time.second()) return true;
	}

	return false;
}


Time& Time::operator = (const Var& var)
{
#ifndef __GNUC__
// g++ used to choke on this, newer versions seem to digest it fine
// TODO: determine the version able to handle it properly
	*this = var.extract<Time>();
#else
	*this = var.operator Time(); 
#endif
	return *this;
}


} } // namespace Poco::Data


#ifdef __GNUC__
// only needed for g++ (see comment in Time::operator = above)

namespace Poco {
namespace Dynamic {


using Poco::Data::Time;
using Poco::DateTime;


template <>
Var::operator Time () const
{
	VarHolder* pHolder = content();

	if (!pHolder)
		throw InvalidAccessException("Can not convert empty value.");

	if (typeid(Time) == pHolder->type())
		return extract<Time>();
	else
	{
		Poco::DateTime result;
		pHolder->convert(result);
		return Time(result);
	}
}


} } // namespace Poco::Dynamic


#endif // __GNUC__