~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.h

  • Committer: Monty Taylor
  • Date: 2008-10-23 00:05:28 UTC
  • Revision ID: monty@inaugust.com-20081023000528-grdvrd8c4058nutm
Moved my_handler to myisam, which is where it actually belongs.

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-2009 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
/**
22
 
 * @file
23
 
 *
24
 
 * Defines the API for dealing with temporal data inside the server.
25
 
 *
26
 
 * The Temporal class is the base class for all data of any temporal
27
 
 * type.  A number of derived classes define specialized classes
28
 
 * representng various date, date-time, time, or timestamp types.
29
 
 *
30
 
 * All Temporal derived classes are ValueObjects.  That is to say that
31
 
 * Temporal class instances are not part of the Item hierarchy and serve
32
 
 * <em>only</em> to represent a time or date-related piece of data.
33
 
 *
34
 
 * @note
35
 
 *
36
 
 * Low-level calendrical calculations are done via routines in the
37
 
 * calendar.cc file.
38
 
 *
39
 
 * @see drizzled/calendar.cc
40
 
 */
41
 
 
42
 
#ifndef DRIZZLED_TEMPORAL_H
43
 
#define DRIZZLED_TEMPORAL_H
44
 
 
45
 
#define DRIZZLE_MAX_SECONDS 59
46
 
#define DRIZZLE_MAX_MINUTES 59
47
 
#define DRIZZLE_MAX_HOURS 23
48
 
#define DRIZZLE_MAX_DAYS 31
49
 
#define DRIZZLE_MAX_MONTHS 12
50
 
#define DRIZZLE_MAX_YEARS_SQL 9999
51
 
#define DRIZZLE_MAX_YEARS_EPOCH 2038
52
 
#define DRIZZLE_MIN_SECONDS 0
53
 
#define DRIZZLE_MIN_MINUTES 0
54
 
#define DRIZZLE_MIN_HOURS 0
55
 
#define DRIZZLE_MIN_DAYS 1
56
 
#define DRIZZLE_MIN_MONTHS 1
57
 
#define DRIZZLE_MIN_YEARS_SQL 1
58
 
#define DRIZZLE_MIN_YEARS_EPOCH 1970
59
 
 
60
 
#define DRIZZLE_SECONDS_IN_MINUTE 60
61
 
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
62
 
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
63
 
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
64
 
 
65
 
#define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
66
 
 
67
 
#define DRIZZLE_YY_PART_YEAR  70
68
 
 
69
 
#include "drizzled/calendar.h"
70
 
 
71
 
#include <cassert>
72
 
#include <ostream>
73
 
 
74
 
/* Outside forward declarations */
75
 
class my_decimal;
76
 
 
77
 
namespace drizzled
78
 
{
79
 
 
80
 
/* Forward declaration needed */
81
 
class TemporalInterval;
82
 
class TemporalIntervalYear;
83
 
class TemporalIntervalDayOrLess;
84
 
class TemporalIntervalDayOrWeek;
85
 
class TemporalIntervalYearMonth;
86
 
 
87
 
/**
88
 
 * Base class for all temporal data classes.
89
 
 */
90
 
class Temporal
91
 
{
92
 
protected:
93
 
  enum calendar _calendar;
94
 
  uint32_t _years;
95
 
  uint32_t _months;
96
 
  uint32_t _days;
97
 
  uint32_t _hours;
98
 
  uint32_t _minutes;
99
 
  uint32_t _seconds;
100
 
  time_t _epoch_seconds;
101
 
  uint32_t _useconds;
102
 
  uint32_t _nseconds;
103
 
  /** Set on some operator overloads.  Indicates that an overflow occurred. */
104
 
  bool _overflow;
105
 
  /** Returns number of seconds in time components (hour + minute + second) */
106
 
  uint64_t _cumulative_seconds_in_time() const;
107
 
  /** Resets all temporal components to zero */
108
 
  inline void _reset()
109
 
  {
110
 
    _years= _months= _days= _hours= _minutes=
111
 
      _seconds= _epoch_seconds= _useconds= _nseconds= 0;
112
 
  }
113
 
 
114
 
public:
115
 
  Temporal();
116
 
  virtual ~Temporal() {}
117
 
 
118
 
  /** Returns the calendar component. */
119
 
  inline enum calendar calendar() const {return _calendar;}
120
 
  /** Sets the nseconds component. */
121
 
  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
122
 
  /** Returns the nanoseconds component. */
123
 
  inline uint32_t nseconds() const {return _nseconds;}
124
 
  /** Sets the useconds component. */
125
 
  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
126
 
  /** Returns the microsseconds component. */
127
 
  inline uint32_t useconds() const {return _useconds;}
128
 
  /**
129
 
   * Sets the epoch_seconds component automatically,
130
 
   * based on the temporal's components.
131
 
   */
132
 
  void set_epoch_seconds();
133
 
  /** Sets the epch_seconds component manually. */
134
 
  inline void set_epoch_seconds(const uint32_t epoch_second)
135
 
  {_epoch_seconds= epoch_second;}
136
 
  /** Returns the UNIX epoch seconds component. */
137
 
  inline time_t epoch_seconds() const {return _epoch_seconds;}
138
 
  /** Sets the seconds component. */
139
 
  inline void set_seconds(const uint32_t second) {_seconds= second;}
140
 
  /** Returns the seconds component. */
141
 
  inline uint32_t seconds() const {return _seconds;}
142
 
  /** Sets the days component. */
143
 
  inline void set_minutes(const uint32_t minute) {_minutes= minute;}
144
 
  /** Returns the minutes component. */
145
 
  inline uint32_t minutes() const {return _minutes;}
146
 
  /** Sets the hours component. */
147
 
  inline void set_hours(const uint32_t hour) {_hours= hour;}
148
 
  /** Returns the hours component. */
149
 
  inline uint32_t hours() const {return _hours;}
150
 
  /** Sets the days component. */
151
 
  inline void set_days(const uint32_t day) {_days= day;}
152
 
  /** Returns the days component. */
153
 
  inline uint32_t days() const {return _days;}
154
 
  /** Sets the months component. */
155
 
  inline void set_months(const uint32_t month) {_months= month;}
156
 
  /** Returns the months component. */
157
 
  inline uint32_t months() const {return _months;}
158
 
  /** Sets the years component. */
159
 
  inline void set_years(const uint32_t year) {_years= year;}
160
 
  /** Returns the years component. */
161
 
  inline uint32_t years() const {return _years;}
162
 
  /** Returns whether the overflow flag was set
163
 
   *  (which can occur during an overloaded operator's execution) */
164
 
  inline bool overflow() const {return _overflow;}
165
 
 
166
 
  /** Returns whether the temporal value is valid as a date. */
167
 
  virtual bool is_valid_date() const= 0;
168
 
  /** Returns whether the temporal value is valid as a datetime. */
169
 
  virtual bool is_valid_datetime() const= 0;
170
 
  /** Returns whether the temporal value is valid as a time. */
171
 
  virtual bool is_valid_time() const= 0;
172
 
  /** Returns whether the temporal value is valid as a UNIX timestamp. */
173
 
  virtual bool is_valid_timestamp() const= 0;
174
 
 
175
 
  /**
176
 
   * Returns whether the temporal
177
 
   * value is valid. Each subclass defines what is
178
 
   * valid for the range of temporal data it contains.
179
 
   */
180
 
  virtual bool is_valid() const= 0;
181
 
 
182
 
  /**
183
 
   * All Temporal derived classes must implement
184
 
   * conversion routines for converting to and from
185
 
   * a string. Subclasses implement other conversion
186
 
   * routines, but should always follow these notes:
187
 
   *
188
 
   * 1) Ensure that ALL from_xxx methods call is_valid()
189
 
   * 2) Ensure that ALL to_xxx methods are void returns and
190
 
   *    do not call is_valid()
191
 
   *
192
 
   * This minimizes the repeated bounds-checking to
193
 
   * just the conversion from_xxx routines.
194
 
   */
195
 
  friend class TemporalFormat;
196
 
};
197
 
 
198
 
/* Forward declaration needed */
199
 
class DateTime;
200
 
class Timestamp;
201
 
class Time;
202
 
 
203
 
/**
204
 
 * Class representing temporal components in a valid
205
 
 * SQL date range, with no time component
206
 
 */
207
 
class Date: public Temporal
208
 
{
209
 
public:
210
 
  Date() :Temporal() {}
211
 
  /**
212
 
   * Comparison operator overloads to compare a Date against
213
 
   * another Date value.
214
 
   *
215
 
   * @param Date to compare against.
216
 
   */
217
 
  virtual bool operator==(const Date &rhs);
218
 
  virtual bool operator!=(const Date &rhs);
219
 
  virtual bool operator>(const Date &rhs);
220
 
  virtual bool operator>=(const Date &rhs);
221
 
  virtual bool operator<(const Date &rhs);
222
 
  virtual bool operator<=(const Date &rhs);
223
 
 
224
 
  /**
225
 
   * Comparison operator overloads to compare a Date against
226
 
   * a DateTime value.
227
 
   *
228
 
   * @param DateTime to compare against.
229
 
   */
230
 
  virtual bool operator==(const DateTime &rhs);
231
 
  virtual bool operator!=(const DateTime &rhs);
232
 
  virtual bool operator>(const DateTime &rhs);
233
 
  virtual bool operator>=(const DateTime &rhs);
234
 
  virtual bool operator<(const DateTime &rhs);
235
 
  virtual bool operator<=(const DateTime &rhs);
236
 
 
237
 
  /**
238
 
   * Comparison operator overloads to compare this against
239
 
   * a Timestamp value.
240
 
   *
241
 
   * @param Timestamp to compare against.
242
 
   */
243
 
  virtual bool operator==(const Timestamp &rhs);
244
 
  virtual bool operator!=(const Timestamp &rhs);
245
 
  virtual bool operator>(const Timestamp &rhs);
246
 
  virtual bool operator>=(const Timestamp &rhs);
247
 
  virtual bool operator<(const Timestamp &rhs);
248
 
  virtual bool operator<=(const Timestamp &rhs);
249
 
 
250
 
  /**
251
 
   * Operator overload for adding/subtracting another Date
252
 
   * (or subclass) to/from this temporal.  When subtracting
253
 
   * or adding two Dates, we return a new Date instance.
254
 
   *
255
 
   * @param Date instance to add/subtract to/from
256
 
   */
257
 
  const Date operator-(const Date &rhs);
258
 
  const Date operator+(const Date &rhs);
259
 
  Date& operator+=(const Date &rhs);
260
 
  Date& operator-=(const Date &rhs);
261
 
 
262
 
  /**
263
 
   * Operator to add/subtract a Time from a Time.
264
 
   * We can return a Time new temporal instance.
265
 
   *
266
 
   * @param Temporal instance to add/subtract to/from
267
 
   */
268
 
  const Date operator-(const Time &rhs);
269
 
  const Date operator+(const Time &rhs);
270
 
  Date& operator-=(const Time &rhs);
271
 
  Date& operator+=(const Time &rhs);
272
 
 
273
 
 
274
 
  /**
275
 
   * Operator overload for adding/subtracting a DateTime
276
 
   * (or subclass) to/from this temporal.  When subtracting
277
 
   * or adding two Dates, we return a new Date instance.
278
 
   *
279
 
   * @param DateTime instance to add/subtract to/from
280
 
   */
281
 
  const Date operator-(const DateTime &rhs);
282
 
  const Date operator+(const DateTime &rhs);
283
 
  Date& operator+=(const DateTime &rhs);
284
 
  Date& operator-=(const DateTime &rhs);
285
 
 
286
 
 
287
 
  /**
288
 
   * Operator overload for when a DateTime instance is
289
 
   * assigned to a Date.  We do a copy of the DateTime's
290
 
   * date-related components.
291
 
   *
292
 
   * @param The DateTime to copy from
293
 
   */
294
 
  Date& operator=(const DateTime &rhs);
295
 
 
296
 
  virtual bool is_valid_date() const {return is_valid();}
297
 
  virtual bool is_valid_datetime() const {return is_valid();}
298
 
  virtual bool is_valid_time() const {return false;}
299
 
  virtual bool is_valid_timestamp() const
300
 
  {
301
 
    return is_valid() && in_unix_epoch();
302
 
  }
303
 
 
304
 
  /** Returns whether the temporal value is valid date. */
305
 
  virtual bool is_valid() const;
306
 
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
307
 
  virtual bool in_unix_epoch() const;
308
 
 
309
 
  /**
310
 
   * Fills a supplied char string with a
311
 
   * string representation of the Date
312
 
   * value.
313
 
   *
314
 
   * @param C-String to fill.
315
 
   * @param Length of to C-String
316
 
   * @returns length of string written (including trailing '\0').
317
 
   *          If output was truncated, returns length that would have
318
 
   *          been outputted.
319
 
   */
320
 
  virtual int to_string(char *to, size_t to_len) const;
321
 
 
322
 
  /**
323
 
   * Maximum length of C-String needed to represent type
324
 
   * (including '\0').
325
 
   */
326
 
  static const int MAX_STRING_LENGTH= 11;
327
 
 
328
 
  /**
329
 
   * Attempts to populate the Date instance based
330
 
   * on the contents of a supplied string.
331
 
   *
332
 
   * Returns whether the conversion was
333
 
   * successful.
334
 
   *
335
 
   * @param String to convert from
336
 
   * @param Length of supplied string (not including trailing '\0').
337
 
   */
338
 
  virtual bool from_string(const char *from, size_t from_len);
339
 
 
340
 
  /**
341
 
   * Fills a supplied 8-byte integer pointer with an
342
 
   * integer representation of the Date
343
 
   * value.
344
 
   *
345
 
   * @param Integer to fill.
346
 
   */
347
 
  virtual void to_int64_t(int64_t *to) const;
348
 
 
349
 
  /**
350
 
   * Fills a supplied 4-byte integer pointer with an
351
 
   * integer representation of the Date
352
 
   * value.
353
 
   *
354
 
   * @param Integer to fill.
355
 
   */
356
 
  virtual void to_int32_t(int32_t *to) const;
357
 
 
358
 
  /**
359
 
   * Attempts to populate the Date instance based
360
 
   * on the contents of a supplied 4-byte integer.
361
 
   *
362
 
   * Returns whether the conversion was
363
 
   * successful.
364
 
   *
365
 
   * @param Integer to convert from
366
 
   */
367
 
  virtual bool from_int32_t(const int32_t from);
368
 
 
369
 
  /**
370
 
   * Fills a supplied int64_t with the Julian Day Number
371
 
   * representation of this Date.
372
 
   *
373
 
   * @note Julian Day Number != julian day!
374
 
   *
375
 
   * Julian Day Number is the monotonically increasing number
376
 
   * of days from the start of the Julian calendar (~4713 B.C.)
377
 
   *
378
 
   * julian day is the ordinal day number of a day in a year.
379
 
   *
380
 
   * @param int64_t to fill
381
 
   */
382
 
  void to_julian_day_number(int64_t *to) const;
383
 
 
384
 
  /**
385
 
   * Attempts to populate the Date instance based
386
 
   * on the contents of a supplied Julian Day Number
387
 
   *
388
 
   * Returns whether the conversion was
389
 
   * successful.
390
 
   *
391
 
   * @param Integer to convert from
392
 
   */
393
 
  bool from_julian_day_number(const int64_t from);
394
 
 
395
 
  /**
396
 
   * Fills a supplied tm pointer with an
397
 
   * representation of the Date
398
 
   * value.
399
 
   *
400
 
   * @param tm to fill.
401
 
   */
402
 
  virtual void to_tm(struct tm *to) const;
403
 
 
404
 
  /**
405
 
   * Attempts to populate the Date instance based
406
 
   * on the contents of a supplied pointer to struct tm
407
 
   * (broken time).
408
 
   *
409
 
   * Returns whether the conversion was
410
 
   * successful.
411
 
   *
412
 
   * @param Pointe rto the struct tm to convert from
413
 
   */
414
 
  virtual bool from_tm(const struct tm *from);
415
 
 
416
 
  /**
417
 
   * Attempts to convert the Date value into
418
 
   * a supplied time_t.
419
 
   *
420
 
   * @param Pointer to a time_t to convert to
421
 
   */
422
 
  virtual void to_time_t(time_t *to) const;
423
 
 
424
 
  /**
425
 
   * Attempts to populate the Date instance based
426
 
   * on the contents of a supplied time_t
427
 
   *
428
 
   * Returns whether the conversion was
429
 
   * successful.
430
 
   *
431
 
   * @param time_t to convert from
432
 
   */
433
 
  virtual bool from_time_t(const time_t from);
434
 
 
435
 
  /**
436
 
   * Fills a supplied my_decimal with a representation of
437
 
   * the Date value.
438
 
   *
439
 
   * @param Pointer to the my_decimal to fill
440
 
   */
441
 
  virtual void to_decimal(my_decimal *to) const;
442
 
 
443
 
  friend class TemporalInterval;
444
 
  friend class Timestamp;
445
 
};
446
 
 
447
 
/* Forward declare needed for friendship */
448
 
class DateTime;
449
 
 
450
 
/**
451
 
 * Class representing temporal components having only
452
 
 * a time component, with no date structure
453
 
 */
454
 
class Time: public Temporal
455
 
{
456
 
public:
457
 
  Time() :Temporal() {}
458
 
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
459
 
  static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
460
 
 
461
 
  /**
462
 
   * Comparison operator overloads to compare a Time against
463
 
   * another Time value.
464
 
   *
465
 
   * @param Time to compare against.
466
 
   */
467
 
  bool operator==(const Time &rhs);
468
 
  bool operator!=(const Time &rhs);
469
 
  bool operator>(const Time &rhs);
470
 
  bool operator>=(const Time &rhs);
471
 
  bool operator<(const Time &rhs);
472
 
  bool operator<=(const Time &rhs);
473
 
  /**
474
 
   * Operator to add/subtract a Time from a Time.
475
 
   * We can return a Time new temporal instance.
476
 
   *
477
 
   * @param Temporal instance to add/subtract to/from
478
 
   */
479
 
  const Time operator-(const Time &rhs);
480
 
  const Time operator+(const Time &rhs);
481
 
  Time& operator-=(const Time &rhs);
482
 
  Time& operator+=(const Time &rhs);
483
 
 
484
 
  bool is_valid_date() const {return false;}
485
 
  bool is_valid_datetime() const {return false;}
486
 
  bool is_valid_time() const {return is_valid();}
487
 
  bool is_valid_timestamp() const {return false;}
488
 
  /** Returns whether the temporal value is valid date. */
489
 
  bool is_valid() const;
490
 
 
491
 
  /**
492
 
   * Fills a supplied char string with a
493
 
   * string representation of the Time
494
 
   * value.
495
 
   *
496
 
   * @param C-String to fill
497
 
   * @param Length of to C-String
498
 
   * @returns length of string written (not including trailing '\0').
499
 
   *          If output was truncated, returns length that would have
500
 
   *          been outputted.
501
 
   */
502
 
  int to_string(char *to, size_t to_len) const;
503
 
 
504
 
  /**
505
 
   * Maximum length of C-String needed to represent type
506
 
   * (including '\0').
507
 
   */
508
 
  static const int MAX_STRING_LENGTH= 9;
509
 
 
510
 
 
511
 
  /**
512
 
   * Attempts to populate the Time instance based
513
 
   * on the contents of a supplied string.
514
 
   *
515
 
   * Returns whether the conversion was
516
 
   * successful.
517
 
   *
518
 
   * @param String to convert from
519
 
   * @param Length of supplied string
520
 
   */
521
 
  bool from_string(const char *from, size_t from_len);
522
 
 
523
 
  /**
524
 
   * Fills a supplied 4-byte integer pointer with an
525
 
   * integer representation of the Time
526
 
   * value.
527
 
   *
528
 
   * @param Integer to fill.
529
 
   */
530
 
  void to_int32_t(int32_t *to) const;
531
 
 
532
 
  /**
533
 
   * Attempts to populate the Time instance based
534
 
   * on the contents of a supplied 4-byte integer.
535
 
   *
536
 
   * Returns whether the conversion was
537
 
   * successful.
538
 
   *
539
 
   * @param Integer to convert from
540
 
   */
541
 
  bool from_int32_t(const int32_t from);
542
 
 
543
 
  /**
544
 
   * Attempts to populate the Time instance based
545
 
   * on the contents of a supplied time_t
546
 
   *
547
 
   * Returns whether the conversion was
548
 
   * successful.
549
 
   *
550
 
   * @note
551
 
   *
552
 
   * We can only convert *from* a time_t, not back
553
 
   * to a time_t since it would be a lossy conversion.
554
 
   *
555
 
   * @param time_t to convert from
556
 
   */
557
 
  bool from_time_t(const time_t from);
558
 
 
559
 
  /**
560
 
   * Fills a supplied my_decimal with a representation of
561
 
   * the Time value.
562
 
   *
563
 
   * @param Pointer to the my_decimal to fill
564
 
   */
565
 
  void to_decimal(my_decimal *to) const;
566
 
 
567
 
  friend class Date;
568
 
  friend class DateTime;
569
 
};
570
 
 
571
 
/**
572
 
 * Class representing temporal components in a valid
573
 
 * SQL datetime range, including a time component
574
 
 */
575
 
class DateTime: public Date
576
 
{
577
 
public:
578
 
  DateTime() :Date() {}
579
 
 
580
 
  friend class TemporalInterval;
581
 
 
582
 
  /** Returns whether the DateTime (or subclass) instance
583
 
   *  is in the Unix Epoch.
584
 
   */
585
 
  bool in_unix_epoch() const;
586
 
  /** Returns whether the temporal value is valid datetime. */
587
 
  virtual bool is_valid() const;
588
 
 
589
 
  /**
590
 
   * It's not possible to convert to and from a DateTime and
591
 
   * a 4-byte integer, so let us know if we try and do it!
592
 
   */
593
 
  void to_int32_t(int32_t *) const {assert(0);}
594
 
  bool from_int32_t(int32_t) {assert(0); return false;}
595
 
 
596
 
  /**
597
 
   * Fills a supplied char string with a
598
 
   * string representation of the DateTime
599
 
   * value.
600
 
   *
601
 
   * @param C-String to fill
602
 
   * @param Length of to C-String
603
 
   * @returns length of string written (not including trailing '\0').
604
 
   *          If output was truncated, returns length that would have
605
 
   *          been outputted.
606
 
   */
607
 
  virtual int to_string(char *to, size_t to_len) const;
608
 
 
609
 
  /**
610
 
   * Maximum length of C-String needed to represent type
611
 
   * (including '\0').
612
 
   */
613
 
  static const int MAX_STRING_LENGTH= 27;
614
 
 
615
 
  /**
616
 
   * Attempts to populate the DateTime instance based
617
 
   * on the contents of a supplied string.
618
 
   *
619
 
   * Returns whether the conversion was
620
 
   * successful.
621
 
   *
622
 
   * @param String to convert from
623
 
   * @param Length of supplied string
624
 
   */
625
 
  bool from_string(const char *from, size_t from_len);
626
 
 
627
 
  /**
628
 
   * Fills a supplied 8-byte integer pointer with an
629
 
   * integer representation of the DateTime
630
 
   * value.
631
 
   *
632
 
   * @param Integer to fill.
633
 
   */
634
 
  void to_int64_t(int64_t *to) const;
635
 
 
636
 
  /**
637
 
   * Attempts to populate the DateTime instance based
638
 
   * on the contents of a supplied time_t
639
 
   *
640
 
   * Returns whether the conversion was
641
 
   * successful.
642
 
   *
643
 
   * @param time_t to convert from
644
 
   */
645
 
  bool from_time_t(const time_t from);
646
 
 
647
 
  /**
648
 
   * Attempts to populate the DateTime instance based
649
 
   * on the contents of a supplied 8-byte integer.
650
 
   *
651
 
   * Returns whether the conversion was
652
 
   * successful.
653
 
   *
654
 
   * @param Integer to convert from
655
 
   * @param convert if conversion to canonical representation
656
 
   *        should be attempted
657
 
   */
658
 
  bool from_int64_t(const int64_t from, bool convert);
659
 
 
660
 
  bool from_int64_t(const int64_t from) {
661
 
    return from_int64_t(from, true);
662
 
  }
663
 
 
664
 
  /**
665
 
   * Fills a supplied tm pointer with an
666
 
   * representation of the DateTime
667
 
   * value.
668
 
   *
669
 
   * @param tm to fill.
670
 
   */
671
 
  void to_tm(struct tm *to) const;
672
 
 
673
 
  /**
674
 
   * Fills a supplied my_decimal with a representation of
675
 
   * the DateTime value.
676
 
   *
677
 
   * @param Pointer to the my_decimal to fill
678
 
   */
679
 
  void to_decimal(my_decimal *to) const;
680
 
 
681
 
  friend class Timestamp;
682
 
};
683
 
 
684
 
/**
685
 
 * Class representing temporal components in the UNIX epoch
686
 
 */
687
 
class Timestamp: public DateTime
688
 
{
689
 
public:
690
 
  Timestamp() :DateTime() {}
691
 
 
692
 
  /**
693
 
   * Comparison operator overloads to compare this against
694
 
   * a Date value.
695
 
   *
696
 
   * @param Timestamp to compare against.
697
 
   */
698
 
  bool operator==(const Date &rhs);
699
 
  bool operator!=(const Date &rhs);
700
 
  bool operator>(const Date &rhs);
701
 
  bool operator>=(const Date &rhs);
702
 
  bool operator<(const Date &rhs);
703
 
  bool operator<=(const Date &rhs);
704
 
 
705
 
  /**
706
 
   * Comparison operator overloads to compare this against
707
 
   * a DateTime value.
708
 
   *
709
 
   * @param DateTime to compare against.
710
 
   */
711
 
  bool operator==(const DateTime &rhs);
712
 
  bool operator!=(const DateTime &rhs);
713
 
  bool operator>(const DateTime &rhs);
714
 
  bool operator>=(const DateTime &rhs);
715
 
  bool operator<(const DateTime &rhs);
716
 
  bool operator<=(const DateTime &rhs);
717
 
 
718
 
  /**
719
 
   * Comparison operator overloads to compare this against
720
 
   * another Timestamp value.
721
 
   *
722
 
   * @param Timestamp to compare against.
723
 
   */
724
 
  bool operator==(const Timestamp &rhs);
725
 
  bool operator!=(const Timestamp &rhs);
726
 
  bool operator>(const Timestamp &rhs);
727
 
  bool operator>=(const Timestamp &rhs);
728
 
  bool operator<(const Timestamp &rhs);
729
 
  bool operator<=(const Timestamp &rhs);
730
 
 
731
 
  bool is_valid_timestamp() const {return is_valid();}
732
 
  /** Returns whether the temporal value is valid timestamp. */
733
 
  virtual bool is_valid() const;
734
 
 
735
 
  /**
736
 
   * Attempts to convert the Timestamp value into
737
 
   * a supplied time_t.
738
 
   *
739
 
   * @param Pointer to a time_t to convert to
740
 
   */
741
 
  void to_time_t(time_t *to) const;
742
 
};
743
 
 
744
 
/**
745
 
 * Operator overload to an output stream for a Timestamp.
746
 
 */
747
 
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
748
 
 
749
 
/**
750
 
 * Class representing temporal components in the UNIX epoch
751
 
 * with an additional microsecond component.
752
 
 */
753
 
class MicroTimestamp: public Timestamp
754
 
{
755
 
public:
756
 
  MicroTimestamp() :Timestamp() {}
757
 
  /** Returns whether the temporal value is valid micro-timestamp. */
758
 
  bool is_valid() const;
759
 
 
760
 
  /**
761
 
   * Fills a supplied char string with a
762
 
   * string representation of the MicroTimestamp
763
 
   * value.
764
 
   *
765
 
   * @param C-String to fill
766
 
   * @param Length of to C-String
767
 
   * @returns length of string written (not including trailing '\0').
768
 
   *          If output was truncated, returns length that would have
769
 
   *          been outputted.
770
 
   */
771
 
  int to_string(char *to, size_t to_len) const;
772
 
 
773
 
  /**
774
 
   * Maximum length of C-String needed to represent type
775
 
   * (including '\0').
776
 
   */
777
 
  static const int MAX_STRING_LENGTH= 27;
778
 
 
779
 
  /**
780
 
   * Fills a supplied timeval pointer with an
781
 
   * representation of the MicroTimestamp
782
 
   * value.
783
 
   *
784
 
   * Returns whether the conversion was
785
 
   * successful.
786
 
   *
787
 
   * @param timeval to fill.
788
 
   */
789
 
  void to_timeval(struct timeval *to) const;
790
 
};
791
 
 
792
 
/**
793
 
 * Class representing temporal components in the UNIX epoch
794
 
 * with an additional nanosecond component.
795
 
 */
796
 
class NanoTimestamp: public Timestamp
797
 
{
798
 
public:
799
 
  NanoTimestamp() :Timestamp() {}
800
 
  /** Returns whether the temporal value is valid nano-timestamp. */
801
 
  bool is_valid() const;
802
 
 
803
 
  /**
804
 
   * Fills a supplied timespec pointer with an
805
 
   * representation of the NanoTimestamp
806
 
   * value.
807
 
   *
808
 
   * Returns whether the conversion was
809
 
   * successful.
810
 
   *
811
 
   * @param timespec to fill.
812
 
   */
813
 
  void to_timespec(struct timespec *to) const;
814
 
};
815
 
 
816
 
} /* end namespace drizzled */
817
 
 
818
 
#endif /* DRIZZLED_TEMPORAL_H */