File: datetime.cpp

package info (click to toggle)
mysql%2B%2B 3.0.9-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,228 kB
  • ctags: 9,647
  • sloc: cpp: 33,154; sh: 3,098; perl: 778; makefile: 700
file content (323 lines) | stat: -rw-r--r-- 6,417 bytes parent folder | download | duplicates (7)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***********************************************************************
 datetime.cpp - Implements date and time classes compatible with MySQL's
	various date and time column types.

 Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
 (c) 2004-2008 by Educational Technology Resources, Inc.  Others may
 also hold copyrights on code in this file.  See the CREDITS.txt file
 in the top directory of the distribution for details.

 This file is part of MySQL++.

 MySQL++ is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.

 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with MySQL++; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#define MYSQLPP_NOT_HEADER
#include "common.h"

#include "datetime.h"
#include "stream2string.h"

#include <iomanip>

#include <stdlib.h>
#include <time.h>

using namespace std;

namespace mysqlpp {

static void
safe_localtime(struct tm* ptm, const time_t t)
{
#if defined(MYSQLPP_HAVE_LOCALTIME_S)
	// common.h detected localtime_s() from native RTL of VC++ 2005 and up
	localtime_s(ptm, &t);
#elif defined(HAVE_LOCALTIME_R)
	// autoconf detected POSIX's localtime_r() on this system
	localtime_r(&t, ptm);
#else
	// No explicitly thread-safe localtime() replacement found.  This
	// may still be thread-safe, as some C libraries take special steps
	// within localtime() to get thread safety, such as TLS.
	memcpy(ptm, localtime(&t), sizeof(tm));
#endif
}


std::ostream& operator <<(std::ostream& os, const Date& d)
{
	char fill = os.fill('0');
	ios::fmtflags flags = os.setf(ios::right);
	os		<< setw(4) << d.year() << '-'
			<< setw(2) << static_cast<int>(d.month()) << '-'
			<< setw(2) << static_cast<int>(d.day());
	os.flags(flags);
	os.fill(fill);
	return os;
}


std::ostream& operator <<(std::ostream& os, const Time& t)
{
	char fill = os.fill('0');
	ios::fmtflags flags = os.setf(ios::right);
	os		<< setw(2) << static_cast<int>(t.hour()) << ':'
			<< setw(2) << static_cast<int>(t.minute()) << ':'
			<< setw(2) << static_cast<int>(t.second());
	os.flags(flags);
	os.fill(fill);
	return os;
}


std::ostream& operator <<(std::ostream& os, const DateTime& dt)
{
	if (dt.is_now()) {
		return os << "NOW()";
	}
	else {
		operator <<(os, Date(dt));
		os << ' ';
		return operator <<(os, Time(dt));
	}
}


Date::Date(time_t t)
{
	struct tm tm;
	safe_localtime(&tm, t);

	year_ = tm.tm_year + 1900;
	month_ = tm.tm_mon + 1;
	day_ = tm.tm_mday;
}


DateTime::DateTime(time_t t)
{
	struct tm tm;
	safe_localtime(&tm, t);

	year_ = tm.tm_year + 1900;
	month_ = tm.tm_mon + 1;
	day_ = tm.tm_mday;
	hour_ = tm.tm_hour;
	minute_ = tm.tm_min;
	second_ = tm.tm_sec;

	now_ = false;
}


Time::Time(time_t t)
{
	struct tm tm;
	safe_localtime(&tm, t);

	hour_ = tm.tm_hour;
	minute_ = tm.tm_min;
	second_ = tm.tm_sec;
}


const char*
Date::convert(const char* str)
{
	char num[5];

	num[0] = *str++;
	num[1] = *str++;
	num[2] = *str++;
	num[3] = *str++;
	num[4] = 0;
	year_ = static_cast<unsigned short>(strtol(num, 0, 10));
	if (*str == '-') str++;

	num[0] = *str++;
	num[1] = *str++;
	num[2] = 0;
	month_ = static_cast<unsigned char>(strtol(num, 0, 10));
	if (*str == '-') str++;

	num[0] = *str++;
	num[1] = *str++;
	num[2] = 0;
	day_ = static_cast<unsigned char>(strtol(num, 0, 10));

	return str;
}


const char*
Time::convert(const char* str)
{
	char num[5];

	num[0] = *str++;
	num[1] = *str++;
	num[2] = 0;
	hour_ = static_cast<unsigned char>(strtol(num,0,10));
	if (*str == ':') str++;

	num[0] = *str++;
	num[1] = *str++;
	num[2] = 0;
	minute_ = static_cast<unsigned char>(strtol(num,0,10));
	if (*str == ':') str++;

	num[0] = *str++;
	num[1] = *str++;
	num[2] = 0;
	second_ = static_cast<unsigned char>(strtol(num,0,10));

	return str;
}


const char*
DateTime::convert(const char* str)
{
	Date d;
	str = d.convert(str);
	year_ = d.year();
	month_ = d.month();
	day_ = d.day();
	
	if (*str == ' ') ++str;

	Time t;
	str = t.convert(str);
	hour_ = t.hour();
	minute_ = t.minute();
	second_ = t.second();

	now_ = false;
	
	return str;
}


int
Date::compare(const Date& other) const
{
	if (year_ != other.year_) return year_ - other.year_;
	if (month_ != other.month_) return month_ - other.month_;
	return day_ - other.day_;
}


int
Time::compare(const Time& other) const
{
	if (hour_ != other.hour_) return hour_ - other.hour_;
	if (minute_ != other.minute_) return minute_ - other.minute_;
	return second_ - other.second_;
}


int
DateTime::compare(const DateTime& other) const
{
	if (now_ && other.now_) {
		return 0;
	}
	else {
		Date d(*this), od(other);
		Time t(*this), ot(other);

		if (int x = d.compare(od)) {
			return x;
		}
		else {
			return t.compare(ot);
		}
	}
}


Date::operator std::string() const
{
	return stream2string(*this);
}


DateTime::operator std::string() const
{
	return stream2string(*this);
}


Time::operator std::string() const
{
	return stream2string(*this);
}


Date::operator time_t() const
{
	struct tm tm;
	safe_localtime(&tm, time(0));

	tm.tm_mday = day_;
	tm.tm_mon = month_ - 1;
	tm.tm_year = year_ - 1900;
	tm.tm_isdst = -1;

	return mktime(&tm);
}


DateTime::operator time_t() const
{
	if (now_) {
		// Many factors combine to make it almost impossible for this
		// case to return the same value as you'd get if you used this
		// in a query.  But, you gotta better idea than to return the
		// current time for an object initialized with the value "now"?
		return time(0);
	}
	else {
		struct tm tm;
		tm.tm_sec = second_;
		tm.tm_min = minute_;
		tm.tm_hour = hour_;
		tm.tm_mday = day_;
		tm.tm_mon = month_ - 1;
		tm.tm_year = year_ - 1900;
		tm.tm_isdst = -1;

		return mktime(&tm);
	}
}


Time::operator time_t() const
{
	struct tm tm;
	safe_localtime(&tm, time(0));

	tm.tm_sec = second_;
	tm.tm_min = minute_;
	tm.tm_hour = hour_;
	tm.tm_isdst = -1;

	return mktime(&tm);
}

} // end namespace mysqlpp