~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

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