~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 01:02:23 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321010223-j8cph7eeyt1u3xol
Fixed function object to ensure it correctly returns a boolean type since
memcmp returns an integer. Added some more comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  Authors:
 
7
 *
 
8
 *  Jay Pipes <jay.pipes@sun.com>
 
9
 *
 
10
 *  This program is free software; you can redistribute it and/or modify
 
11
 *  it under the terms of the GNU General Public License as published by
 
12
 *  the Free Software Foundation; either version 2 of the License, or
 
13
 *  (at your option) any later version.
 
14
 *
 
15
 *  This program is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *  GNU General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU General Public License
 
21
 *  along with this program; if not, write to the Free Software
 
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
23
 */
 
24
 
 
25
/**
 
26
 * @file 
 
27
 *
 
28
 * Implementation of the server's temporal class API
 
29
 *
 
30
 * @todo
 
31
 *
 
32
 * Move to completed ValueObject API, which would remove the from_xxx() methods
 
33
 * and replace them with constructors which take other ValueObject instances as
 
34
 * their single parameter.
 
35
 */
 
36
 
 
37
#include "drizzled/global.h"
 
38
 
 
39
#include "mystrings/m_ctype.h"
 
40
#include "drizzled/my_decimal.h"
 
41
#include "drizzled/calendar.h"
 
42
#include "drizzled/temporal.h"
 
43
#ifdef NOTYETIMPLEMENTED
 
44
#include "drizzled/temporal_interval.h"
 
45
#endif
 
46
#include "drizzled/temporal_format.h"
 
47
 
 
48
#include <ostream>
 
49
#include <iomanip>
 
50
#include <vector>
 
51
#include <string.h>
 
52
 
 
53
/* time.h may already have been included in global.h, but we
 
54
   need to pick up the extra defs as well, after the global time.h */
 
55
#ifndef HAVE_DECL_TIMEGM
 
56
# include <gnulib/time.h>
 
57
#endif
 
58
 
 
59
extern std::vector<drizzled::TemporalFormat *> known_datetime_formats;
 
60
extern std::vector<drizzled::TemporalFormat *> known_date_formats;
 
61
extern std::vector<drizzled::TemporalFormat *> known_time_formats;
 
62
 
 
63
namespace drizzled 
 
64
{
 
65
 
 
66
Temporal::Temporal()
 
67
:
 
68
  _calendar(GREGORIAN)
 
69
, _years(0)
 
70
, _months(0)
 
71
, _days(0)
 
72
, _hours(0)
 
73
, _minutes(0)
 
74
, _seconds(0)
 
75
, _epoch_seconds(0)
 
76
, _useconds(0)
 
77
, _nseconds(0)
 
78
, _overflow(false)
 
79
{}
 
80
 
 
81
uint64_t Temporal::_cumulative_seconds_in_time() const
 
82
{
 
83
  return (uint64_t) ((_hours * INT64_C(3600)) 
 
84
      + (_minutes * INT64_C(60)) 
 
85
      + _seconds);
 
86
}
 
87
 
 
88
void Temporal::set_epoch_seconds()
 
89
{
 
90
  /* 
 
91
   * If the temporal is in the range of a timestamp, set 
 
92
   * the epoch_seconds member variable
 
93
   */
 
94
  if (in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds))
 
95
  {
 
96
    time_t result_time;
 
97
    struct tm broken_time;
 
98
 
 
99
    broken_time.tm_sec= _seconds;
 
100
    broken_time.tm_min= _minutes;
 
101
    broken_time.tm_hour= _hours;
 
102
    broken_time.tm_mday= _days; /* Drizzle format uses ordinal, standard tm does too! */
 
103
    broken_time.tm_mon= _months - 1; /* Drizzle format uses ordinal, standard tm does NOT! */
 
104
    broken_time.tm_year= _years - 1900; /* tm_year expects range of 70 - 38 */
 
105
 
 
106
    result_time= timegm(&broken_time);
 
107
 
 
108
    _epoch_seconds= result_time;
 
109
  }
 
110
}
 
111
 
 
112
bool Date::from_string(const char *from, size_t from_len)
 
113
{
 
114
  /* 
 
115
   * Loop through the known date formats and see if 
 
116
   * there is a match.
 
117
   */
 
118
  bool matched= false;
 
119
  TemporalFormat *current_format;
 
120
  std::vector<TemporalFormat *>::iterator current= known_date_formats.begin();
 
121
 
 
122
  while (current != known_date_formats.end())
 
123
  {
 
124
    current_format= *current;
 
125
    if (current_format->matches(from, from_len, this))
 
126
    {
 
127
      matched= true;
 
128
      break;
 
129
    }
 
130
    current++;
 
131
  }
 
132
 
 
133
  if (! matched)
 
134
    return false;
 
135
 
 
136
  set_epoch_seconds();
 
137
  return is_valid();
 
138
}
 
139
 
 
140
bool DateTime::from_string(const char *from, size_t from_len)
 
141
{
 
142
  /* 
 
143
   * Loop through the known datetime formats and see if 
 
144
   * there is a match.
 
145
   */
 
146
  bool matched= false;
 
147
  TemporalFormat *current_format;
 
148
  std::vector<TemporalFormat *>::iterator current= known_datetime_formats.begin();
 
149
 
 
150
  while (current != known_datetime_formats.end())
 
151
  {
 
152
    current_format= *current;
 
153
    if (current_format->matches(from, from_len, this))
 
154
    {
 
155
      matched= true;
 
156
      break;
 
157
    }
 
158
    current++;
 
159
  }
 
160
 
 
161
  if (! matched)
 
162
    return false;
 
163
 
 
164
  set_epoch_seconds();
 
165
  return is_valid();
 
166
}
 
167
 
 
168
/*
 
169
 * Comparison operators for Time against another Time
 
170
 * are easy.  We simply compare the cumulative time
 
171
 * value of each.
 
172
 */
 
173
bool Time::operator==(const Time& rhs)
 
174
{
 
175
  return (
 
176
          _hours == rhs._hours
 
177
       && _minutes == rhs._minutes
 
178
       && _seconds == rhs._seconds
 
179
       && _useconds == rhs._useconds
 
180
       && _nseconds == rhs._nseconds
 
181
      );
 
182
}
 
183
bool Time::operator!=(const Time& rhs)
 
184
{
 
185
  return ! (*this == rhs);
 
186
}
 
187
bool Time::operator<(const Time& rhs)
 
188
{
 
189
  return (_cumulative_seconds_in_time() < rhs._cumulative_seconds_in_time());
 
190
}
 
191
bool Time::operator<=(const Time& rhs)
 
192
{
 
193
  return (_cumulative_seconds_in_time() <= rhs._cumulative_seconds_in_time());
 
194
}
 
195
bool Time::operator>(const Time& rhs)
 
196
{
 
197
  return (_cumulative_seconds_in_time() > rhs._cumulative_seconds_in_time());
 
198
}
 
199
bool Time::operator>=(const Time& rhs)
 
200
{
 
201
  return (_cumulative_seconds_in_time() >= rhs._cumulative_seconds_in_time());
 
202
}
 
203
 
 
204
/** 
 
205
 * Subtracting one Time value from another can yield
 
206
 * a new Time instance.
 
207
 *
 
208
 * This operator is called in the following situation:
 
209
 *
 
210
 * @code
 
211
 * drizzled::Time lhs;
 
212
 * lhs.from_string("20:00:00");
 
213
 * drizzled::Time rhs;
 
214
 * rhs.from_string("19:00:00");
 
215
 *
 
216
 * drizzled::Time result= lhs - rhs;
 
217
 * @endcode
 
218
 *
 
219
 * @note
 
220
 *
 
221
 * Subtracting a larger time value from a smaller one
 
222
 * should throw an exception at some point.  The result
 
223
 * of such an operator should be a TemporalInterval, not
 
224
 * a Time instance, since a negative time is not possible.
 
225
 */
 
226
const Time Time::operator-(const Time& rhs)
 
227
{
 
228
  Time result;
 
229
 
 
230
  int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
 
231
  result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
232
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
233
  result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
234
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
235
  result._seconds= (uint32_t) second_diff;
 
236
  
 
237
  return result;
 
238
}
 
239
const Time Time::operator+(const Time& rhs)
 
240
{
 
241
  Time result;
 
242
  int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
 
243
  result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
244
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
245
  result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
246
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
247
  result._seconds= (uint32_t) second_diff;
 
248
  /** 
 
249
   * @TODO Once exceptions are supported, we should raise an error here if
 
250
   *       the result Time is not valid?
 
251
   */
 
252
  return result;
 
253
}
 
254
 
 
255
/*
 
256
 * Variation of + and - operator which returns a reference to the left-hand
 
257
 * side Time object and adds the right-hand side to itself.
 
258
 */
 
259
Time& Time::operator+=(const Time& rhs)
 
260
{
 
261
  int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
 
262
  _hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
263
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
264
  _minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
265
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
266
  _seconds= (uint32_t) second_diff;
 
267
  /** 
 
268
   * @TODO Once exceptions are supported, we should raise an error here if
 
269
   *       the result Time is not valid?
 
270
   */
 
271
  return *this;
 
272
}
 
273
Time& Time::operator-=(const Time& rhs)
 
274
{
 
275
  int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
 
276
  _hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
277
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
278
  _minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
279
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
280
  _seconds= (uint32_t) second_diff;
 
281
  /** 
 
282
   * @TODO Once exceptions are supported, we should raise an error here if
 
283
   *       the result Time is not valid?
 
284
   */
 
285
  return *this;
 
286
}
 
287
 
 
288
/*
 
289
 * Comparison operators for Date against another Date
 
290
 * are easy.  We simply compare the cumulative
 
291
 * value of each.
 
292
 */
 
293
bool Date::operator==(const Date& rhs)
 
294
{
 
295
  return (
 
296
          _years == rhs._years
 
297
       && _months == rhs._months
 
298
       && _days == rhs._days
 
299
      );
 
300
}
 
301
bool Date::operator!=(const Date& rhs)
 
302
{
 
303
  return ! (*this == rhs);
 
304
}
 
305
bool Date::operator<(const Date& rhs)
 
306
{
 
307
  int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
308
  int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
309
  return (days_left < days_right);
 
310
}
 
311
bool Date::operator<=(const Date& rhs)
 
312
{
 
313
  int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
314
  int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
315
  return (days_left <= days_right);
 
316
}
 
317
bool Date::operator>(const Date& rhs)
 
318
{
 
319
  return ! (*this <= rhs);
 
320
}
 
321
bool Date::operator>=(const Date& rhs)
 
322
{
 
323
  return ! (*this < rhs);
 
324
}
 
325
 
 
326
/*
 
327
 * Comparison operators for DateTime against another DateTime
 
328
 * are easy.  We simply compare the cumulative time
 
329
 * value of each.
 
330
 */
 
331
bool Date::operator==(const DateTime& rhs)
 
332
{
 
333
  return (
 
334
          _years == rhs._years
 
335
       && _months == rhs._months
 
336
       && _days == rhs._days
 
337
       && _hours == rhs._hours
 
338
       && _minutes == rhs._minutes
 
339
       && _seconds == rhs._seconds
 
340
       && _useconds == rhs._useconds
 
341
       && _nseconds == rhs._nseconds
 
342
      );
 
343
}
 
344
bool Date::operator!=(const DateTime& rhs)
 
345
{
 
346
  return ! (*this == rhs);
 
347
}
 
348
bool Date::operator<(const DateTime& rhs)
 
349
{
 
350
  int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
351
  int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
352
  if (days_left < days_right)
 
353
    return true;
 
354
  else if (days_left > days_right)
 
355
    return false;
 
356
  /* Here if both dates are the same, so compare times */
 
357
  return (_cumulative_seconds_in_time() < rhs._cumulative_seconds_in_time());
 
358
}
 
359
bool Date::operator<=(const DateTime& rhs)
 
360
{
 
361
  int64_t days_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
362
  int64_t days_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
363
  if (days_left < days_right)
 
364
    return true;
 
365
  else if (days_left > days_right)
 
366
    return false;
 
367
  /* Here if both dates are the same, so compare times */
 
368
  return (_cumulative_seconds_in_time() <= rhs._cumulative_seconds_in_time());
 
369
}
 
370
bool Date::operator>(const DateTime& rhs)
 
371
{
 
372
  return ! (*this <= rhs);
 
373
}
 
374
bool Date::operator>=(const DateTime& rhs)
 
375
{
 
376
  return ! (*this < rhs);
 
377
}
 
378
 
 
379
/** 
 
380
 * We can add or subtract a Time value to/from a DateTime value 
 
381
 * as well...it always produces a DateTime.
 
382
 */
 
383
const Date Date::operator-(const Time& rhs)
 
384
{
 
385
  DateTime result;
 
386
 
 
387
  /* 
 
388
   * First, we set the resulting DATE pieces equal to our 
 
389
   * left-hand side DateTime's DATE components. Then, deal with 
 
390
   * the time components.
 
391
   */
 
392
  result._years= _years;
 
393
  result._months= _months;
 
394
  result._days= _days;
 
395
 
 
396
  int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
 
397
 
 
398
  /* 
 
399
   * The resulting diff might be negative.  If it is, that means that 
 
400
   * we have subtracting a larger time piece from the datetime, like so:
 
401
   *
 
402
   * x = DateTime("2007-06-09 09:30:00") - Time("16:30:00");
 
403
   *
 
404
   * In these cases, we need to subtract a day from the resulting
 
405
   * DateTime.
 
406
   */
 
407
  if (second_diff < 0)
 
408
    result._days--;
 
409
 
 
410
  result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
411
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
412
  result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
413
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
414
  result._seconds= (uint32_t) second_diff;
 
415
 
 
416
  /* Handle the microsecond precision */
 
417
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
418
  if (microsecond_diff < 0)
 
419
  {
 
420
    microsecond_diff= (-1 * microsecond_diff);
 
421
    result._seconds--;
 
422
  }
 
423
  result._useconds= (uint32_t) microsecond_diff;
 
424
 
 
425
  return result;
 
426
}
 
427
const Date Date::operator+(const Time& rhs)
 
428
{
 
429
  DateTime result;
 
430
 
 
431
  /* 
 
432
   * First, we set the resulting DATE pieces equal to our 
 
433
   * left-hand side DateTime's DATE components. Then, deal with 
 
434
   * the time components.
 
435
   */
 
436
  result._years= _years;
 
437
  result._months= _months;
 
438
  result._days= _days;
 
439
 
 
440
  int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
 
441
 
 
442
  /* 
 
443
   * The resulting seconds might be more than a day.  If do, 
 
444
   * adjust our resulting days up 1.
 
445
   */
 
446
  if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
 
447
  {
 
448
    result._days++;
 
449
    second_diff%= DRIZZLE_SECONDS_IN_DAY;
 
450
  }
 
451
 
 
452
  result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
453
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
454
  result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
455
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
456
  result._seconds= (uint32_t) second_diff;
 
457
 
 
458
  /* Handle the microsecond precision */
 
459
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
460
  if (microsecond_diff < 0)
 
461
  {
 
462
    microsecond_diff= (-1 * microsecond_diff);
 
463
    result._seconds--;
 
464
  }
 
465
  result._useconds= (uint32_t) microsecond_diff;
 
466
 
 
467
  return result;
 
468
}
 
469
 
 
470
/*
 
471
 * Variation of + and - operator which returns a reference to the left-hand
 
472
 * side DateTime object and adds the right-hand side Time to itself.
 
473
 */
 
474
Date& Date::operator+=(const Time& rhs)
 
475
{
 
476
  int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
 
477
  /* 
 
478
   * The resulting seconds might be more than a day.  If do, 
 
479
   * adjust our resulting days up 1.
 
480
   */
 
481
  if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
 
482
  {
 
483
    _days++;
 
484
    second_diff%= DRIZZLE_SECONDS_IN_DAY;
 
485
  }
 
486
 
 
487
  _hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
488
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
489
  _minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
490
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
491
  _seconds= (uint32_t) second_diff;
 
492
 
 
493
  /* Handle the microsecond precision */
 
494
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
495
  if (microsecond_diff < 0)
 
496
  {
 
497
    microsecond_diff= (-1 * microsecond_diff);
 
498
    _seconds--;
 
499
  }
 
500
  _useconds= (uint32_t) microsecond_diff;
 
501
  /** 
 
502
   * @TODO Once exceptions are supported, we should raise an error here if
 
503
   *       the result Time is not valid?
 
504
   */
 
505
  return *this;
 
506
}
 
507
Date& Date::operator-=(const Time& rhs)
 
508
{
 
509
  int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
 
510
 
 
511
  /* 
 
512
   * The resulting diff might be negative.  If it is, that means that 
 
513
   * we have subtracting a larger time piece from the datetime, like so:
 
514
   *
 
515
   * x = DateTime("2007-06-09 09:30:00");
 
516
   * x-= Time("16:30:00");
 
517
   *
 
518
   * In these cases, we need to subtract a day from the resulting
 
519
   * DateTime.
 
520
   */
 
521
  if (second_diff < 0)
 
522
    _days--;
 
523
 
 
524
  _hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
525
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
526
  _minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
527
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
528
  _seconds= (uint32_t) second_diff;
 
529
 
 
530
  /* Handle the microsecond precision */
 
531
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
532
  if (microsecond_diff < 0)
 
533
  {
 
534
    microsecond_diff= (-1 * microsecond_diff);
 
535
    _seconds--;
 
536
  }
 
537
  _useconds= (uint32_t) microsecond_diff;
 
538
  /** 
 
539
   * @TODO Once exceptions are supported, we should raise an error here if
 
540
   *       the result Time is not valid?
 
541
   */
 
542
  return *this;
 
543
}
 
544
 
 
545
/**
 
546
 * We can add/subtract two Dates to/from each other.  The result
 
547
 * is always another Date instance.
 
548
 */
 
549
const Date Date::operator-(const Date &rhs)
 
550
{
 
551
  /* Figure out the difference in days between the two dates */
 
552
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
553
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
554
  int64_t day_diff= day_left - day_right;
 
555
 
 
556
  Date result;
 
557
  /* Now re-compose the Date's structure from the resulting Julian Day Number */
 
558
  gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
 
559
  return result;
 
560
}
 
561
const Date Date::operator+(const Date &rhs)
 
562
{
 
563
  /* 
 
564
   * Figure out the new Julian Day Number by adding the JDNs of both
 
565
   * dates together.
 
566
   */
 
567
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
568
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
569
  int64_t day_diff= day_left + day_right;
 
570
 
 
571
  /** @TODO Need an exception check here for bounds of JDN... */
 
572
 
 
573
  Date result;
 
574
  /* Now re-compose the Date's structure from the resulting Julian Day Number */
 
575
  gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
 
576
  return result;
 
577
}
 
578
/* Similar to the above, but we add/subtract the right side to this object itself */
 
579
Date& Date::operator-=(const Date &rhs)
 
580
{
 
581
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
582
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
583
  int64_t day_diff= day_left - day_right;
 
584
 
 
585
  /* Now re-compose the Date's structure from the resulting Julian Day Number */
 
586
  gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
 
587
  return *this;
 
588
}
 
589
Date& Date::operator+=(const Date &rhs)
 
590
{
 
591
  /* 
 
592
   * Figure out the new Julian Day Number by adding the JDNs of both
 
593
   * dates together.
 
594
   */
 
595
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
596
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
597
  int64_t day_diff= day_left + day_right;
 
598
 
 
599
  /** @TODO Need an exception check here for bounds of JDN... */
 
600
 
 
601
  /* Now re-compose the Date's structure from the resulting Julian Day Number */
 
602
  gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
 
603
  return *this;
 
604
}
 
605
 
 
606
Date& Date::operator=(const DateTime &rhs)
 
607
{
 
608
  /* Only copy the Date components of the assigned DateTime... */
 
609
  _years= rhs._years;
 
610
  _months= rhs._months;
 
611
  _days= rhs._days;
 
612
  /* Zero-out everything else.. */
 
613
  _hours= _minutes= _seconds= _useconds= _nseconds= 0;
 
614
  return *this;
 
615
}
 
616
 
 
617
/**
 
618
 * We can add/subtract two DateTimes to/from each other.  The result
 
619
 * is always another DateTime instance.
 
620
 */
 
621
const Date Date::operator-(const DateTime &rhs)
 
622
{
 
623
  /* Figure out the difference in days between the two dates. */
 
624
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
625
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
626
  int64_t day_diff= day_left - day_right;
 
627
 
 
628
  DateTime result;
 
629
  /* Now re-compose the Date's structure from the resulting Julian Day Number */
 
630
  gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
 
631
 
 
632
  /* And now handle the time components */
 
633
  int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
 
634
 
 
635
  /* 
 
636
   * The resulting diff might be negative.  If it is, that means that 
 
637
   * we have subtracting a larger time piece from the datetime, like so:
 
638
   *
 
639
   * x = DateTime("2007-06-09 09:30:00");
 
640
   * x-= Time("16:30:00");
 
641
   *
 
642
   * In these cases, we need to subtract a day from the resulting
 
643
   * DateTime.
 
644
   */
 
645
  if (second_diff < 0)
 
646
    _days--;
 
647
 
 
648
  result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
649
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
650
  result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
651
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
652
  result._seconds= (uint32_t) second_diff;
 
653
 
 
654
  /* Handle the microsecond precision */
 
655
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
656
  if (microsecond_diff < 0)
 
657
  {
 
658
    microsecond_diff= (-1 * microsecond_diff);
 
659
    result._seconds--;
 
660
  }
 
661
  result._useconds= (uint32_t) microsecond_diff;
 
662
 
 
663
  return result;
 
664
}
 
665
const Date Date::operator+(const DateTime &rhs)
 
666
{
 
667
  /*
 
668
   * Figure out the new Julian Day Number by adding the JDNs of both
 
669
   * dates together.
 
670
   */
 
671
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
672
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
673
  int64_t day_diff= day_left + day_right;
 
674
 
 
675
  /** @TODO Need an exception check here for bounds of JDN... */
 
676
 
 
677
  DateTime result;
 
678
  /* Now re-compose the Date's structure from the resulting Julian Day Number */
 
679
  gregorian_date_from_julian_day_number(day_diff, &result._years, &result._months, &result._days);
 
680
 
 
681
  /* And now handle the time components */
 
682
  int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
 
683
 
 
684
  /* 
 
685
   * The resulting seconds might be more than a day.  If do, 
 
686
   * adjust our resulting days up 1.
 
687
   */
 
688
  if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
 
689
  {
 
690
    result._days++;
 
691
    second_diff%= DRIZZLE_SECONDS_IN_DAY;
 
692
  }
 
693
 
 
694
  result._hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
695
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
696
  result._minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
697
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
698
  result._seconds= (uint32_t) second_diff;
 
699
 
 
700
  /* Handle the microsecond precision */
 
701
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
702
  if (microsecond_diff < 0)
 
703
  {
 
704
    microsecond_diff= (-1 * microsecond_diff);
 
705
    result._seconds--;
 
706
  }
 
707
  result._useconds= (uint32_t) microsecond_diff;
 
708
 
 
709
  return result;
 
710
}
 
711
/* Similar to the above, but we add/subtract the right side to this object itself */
 
712
Date& Date::operator-=(const DateTime &rhs)
 
713
{
 
714
  /* Figure out the difference in days between the two dates.  */
 
715
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
716
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
717
  int64_t day_diff= day_left - day_right;
 
718
 
 
719
  /* Now re-compose the Date's structure from the ng Julian Day Number */
 
720
  gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
 
721
 
 
722
  /* And now handle the time components */
 
723
  int64_t second_diff= _cumulative_seconds_in_time() - rhs._cumulative_seconds_in_time();
 
724
 
 
725
  /* 
 
726
   * The resulting diff might be negative.  If it is, that means that 
 
727
   * we have subtracting a larger time piece from the datetime, like so:
 
728
   *
 
729
   * x = DateTime("2007-06-09 09:30:00");
 
730
   * x-= Time("16:30:00");
 
731
   *
 
732
   * In these cases, we need to subtract a day from the ng
 
733
   * DateTime.
 
734
   */
 
735
  if (second_diff < 0)
 
736
    _days--;
 
737
 
 
738
  _hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
739
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
740
  _minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
741
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
742
  _seconds= (uint32_t) second_diff;
 
743
 
 
744
  /* Handle the microsecond precision */
 
745
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
746
  if (microsecond_diff < 0)
 
747
  {
 
748
    microsecond_diff= (-1 * microsecond_diff);
 
749
    _seconds--;
 
750
  }
 
751
  _useconds= (uint32_t) microsecond_diff;
 
752
 
 
753
  return *this;
 
754
}
 
755
Date& Date::operator+=(const DateTime &rhs)
 
756
{
 
757
  /* 
 
758
   * Figure out the new Julian Day Number by adding the JDNs of both
 
759
   * dates together.
 
760
   */
 
761
  int64_t day_left= julian_day_number_from_gregorian_date(_years, _months, _days);
 
762
  int64_t day_right= julian_day_number_from_gregorian_date(rhs._years, rhs._months, rhs._days);
 
763
  int64_t day_diff= day_left + day_right;
 
764
 
 
765
  /** @TODO Need an exception check here for bounds of JDN... */
 
766
 
 
767
  /* Now re-compose the Date's structure from the ng Julian Day Number */
 
768
  gregorian_date_from_julian_day_number(day_diff, &_years, &_months, &_days);
 
769
 
 
770
  /* And now handle the time components */
 
771
  int64_t second_diff= _cumulative_seconds_in_time() + rhs._cumulative_seconds_in_time();
 
772
 
 
773
  /* 
 
774
   * The resulting seconds might be more than a day.  If do, 
 
775
   * adjust our ng days up 1.
 
776
   */
 
777
  if (second_diff >= DRIZZLE_SECONDS_IN_DAY)
 
778
  {
 
779
    _days++;
 
780
    second_diff%= DRIZZLE_SECONDS_IN_DAY;
 
781
  }
 
782
 
 
783
  _hours= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_HOUR;
 
784
  second_diff%= DRIZZLE_SECONDS_IN_HOUR;
 
785
  _minutes= (uint32_t) second_diff / DRIZZLE_SECONDS_IN_MINUTE;
 
786
  second_diff%= DRIZZLE_SECONDS_IN_MINUTE;
 
787
  _seconds= (uint32_t) second_diff;
 
788
 
 
789
  /* Handle the microsecond precision */
 
790
  int64_t microsecond_diff= _useconds - rhs._useconds;
 
791
  if (microsecond_diff < 0)
 
792
  {
 
793
    microsecond_diff= (-1 * microsecond_diff);
 
794
    _seconds--;
 
795
  }
 
796
  _useconds= (uint32_t) microsecond_diff;
 
797
 
 
798
  return *this;
 
799
}
 
800
#ifdef NOTYETIMPLEMENTED
 
801
Date& Date::operator+=(const TemporalIntervalYear &rhs)
 
802
{
 
803
  /* Simple one...add the years and adjust for any leaps */
 
804
  int64_t new_years= _years;
 
805
  new_years+= rhs._years;
 
806
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
 
807
  {
 
808
    /* 
 
809
     * Set everything to zero. We got an overflow.
 
810
     * @TODO Exceptions would be great here...
 
811
     */
 
812
    _reset();
 
813
    _overflow= true;
 
814
    return *this;
 
815
  }
 
816
  _years= (uint32_t) new_years;
 
817
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
 
818
    _days= 28;
 
819
  return *this;
 
820
 
821
 
 
822
Date& Date::operator-=(const TemporalIntervalYear &rhs)
 
823
{
 
824
  /* Simple one...subtract the years and adjust for any leaps */
 
825
  int64_t new_years= _years;
 
826
  new_years-= rhs._years;
 
827
  if (new_years < 0)
 
828
  {
 
829
    /* 
 
830
     * Set everything to zero. We got an overflow.
 
831
     * @TODO Exceptions would be great here...
 
832
     */
 
833
    _reset();
 
834
    _overflow= true;
 
835
    return *this;
 
836
  }
 
837
  _years= (uint32_t) new_years;
 
838
  if (_months == 2 && _days == 29 && days_in_gregorian_year_month(_years, _months) != 366)
 
839
    _days= 28;
 
840
  return *this;
 
841
 
842
 
 
843
Date& Date::operator+=(const TemporalIntervalDayOrWeek &rhs)
 
844
{
 
845
  /* Simple one...add the days */
 
846
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) + rhs._days;
 
847
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
 
848
  return *this;
 
849
 
850
 
 
851
Date& Date::operator-=(const TemporalIntervalDayOrWeek &rhs)
 
852
{
 
853
  /* Simple one...subtract the days */
 
854
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, _days) - rhs._days;
 
855
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
 
856
  return *this;
 
857
 
858
 
 
859
Date& Date::operator+=(const TemporalIntervalYearMonth &rhs)
 
860
{
 
861
  /* Simple one...add the months in the period adjust */
 
862
  int64_t period= (_years * 12) + (rhs._years * 12) + (_months - 1) + rhs._months;
 
863
  int64_t new_years= (period / 12);
 
864
  if (new_years > DRIZZLE_MAX_YEARS_SQL)
 
865
  {
 
866
    /* 
 
867
     * Set everything to zero. We got an overflow.
 
868
     * @TODO Exceptions would be great here...
 
869
     */
 
870
    _reset();
 
871
    _overflow= true;
 
872
    return *this;
 
873
  }
 
874
  _years= (uint32_t) new_years;
 
875
  _months= (uint32_t) (period % 12) + 1;
 
876
  
 
877
  /* Adjust day if the new month doesn't have enough days */
 
878
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
 
879
  if (_days > days_in_new_month)
 
880
    _days= days_in_new_month;
 
881
  return *this;
 
882
 
883
 
 
884
Date& Date::operator-=(const TemporalIntervalYearMonth &rhs)
 
885
{
 
886
  /* Simple one...subtract the months in the period and adjust */
 
887
  int64_t period= (_years * 12) - (rhs._years * 12) + (_months - 1) - rhs._months;
 
888
  int64_t new_years= (period / 12);
 
889
  if (new_years < 0)
 
890
  {
 
891
    /* 
 
892
     * Set everything to zero. We got an overflow.
 
893
     * @TODO Exceptions would be great here...
 
894
     */
 
895
    _reset();
 
896
    _overflow= true;
 
897
    return *this;
 
898
  }
 
899
  _years= (uint32_t) (period / 12);
 
900
  _months= (uint32_t) (period % 12) + 1;
 
901
  
 
902
  /* Adjust day if the new month doesn't have enough days */
 
903
  uint32_t days_in_new_month= days_in_gregorian_year_month(_years, _months);
 
904
  if (_days > days_in_new_month)
 
905
    _days= days_in_new_month;
 
906
  return *this;
 
907
 
908
 
 
909
Date& Date::operator+=(const TemporalIntervalDayOrLess &rhs)
 
910
{
 
911
  /* 
 
912
   * Convert the temporal and the interval into a number of 
 
913
   * microseconds, then add them together and convert the
 
914
   * resulting microseconds back into a broken-down temporal
 
915
   * component.
 
916
   */
 
917
  int64_t new_seconds;
 
918
  int64_t new_microseconds;
 
919
  int64_t extra_sec;
 
920
  int64_t new_days;
 
921
  new_microseconds= _useconds + rhs._useconds;
 
922
  extra_sec= new_microseconds / INT64_C(1000000);
 
923
  new_microseconds= new_microseconds % INT64_C(1000000);
 
924
 
 
925
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
 
926
  new_seconds+= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
 
927
  new_seconds+= extra_sec;
 
928
 
 
929
  if (new_microseconds < 0)
 
930
  {
 
931
    new_microseconds+= INT64_C(1000000);
 
932
    new_seconds--;
 
933
  }
 
934
  
 
935
  new_days= new_seconds / (3600 * 24L);
 
936
  new_seconds-= new_days * 3600 * 24L;
 
937
  if (new_seconds < 0)
 
938
  {
 
939
    new_days--;
 
940
    new_seconds+= 3600 * 24L;
 
941
  }
 
942
  _useconds= (uint32_t) new_microseconds;
 
943
  _seconds= (uint32_t) (new_seconds % 60);
 
944
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
 
945
  _hours= (uint32_t) (new_seconds / 3600);
 
946
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
 
947
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
 
948
  return *this;
 
949
}
 
950
 
 
951
Date& Date::operator-=(const TemporalIntervalDayOrLess &rhs)
 
952
{
 
953
  /* 
 
954
   * Convert the temporal and the interval into a number of 
 
955
   * microseconds, then subtract them from each other and convert 
 
956
   * the resulting microseconds back into a broken-down temporal
 
957
   * component.
 
958
   */
 
959
  int64_t new_seconds;
 
960
  int64_t new_microseconds;
 
961
  int64_t extra_sec;
 
962
  int64_t new_days;
 
963
  new_microseconds= _useconds - rhs._useconds;
 
964
  extra_sec= new_microseconds / INT64_C(1000000);
 
965
  new_microseconds= new_microseconds % INT64_C(1000000);
 
966
 
 
967
  new_seconds= ((_days - 1) * 3600 * 24) + (_hours * 3600) + (_minutes * 60) + _seconds;
 
968
  new_seconds-= (rhs._days * 3600 * 24) + (rhs._hours * 3600) + (rhs._minutes * 60) + rhs._seconds;
 
969
  new_seconds+= extra_sec;
 
970
 
 
971
  if (new_microseconds < 0)
 
972
  {
 
973
    new_microseconds+= INT64_C(1000000);
 
974
    new_seconds--;
 
975
  }
 
976
  
 
977
  new_days= new_seconds / (3600 * 24L);
 
978
  new_seconds-= new_days * 3600 * 24L;
 
979
  if (new_seconds < 0)
 
980
  {
 
981
    new_days--;
 
982
    new_seconds+= 3600 * 24L;
 
983
  }
 
984
  _useconds= (uint32_t) new_microseconds;
 
985
  _seconds= (uint32_t) (new_seconds % 60);
 
986
  _minutes= (uint32_t) ((new_seconds / 60) % 60);
 
987
  _hours= (uint32_t) (new_seconds / 3600);
 
988
  int64_t julian_day= julian_day_number_from_gregorian_date(_years, _months, 1) + new_days;
 
989
  gregorian_date_from_julian_day_number(julian_day, &_years, &_months, &_days);
 
990
  return *this;
 
991
}
 
992
#endif /* NOTYETIMPLEMENTED */
 
993
/*
 
994
 * Comparison operators between a Date and a Timestamp
 
995
 */
 
996
bool Date::operator==(const Timestamp& rhs)
 
997
{
 
998
  return (_years == rhs._years && _months == rhs._months && _days == rhs._days);
 
999
}
 
1000
bool Date::operator!=(const Timestamp& rhs)
 
1001
{
 
1002
  return ! (*this == rhs);
 
1003
}
 
1004
bool Date::operator<(const Timestamp& rhs)
 
1005
{
 
1006
  if (_years < rhs._years)
 
1007
    return true;
 
1008
  if (_years > rhs._years)
 
1009
    return false;
 
1010
  /* In same year */
 
1011
  if (_months < rhs._months)
 
1012
    return true;
 
1013
  if (_months > rhs._months)
 
1014
    return false;
 
1015
  /* Same month */
 
1016
  return _days < rhs._days;
 
1017
}
 
1018
bool Date::operator<=(const Timestamp& rhs)
 
1019
{
 
1020
  return (*this < rhs || *this == rhs);
 
1021
}
 
1022
bool Date::operator>(const Timestamp& rhs)
 
1023
{
 
1024
  return ! (*this < rhs);
 
1025
}
 
1026
bool Date::operator>=(const Timestamp& rhs)
 
1027
{
 
1028
  return ! (*this <= rhs);
 
1029
}
 
1030
/*
 
1031
 * Comparison operators between a Timestamp and a Date
 
1032
 */
 
1033
bool Timestamp::operator==(const Date& rhs)
 
1034
{
 
1035
  return (_years == rhs._years && _months == rhs._months && _days == rhs._days);
 
1036
}
 
1037
bool Timestamp::operator!=(const Date& rhs)
 
1038
{
 
1039
  return ! (*this == rhs);
 
1040
}
 
1041
bool Timestamp::operator<(const Date& rhs)
 
1042
{
 
1043
  if (_years < rhs._years)
 
1044
    return true;
 
1045
  if (_years > rhs._years)
 
1046
    return false;
 
1047
  /* In same year */
 
1048
  if (_months < rhs._months)
 
1049
    return true;
 
1050
  if (_months > rhs._months)
 
1051
    return false;
 
1052
  /* Same month */
 
1053
  return _days < rhs._days;
 
1054
}
 
1055
bool Timestamp::operator<=(const Date& rhs)
 
1056
{
 
1057
  return (*this < rhs || *this == rhs);
 
1058
}
 
1059
bool Timestamp::operator>(const Date& rhs)
 
1060
{
 
1061
  return ! (*this < rhs);
 
1062
}
 
1063
bool Timestamp::operator>=(const Date& rhs)
 
1064
{
 
1065
  return ! (*this <= rhs);
 
1066
}
 
1067
/*
 
1068
 * Comparison operators between a Timestamp and a DateTime
 
1069
 */
 
1070
bool Timestamp::operator==(const DateTime& rhs)
 
1071
{
 
1072
  return (_years == rhs._years && _months == rhs._months && _days == rhs._days
 
1073
          && _hours == rhs._hours && _minutes == rhs._minutes && _seconds == rhs._seconds);
 
1074
}
 
1075
bool Timestamp::operator!=(const DateTime& rhs)
 
1076
{
 
1077
  return ! (*this == rhs);
 
1078
}
 
1079
bool Timestamp::operator<(const DateTime& rhs)
 
1080
{
 
1081
  if (_years < rhs._years)
 
1082
    return true;
 
1083
  if (_years > rhs._years)
 
1084
    return false;
 
1085
  /* In same year */
 
1086
  if (_months < rhs._months)
 
1087
    return true;
 
1088
  if (_months > rhs._months)
 
1089
    return false;
 
1090
  /* Same month */
 
1091
  if (_days < rhs._days)
 
1092
    return true;
 
1093
  if (_days > rhs._days)
 
1094
     return false;
 
1095
  /* Same day */
 
1096
  if (_hours < rhs._hours)
 
1097
    return true;
 
1098
  if (_hours > rhs._hours)
 
1099
    return false;
 
1100
  /* Same hour */
 
1101
  if (_minutes < rhs._minutes)
 
1102
    return true;
 
1103
  if (_minutes > rhs._minutes)
 
1104
    return false;
 
1105
  /* Same minute */
 
1106
  return _seconds < rhs._seconds;
 
1107
}
 
1108
bool Timestamp::operator<=(const DateTime& rhs)
 
1109
{
 
1110
  return (*this < rhs || *this == rhs);
 
1111
}
 
1112
bool Timestamp::operator>(const DateTime& rhs)
 
1113
{
 
1114
  return ! (*this < rhs);
 
1115
}
 
1116
bool Timestamp::operator>=(const DateTime& rhs)
 
1117
{
 
1118
  return ! (*this <= rhs);
 
1119
}
 
1120
/*
 
1121
 * Comparison operators between two Timestamps
 
1122
 */
 
1123
bool Timestamp::operator==(const Timestamp& rhs)
 
1124
{
 
1125
  return (_epoch_seconds == rhs._epoch_seconds);
 
1126
}
 
1127
bool Timestamp::operator!=(const Timestamp& rhs)
 
1128
{
 
1129
  return ! (*this == rhs);
 
1130
}
 
1131
bool Timestamp::operator<(const Timestamp& rhs)
 
1132
{
 
1133
  return (_epoch_seconds < rhs._epoch_seconds);
 
1134
}
 
1135
bool Timestamp::operator<=(const Timestamp& rhs)
 
1136
{
 
1137
  return (_epoch_seconds <= rhs._epoch_seconds);
 
1138
}
 
1139
bool Timestamp::operator>(const Timestamp& rhs)
 
1140
{
 
1141
  return ! (*this < rhs);
 
1142
}
 
1143
bool Timestamp::operator>=(const Timestamp& rhs)
 
1144
{
 
1145
  return ! (*this <= rhs);
 
1146
}
 
1147
 
 
1148
/**
 
1149
 * Push the contents of the timestamp into the output stream
 
1150
 * as a formatted Timestamp value.
 
1151
 *
 
1152
 * @TODO This unfortunately fails in a weird way...even with std::noskipws, 
 
1153
 * the output stream only reads up to the space in the string... :(
 
1154
 */
 
1155
std::ostream& operator<<(std::ostream& os, const Timestamp& subject)
 
1156
{
 
1157
  return os << subject.years() << '-' 
 
1158
            << std::setw(2) << std::setfill('0') << subject.months() << '-'
 
1159
            << std::setw(2) << std::setfill('0') << subject.days() << ' '
 
1160
            << std::setw(2) << std::setfill('0') << subject.hours() << ':'
 
1161
            << std::setw(2) << std::setfill('0') << subject.minutes() << ':'
 
1162
            << std::setw(2) << std::setfill('0') << subject.seconds();
 
1163
}
 
1164
 
 
1165
bool Time::from_string(const char *from, size_t from_len)
 
1166
{
 
1167
  /*
 
1168
   * Loop through the known time formats and see if
 
1169
   * there is a match.
 
1170
   */
 
1171
  bool matched= false;
 
1172
  TemporalFormat *current_format;
 
1173
  std::vector<TemporalFormat *>::iterator current= known_time_formats.begin();
 
1174
 
 
1175
  while (current != known_time_formats.end())
 
1176
  {
 
1177
    current_format= *current;
 
1178
    if (current_format->matches(from, from_len, this))
 
1179
    {
 
1180
      matched= true;
 
1181
      break;
 
1182
    }
 
1183
    current++;
 
1184
  }
 
1185
 
 
1186
  if (! matched)
 
1187
    return false;
 
1188
  else
 
1189
    return is_valid();
 
1190
}
 
1191
 
 
1192
void Time::to_string(char *to, size_t *to_len) const
 
1193
{
 
1194
  *to_len= sprintf(to
 
1195
                 , "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
 
1196
                 , _hours
 
1197
                 , _minutes
 
1198
                 , _seconds);
 
1199
}
 
1200
 
 
1201
void Date::to_string(char *to, size_t *to_len) const
 
1202
{
 
1203
  *to_len= sprintf(to
 
1204
                 , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
 
1205
                 , _years
 
1206
                 , _months
 
1207
                 , _days);
 
1208
}
 
1209
 
 
1210
void DateTime::to_string(char *to, size_t *to_len) const
 
1211
{
 
1212
  /* If the temporal has a microsecond component, use a slightly different output */
 
1213
  if (_useconds == 0)
 
1214
  {
 
1215
    *to_len= sprintf(to
 
1216
                  , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
 
1217
                  , _years
 
1218
                  , _months
 
1219
                  , _days
 
1220
                  , _hours
 
1221
                  , _minutes
 
1222
                  , _seconds);
 
1223
  }
 
1224
  else
 
1225
  {
 
1226
    *to_len= sprintf(to
 
1227
                  , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32
 
1228
                  , _years
 
1229
                  , _months
 
1230
                  , _days
 
1231
                  , _hours
 
1232
                  , _minutes
 
1233
                  , _seconds
 
1234
                  , _useconds);
 
1235
  }
 
1236
}
 
1237
 
 
1238
void MicroTimestamp::to_string(char *to, size_t *to_len) const
 
1239
{
 
1240
  *to_len= sprintf(to
 
1241
                 , "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32
 
1242
                 , _years
 
1243
                 , _months
 
1244
                 , _days
 
1245
                 , _hours
 
1246
                 , _minutes
 
1247
                 , _seconds
 
1248
                 , _useconds);
 
1249
}
 
1250
 
 
1251
void Time::to_decimal(my_decimal *to) const
 
1252
{
 
1253
  int64_t time_portion= (((_hours * 100L) + _minutes) * 100L) + _seconds;
 
1254
  (void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
 
1255
  if (_useconds > 0)
 
1256
  {
 
1257
    to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
 
1258
    to->frac= 6;
 
1259
  }
 
1260
}
 
1261
 
 
1262
void Date::to_decimal(my_decimal *to) const
 
1263
{
 
1264
  int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
 
1265
  (void) int2my_decimal(E_DEC_FATAL_ERROR, date_portion, false, to);
 
1266
}
 
1267
 
 
1268
void DateTime::to_decimal(my_decimal *to) const
 
1269
{
 
1270
  int64_t date_portion= (((_years * 100L) + _months) * 100L) + _days;
 
1271
  int64_t time_portion= (((((date_portion * 100L) + _hours) * 100L) + _minutes) * 100L) + _seconds;
 
1272
  (void) int2my_decimal(E_DEC_FATAL_ERROR, time_portion, false, to);
 
1273
  if (_useconds > 0)
 
1274
  {
 
1275
    to->buf[(to->intg-1) / 9 + 1]= _useconds * 1000;
 
1276
    to->frac= 6;
 
1277
  }
 
1278
}
 
1279
 
 
1280
void Date::to_int64_t(int64_t *to) const
 
1281
{
 
1282
  *to= (_years * INT32_C(10000)) 
 
1283
     + (_months * INT32_C(100)) 
 
1284
     + _days;
 
1285
}
 
1286
 
 
1287
void Date::to_int32_t(int32_t *to) const
 
1288
{
 
1289
  *to= (_years * INT32_C(10000)) 
 
1290
     + (_months * INT32_C(100)) 
 
1291
     + _days;
 
1292
}
 
1293
 
 
1294
void Time::to_int32_t(int32_t *to) const
 
1295
{
 
1296
  *to= (_hours * INT32_C(10000)) 
 
1297
     + (_minutes * INT32_C(100)) 
 
1298
     + _seconds;
 
1299
}
 
1300
 
 
1301
void DateTime::to_int64_t(int64_t *to) const
 
1302
{
 
1303
  *to= ((
 
1304
       (_years * INT64_C(10000)) 
 
1305
     + (_months * INT64_C(100)) 
 
1306
     + _days
 
1307
       ) * INT64_C(1000000))
 
1308
     + (
 
1309
       (_hours * INT64_C(10000)) 
 
1310
     + (_minutes * INT64_C(100) )
 
1311
     + _seconds
 
1312
     );
 
1313
}
 
1314
 
 
1315
void Date::to_tm(struct tm *to) const
 
1316
{
 
1317
  to->tm_sec= 0;
 
1318
  to->tm_min= 0;
 
1319
  to->tm_hour= 0;
 
1320
  to->tm_mday= _days; /* Drizzle format uses ordinal, standard tm does too! */
 
1321
  to->tm_mon= _months - 1; /* Drizzle format uses ordinal, standard tm does NOT! */
 
1322
  to->tm_year= _years - 1900;
 
1323
}
 
1324
 
 
1325
void DateTime::to_tm(struct tm *to) const
 
1326
{
 
1327
  to->tm_sec= _seconds;
 
1328
  to->tm_min= _minutes;
 
1329
  to->tm_hour= _hours;
 
1330
  to->tm_mday= _days; /* Drizzle format uses ordinal, standard tm does too! */
 
1331
  to->tm_mon= _months - 1; /* Drizzle format uses ordinal, standard tm does NOT! */
 
1332
  to->tm_year= _years - 1900;
 
1333
}
 
1334
 
 
1335
bool Date::from_julian_day_number(const int64_t from)
 
1336
{
 
1337
  gregorian_date_from_julian_day_number(from, &_years, &_months, &_days);
 
1338
  return is_valid();
 
1339
}
 
1340
 
 
1341
void Date::to_julian_day_number(int64_t *to) const
 
1342
{
 
1343
  *to= julian_day_number_from_gregorian_date(_years, _months, _days);
 
1344
}
 
1345
 
 
1346
/**
 
1347
 * Ignore overflow and pass-through to DateTime::from_int64_t()
 
1348
 */
 
1349
bool Date::from_int32_t(const int32_t from)
 
1350
{
 
1351
  return ((DateTime *) this)->from_int64_t((int64_t) from);
 
1352
}
 
1353
 
 
1354
/**
 
1355
 * Attempt to interpret the supplied 4-byte integer as
 
1356
 * a TIME value in the format HHmmSS
 
1357
 */
 
1358
bool Time::from_int32_t(const int32_t from)
 
1359
{
 
1360
  uint32_t copy_from= (uint32_t) from;
 
1361
  _hours= copy_from % INT32_C(10000);
 
1362
  _minutes= copy_from % INT32_C(100);
 
1363
  _seconds= copy_from & 3; /* Masks off all but last 2 digits */
 
1364
  return is_valid();
 
1365
}
 
1366
 
 
1367
/**
 
1368
 * We try to intepret the incoming number as a datetime "string".
 
1369
 * This is pretty much a hack for usability, but keeps us compatible
 
1370
 * with MySQL.
 
1371
 */
 
1372
bool DateTime::from_int64_t(const int64_t from)
 
1373
{
 
1374
  int64_t copy_from= from;
 
1375
  int64_t part1;
 
1376
  int64_t part2;
 
1377
 
 
1378
  if (copy_from == 0LL)
 
1379
    return false;
 
1380
 
 
1381
  if (copy_from < 10000101000000LL)
 
1382
  {
 
1383
    if (copy_from < 101)
 
1384
      return false;
 
1385
    else if (copy_from <= (DRIZZLE_YY_PART_YEAR-1)*10000L+1231L)
 
1386
      copy_from= (copy_from+20000000L)*1000000L;                 /* YYMMDD, year: 2000-2069 */
 
1387
    else if (copy_from < (DRIZZLE_YY_PART_YEAR)*10000L+101L)
 
1388
      return false;
 
1389
    else if (copy_from <= 991231L)
 
1390
      copy_from= (copy_from+19000000L)*1000000L;                 /* YYMMDD, year: 1970-1999 */
 
1391
    else if (copy_from < 10000101L)
 
1392
      return false;
 
1393
    else if (copy_from <= 99991231L)
 
1394
      copy_from= copy_from*1000000L;
 
1395
    else if (copy_from < 101000000L)
 
1396
      return false;
 
1397
    else if (copy_from <= (DRIZZLE_YY_PART_YEAR-1) * 10000000000LL + 1231235959LL)
 
1398
      copy_from= copy_from + 20000000000000LL;                   /* YYMMDDHHMMSS, 2000-2069 */
 
1399
    else if (copy_from <  DRIZZLE_YY_PART_YEAR * 10000000000LL + 101000000LL)
 
1400
      return false;
 
1401
    else if (copy_from <= 991231235959LL)
 
1402
      copy_from= copy_from + 19000000000000LL;          /* YYMMDDHHMMSS, 1970-1999 */
 
1403
  }
 
1404
 
 
1405
  part1= (int64_t) (copy_from / 1000000LL);
 
1406
  part2= (int64_t) (copy_from - (int64_t) part1 * 1000000LL);
 
1407
  _years=  (uint32_t) (part1/10000L);  
 
1408
  
 
1409
  part1%=10000L;
 
1410
  _months= (uint32_t) part1 / 100;
 
1411
  _days=   (uint32_t) part1 % 100;
 
1412
  _hours=  (uint32_t) (part2/10000L);  
 
1413
 
 
1414
  part2%=10000L;
 
1415
  _minutes= (uint32_t) part2 / 100;
 
1416
  _seconds= (uint32_t) part2 % 100;
 
1417
 
 
1418
  set_epoch_seconds();
 
1419
  return is_valid();
 
1420
}
 
1421
 
 
1422
bool Date::in_unix_epoch() const
 
1423
{
 
1424
  return in_unix_epoch_range(_years, _months, _days, 0, 0, 0);
 
1425
}
 
1426
 
 
1427
bool DateTime::in_unix_epoch() const
 
1428
{
 
1429
  return in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds);
 
1430
}
 
1431
 
 
1432
bool Date::from_tm(const struct tm *from)
 
1433
{
 
1434
  _years= 1900 + from->tm_year;
 
1435
  _months= 1 + from->tm_mon; /* Month is NOT ordinal for struct tm! */
 
1436
  _days= from->tm_mday; /* Day IS ordinal for struct tm */
 
1437
  _hours= from->tm_hour;
 
1438
  _minutes= from->tm_min;
 
1439
  _seconds= from->tm_sec;
 
1440
  /* Set hires precision to zero */
 
1441
  _useconds= 0;
 
1442
  _nseconds= 0;
 
1443
 
 
1444
  set_epoch_seconds();
 
1445
  return is_valid();
 
1446
}
 
1447
 
 
1448
/* 
 
1449
 * We convert as if it's a Datetime, then simply
 
1450
 * drop the date portions...
 
1451
 */
 
1452
bool Time::from_time_t(const time_t from)
 
1453
{
 
1454
  struct tm broken_time;
 
1455
  struct tm *result;
 
1456
 
 
1457
  result= gmtime_r(&from, &broken_time);
 
1458
  if (result != NULL)
 
1459
  {
 
1460
    _years= 0;
 
1461
    _months= 0;
 
1462
    _days= 0;
 
1463
    _hours= broken_time.tm_hour;
 
1464
    _minutes= broken_time.tm_min;
 
1465
    _seconds= broken_time.tm_sec;
 
1466
    _epoch_seconds= 0; /* Don't store the time_t, since we only use part of it */
 
1467
    /* Set hires precision to zero */
 
1468
    _useconds= 0;
 
1469
    _nseconds= 0;
 
1470
    return true; /* Always true... */
 
1471
  }
 
1472
  else 
 
1473
    return false;
 
1474
}
 
1475
 
 
1476
bool Date::from_time_t(const time_t from)
 
1477
{
 
1478
  struct tm broken_time;
 
1479
  struct tm *result;
 
1480
 
 
1481
  result= gmtime_r(&from, &broken_time);
 
1482
  if (result != NULL)
 
1483
  {
 
1484
    _years= 1900 + broken_time.tm_year;
 
1485
    _months= 1 + broken_time.tm_mon; /* Month is NOT ordinal for struct tm! */
 
1486
    _days= broken_time.tm_mday; /* Day IS ordinal for struct tm */
 
1487
    _hours= 0;
 
1488
    _minutes= 0;
 
1489
    _seconds= 0;
 
1490
    _epoch_seconds= 0; /* Don't store the time_t, since we only use part of it */
 
1491
    /* Set hires precision to zero */
 
1492
    _useconds= 0;
 
1493
    _nseconds= 0;
 
1494
    return is_valid();
 
1495
  }
 
1496
  else 
 
1497
    return false;
 
1498
}
 
1499
 
 
1500
bool DateTime::from_time_t(const time_t from)
 
1501
{
 
1502
  struct tm broken_time;
 
1503
  struct tm *result;
 
1504
 
 
1505
  result= gmtime_r(&from, &broken_time);
 
1506
  if (result != NULL)
 
1507
  {
 
1508
    _years= 1900 + broken_time.tm_year;
 
1509
    _months= 1 + broken_time.tm_mon; /* Month is NOT ordinal for struct tm! */
 
1510
    _days= broken_time.tm_mday; /* Day IS ordinal for struct tm */
 
1511
    _hours= broken_time.tm_hour;
 
1512
    _minutes= broken_time.tm_min;
 
1513
    _seconds= broken_time.tm_sec;
 
1514
    _epoch_seconds= from;
 
1515
    /* Set hires precision to zero */
 
1516
    _useconds= 0;
 
1517
    _nseconds= 0;
 
1518
    return is_valid();
 
1519
  }
 
1520
  else 
 
1521
    return false;
 
1522
}
 
1523
 
 
1524
void Date::to_time_t(time_t *to) const
 
1525
{
 
1526
  if (in_unix_epoch())
 
1527
    *to= _epoch_seconds;
 
1528
  *to= 0;
 
1529
}
 
1530
 
 
1531
void Timestamp::to_time_t(time_t *to) const
 
1532
{
 
1533
  *to= _epoch_seconds;
 
1534
}
 
1535
 
 
1536
void MicroTimestamp::to_timeval(struct timeval *to) const
 
1537
{
 
1538
  to->tv_sec= _epoch_seconds;
 
1539
  to->tv_usec= _useconds;
 
1540
}
 
1541
 
 
1542
void NanoTimestamp::to_timespec(struct timespec *to) const
 
1543
{
 
1544
  to->tv_sec= _epoch_seconds;
 
1545
  to->tv_nsec= _nseconds;
 
1546
}
 
1547
 
 
1548
bool Date::is_valid() const
 
1549
{
 
1550
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
 
1551
      && (_months >= 1 && _months <= 12)
 
1552
      && (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months));
 
1553
}
 
1554
 
 
1555
bool Time::is_valid() const
 
1556
{
 
1557
  return (_years == 0)
 
1558
      && (_months == 0)
 
1559
      && (_days == 0)
 
1560
      && (_hours <= 23)
 
1561
      && (_minutes <= 59)
 
1562
      && (_seconds <= 59); /* No Leap second... TIME is for elapsed time... */
 
1563
}
 
1564
 
 
1565
bool DateTime::is_valid() const
 
1566
{
 
1567
  return (_years >= DRIZZLE_MIN_YEARS_SQL && _years <= DRIZZLE_MAX_YEARS_SQL)
 
1568
      && (_months >= 1 && _months <= 12)
 
1569
      && (_days >= 1 && _days <= days_in_gregorian_year_month(_years, _months))
 
1570
      && (_hours <= 23)
 
1571
      && (_minutes <= 59)
 
1572
      && (_seconds <= 61); /* Leap second... */
 
1573
}
 
1574
 
 
1575
bool Timestamp::is_valid() const
 
1576
{
 
1577
  return DateTime::is_valid() 
 
1578
      && in_unix_epoch_range(_years, _months, _days, _hours, _minutes, _seconds);
 
1579
}
 
1580
 
 
1581
bool MicroTimestamp::is_valid() const
 
1582
{
 
1583
  return Timestamp::is_valid()
 
1584
      && (_useconds <= UINT32_C(999999));
 
1585
}
 
1586
 
 
1587
bool NanoTimestamp::is_valid() const
 
1588
{
 
1589
  return Timestamp::is_valid()
 
1590
      && (_useconds <= UINT32_C(999999))
 
1591
      && (_nseconds <= UINT32_C(999999999));
 
1592
}
 
1593
 
 
1594
} /* end namespace drizzled */