~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.h

  • Committer: Brian Aker
  • Date: 2009-09-26 04:00:11 UTC
  • mfrom: (1126.12.1 trunk-nodebug)
  • Revision ID: brian@gaz-20090926040011-2qzxdcbpm1ibpkhl
Merge Lee

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