~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.h

  • Committer: Jay Pipes
  • Date: 2008-07-17 17:54:00 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717175400-xm2aazihjra8mdzq
Removal of DBUG from libdrizzle/ - Round 2

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, Inc.
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_SECONDS_WITH_LEAP 61
47
 
#define DRIZZLE_MAX_MINUTES 59
48
 
#define DRIZZLE_MAX_HOURS 23
49
 
#define DRIZZLE_MAX_DAYS 31
50
 
#define DRIZZLE_MAX_MONTHS 12
51
 
#define DRIZZLE_MAX_YEARS_SQL 9999
52
 
#define DRIZZLE_MAX_YEARS_EPOCH 2038
53
 
#define DRIZZLE_MIN_SECONDS 0
54
 
#define DRIZZLE_MIN_MINUTES 0
55
 
#define DRIZZLE_MIN_HOURS 0
56
 
#define DRIZZLE_MIN_DAYS 1
57
 
#define DRIZZLE_MIN_MONTHS 1
58
 
#define DRIZZLE_MIN_YEARS_SQL 1
59
 
#define DRIZZLE_MIN_YEARS_EPOCH 1970
60
 
 
61
 
#define DRIZZLE_SECONDS_IN_MINUTE 60
62
 
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
63
 
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
64
 
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
65
 
 
66
 
#define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
67
 
 
68
 
#define DRIZZLE_YY_PART_YEAR  70
69
 
 
70
 
#include <drizzled/calendar.h>
71
 
 
72
 
#include <cassert>
73
 
#include <ostream>
74
 
 
75
 
/* Outside forward declarations */
76
 
namespace type {
77
 
class Decimal;
78
 
}
79
 
 
80
 
namespace drizzled
81
 
{
82
 
 
83
 
/* Forward declaration needed */
84
 
class TemporalInterval;
85
 
class TemporalIntervalYear;
86
 
class TemporalIntervalDayOrLess;
87
 
class TemporalIntervalDayOrWeek;
88
 
class TemporalIntervalYearMonth;
89
 
 
90
 
/**
91
 
 * Base class for all temporal data classes.
92
 
 */
93
 
class Temporal
94
 
{
95
 
protected:
96
 
  enum calendar _calendar;
97
 
  uint32_t _years;
98
 
  uint32_t _months;
99
 
  uint32_t _days;
100
 
  uint32_t _hours;
101
 
  uint32_t _minutes;
102
 
  uint32_t _seconds;
103
 
  time_t _epoch_seconds;
104
 
  uint32_t _useconds;
105
 
  uint32_t _nseconds;
106
 
  /** Set on some operator overloads.  Indicates that an overflow occurred. */
107
 
  bool _overflow;
108
 
  /** Returns number of seconds in time components (hour + minute + second) */
109
 
  uint64_t _cumulative_seconds_in_time() const;
110
 
  /** Resets all temporal components to zero */
111
 
  inline void _reset()
112
 
  {
113
 
    _years= _months= _days= _hours= _minutes=
114
 
      _seconds= _epoch_seconds= _useconds= _nseconds= 0;
115
 
  }
116
 
 
117
 
public:
118
 
  Temporal();
119
 
  virtual ~Temporal() {}
120
 
 
121
 
  /** Returns the calendar component. */
122
 
  inline enum calendar calendar() const {return _calendar;}
123
 
  /** Sets the nseconds component. */
124
 
  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
125
 
  /** Returns the nanoseconds component. */
126
 
  inline uint32_t nseconds() const {return _nseconds;}
127
 
  /** Sets the useconds component. */
128
 
  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
129
 
  /** Returns the microsseconds component. */
130
 
  inline uint32_t useconds() const {return _useconds;}
131
 
  /**
132
 
   * Sets the epoch_seconds component automatically,
133
 
   * based on the temporal's components.
134
 
   */
135
 
  void set_epoch_seconds();
136
 
  /** Sets the epch_seconds component manually. */
137
 
  inline void set_epoch_seconds(const uint32_t epoch_second)
138
 
  {_epoch_seconds= epoch_second;}
139
 
  /** Returns the UNIX epoch seconds component. */
140
 
  inline time_t epoch_seconds() const {return _epoch_seconds;}
141
 
  /** Sets the seconds component. */
142
 
  inline void set_seconds(const uint32_t second) {_seconds= second;}
143
 
  /** Returns the seconds component. */
144
 
  inline uint32_t seconds() const {return _seconds;}
145
 
  /** Sets the days component. */
146
 
  inline void set_minutes(const uint32_t minute) {_minutes= minute;}
147
 
  /** Returns the minutes component. */
148
 
  inline uint32_t minutes() const {return _minutes;}
149
 
  /** Sets the hours component. */
150
 
  inline void set_hours(const uint32_t hour) {_hours= hour;}
151
 
  /** Returns the hours component. */
152
 
  inline uint32_t hours() const {return _hours;}
153
 
  /** Sets the days component. */
154
 
  inline void set_days(const uint32_t day) {_days= day;}
155
 
  /** Returns the days component. */
156
 
  inline uint32_t days() const {return _days;}
157
 
  /** Sets the months component. */
158
 
  inline void set_months(const uint32_t month) {_months= month;}
159
 
  /** Returns the months component. */
160
 
  inline uint32_t months() const {return _months;}
161
 
  /** Sets the years component. */
162
 
  inline void set_years(const uint32_t year) {_years= year;}
163
 
  /** Returns the years component. */
164
 
  inline uint32_t years() const {return _years;}
165
 
  /** Returns whether the overflow flag was set
166
 
   *  (which can occur during an overloaded operator's execution) */
167
 
  inline bool overflow() const {return _overflow;}
168
 
 
169
 
  /** Returns whether the temporal value is valid as a date. */
170
 
  virtual bool is_valid_date() const= 0;
171
 
  /** Returns whether the temporal value is valid as a datetime. */
172
 
  virtual bool is_valid_datetime() const= 0;
173
 
  /** Returns whether the temporal value is valid as a time. */
174
 
  virtual bool is_valid_time() const= 0;
175
 
  /** Returns whether the temporal value is valid as a UNIX timestamp. */
176
 
  virtual bool is_valid_timestamp() const= 0;
177
 
 
178
 
  /**
179
 
   * Returns whether the temporal
180
 
   * value is valid. Each subclass defines what is
181
 
   * valid for the range of temporal data it contains.
182
 
   */
183
 
  virtual bool is_valid() const= 0;
184
 
 
185
 
  /**
186
 
   * All Temporal derived classes must implement
187
 
   * conversion routines for converting to and from
188
 
   * a string. Subclasses implement other conversion
189
 
   * routines, but should always follow these notes:
190
 
   *
191
 
   * 1) Ensure that ALL from_xxx methods call is_valid()
192
 
   * 2) Ensure that ALL to_xxx methods are void returns and
193
 
   *    do not call is_valid()
194
 
   *
195
 
   * This minimizes the repeated bounds-checking to
196
 
   * just the conversion from_xxx routines.
197
 
   */
198
 
  friend class TemporalFormat;
199
 
};
200
 
 
201
 
/* Forward declaration needed */
202
 
class DateTime;
203
 
class Timestamp;
204
 
class Time;
205
 
 
206
 
/**
207
 
 * Class representing temporal components in a valid
208
 
 * SQL date range, with no time component
209
 
 */
210
 
class Date: public Temporal
211
 
{
212
 
public:
213
 
  Date() :Temporal() {}
214
 
  /**
215
 
   * Comparison operator overloads to compare a Date against
216
 
   * another Date value.
217
 
   *
218
 
   * @param Date to compare against.
219
 
   */
220
 
  virtual bool operator==(const Date &rhs);
221
 
  virtual bool operator!=(const Date &rhs);
222
 
  virtual bool operator>(const Date &rhs);
223
 
  virtual bool operator>=(const Date &rhs);
224
 
  virtual bool operator<(const Date &rhs);
225
 
  virtual bool operator<=(const Date &rhs);
226
 
 
227
 
  /**
228
 
   * Comparison operator overloads to compare a Date against
229
 
   * a DateTime value.
230
 
   *
231
 
   * @param DateTime to compare against.
232
 
   */
233
 
  virtual bool operator==(const DateTime &rhs);
234
 
  virtual bool operator!=(const DateTime &rhs);
235
 
  virtual bool operator>(const DateTime &rhs);
236
 
  virtual bool operator>=(const DateTime &rhs);
237
 
  virtual bool operator<(const DateTime &rhs);
238
 
  virtual bool operator<=(const DateTime &rhs);
239
 
 
240
 
  /**
241
 
   * Comparison operator overloads to compare this against
242
 
   * a Timestamp value.
243
 
   *
244
 
   * @param Timestamp to compare against.
245
 
   */
246
 
  virtual bool operator==(const Timestamp &rhs);
247
 
  virtual bool operator!=(const Timestamp &rhs);
248
 
  virtual bool operator>(const Timestamp &rhs);
249
 
  virtual bool operator>=(const Timestamp &rhs);
250
 
  virtual bool operator<(const Timestamp &rhs);
251
 
  virtual bool operator<=(const Timestamp &rhs);
252
 
 
253
 
  /**
254
 
   * Operator overload for adding/subtracting another Date
255
 
   * (or subclass) to/from this temporal.  When subtracting
256
 
   * or adding two Dates, we return a new Date instance.
257
 
   *
258
 
   * @param Date instance to add/subtract to/from
259
 
   */
260
 
  const Date operator-(const Date &rhs);
261
 
  const Date operator+(const Date &rhs);
262
 
  Date& operator+=(const Date &rhs);
263
 
  Date& operator-=(const Date &rhs);
264
 
 
265
 
  /**
266
 
   * Operator to add/subtract a Time from a Time.
267
 
   * We can return a Time new temporal instance.
268
 
   *
269
 
   * @param Temporal instance to add/subtract to/from
270
 
   */
271
 
  const Date operator-(const Time &rhs);
272
 
  const Date operator+(const Time &rhs);
273
 
  Date& operator-=(const Time &rhs);
274
 
  Date& operator+=(const Time &rhs);
275
 
 
276
 
 
277
 
  /**
278
 
   * Operator overload for adding/subtracting a DateTime
279
 
   * (or subclass) to/from this temporal.  When subtracting
280
 
   * or adding two Dates, we return a new Date instance.
281
 
   *
282
 
   * @param DateTime instance to add/subtract to/from
283
 
   */
284
 
  const Date operator-(const DateTime &rhs);
285
 
  const Date operator+(const DateTime &rhs);
286
 
  Date& operator+=(const DateTime &rhs);
287
 
  Date& operator-=(const DateTime &rhs);
288
 
 
289
 
 
290
 
  /**
291
 
   * Operator overload for when a DateTime instance is
292
 
   * assigned to a Date.  We do a copy of the DateTime's
293
 
   * date-related components.
294
 
   *
295
 
   * @param The DateTime to copy from
296
 
   */
297
 
  Date& operator=(const DateTime &rhs);
298
 
 
299
 
  virtual bool is_valid_date() const {return is_valid();}
300
 
  virtual bool is_valid_datetime() const {return is_valid();}
301
 
  virtual bool is_valid_time() const {return false;}
302
 
  virtual bool is_valid_timestamp() const
303
 
  {
304
 
    return is_valid() && in_unix_epoch();
305
 
  }
306
 
 
307
 
  /** Returns whether the temporal value is valid date. */
308
 
  virtual bool is_valid() const;
309
 
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
310
 
  virtual bool in_unix_epoch() const;
311
 
 
312
 
  /**
313
 
   * Fills a supplied char string with a
314
 
   * string representation of the Date
315
 
   * value.
316
 
   *
317
 
   * @param C-String to fill.
318
 
   * @param Length of to C-String
319
 
   * @returns length of string written (including trailing '\0').
320
 
   *          If output was truncated, returns length that would have
321
 
   *          been outputted.
322
 
   */
323
 
  virtual int to_string(char *to, size_t to_len) const;
324
 
 
325
 
  /**
326
 
   * Maximum length of C-String needed to represent type
327
 
   * (including '\0').
328
 
   */
329
 
  static const int MAX_STRING_LENGTH= 11;
330
 
 
331
 
  /**
332
 
   * Attempts to populate the Date instance based
333
 
   * on the contents of a supplied string.
334
 
   *
335
 
   * Returns whether the conversion was
336
 
   * successful.
337
 
   *
338
 
   * @param String to convert from
339
 
   * @param Length of supplied string (not including trailing '\0').
340
 
   */
341
 
  virtual bool from_string(const char *from, size_t from_len);
342
 
 
343
 
  /**
344
 
   * Fills a supplied 8-byte integer pointer with an
345
 
   * integer representation of the Date
346
 
   * value.
347
 
   *
348
 
   * @param Integer to fill.
349
 
   */
350
 
  virtual void to_int64_t(int64_t *to) const;
351
 
 
352
 
  /**
353
 
   * Fills a supplied 4-byte integer pointer with an
354
 
   * integer representation of the Date
355
 
   * value.
356
 
   *
357
 
   * @param Integer to fill.
358
 
   */
359
 
  virtual void to_int32_t(int32_t *to) const;
360
 
 
361
 
  /**
362
 
   * Attempts to populate the Date instance based
363
 
   * on the contents of a supplied 4-byte integer.
364
 
   *
365
 
   * Returns whether the conversion was
366
 
   * successful.
367
 
   *
368
 
   * @param Integer to convert from
369
 
   */
370
 
  virtual bool from_int32_t(const int32_t from);
371
 
 
372
 
  /**
373
 
   * Fills a supplied int64_t with the Julian Day Number
374
 
   * representation of this Date.
375
 
   *
376
 
   * @note Julian Day Number != julian day!
377
 
   *
378
 
   * Julian Day Number is the monotonically increasing number
379
 
   * of days from the start of the Julian calendar (~4713 B.C.)
380
 
   *
381
 
   * julian day is the ordinal day number of a day in a year.
382
 
   *
383
 
   * @param int64_t to fill
384
 
   */
385
 
  void to_julian_day_number(int64_t *to) const;
386
 
 
387
 
  /**
388
 
   * Attempts to populate the Date instance based
389
 
   * on the contents of a supplied Julian Day Number
390
 
   *
391
 
   * Returns whether the conversion was
392
 
   * successful.
393
 
   *
394
 
   * @param Integer to convert from
395
 
   */
396
 
  bool from_julian_day_number(const int64_t from);
397
 
 
398
 
  /**
399
 
   * Fills a supplied tm pointer with an
400
 
   * representation of the Date
401
 
   * value.
402
 
   *
403
 
   * @param tm to fill.
404
 
   */
405
 
  virtual void to_tm(struct tm *to) const;
406
 
 
407
 
  /**
408
 
   * Attempts to populate the Date instance based
409
 
   * on the contents of a supplied pointer to struct tm
410
 
   * (broken time).
411
 
   *
412
 
   * Returns whether the conversion was
413
 
   * successful.
414
 
   *
415
 
   * @param Pointe rto the struct tm to convert from
416
 
   */
417
 
  virtual bool from_tm(const struct tm *from);
418
 
 
419
 
  /**
420
 
   * Attempts to convert the Date value into
421
 
   * a supplied time_t.
422
 
   *
423
 
   * @param Pointer to a time_t to convert to
424
 
   */
425
 
  virtual void to_time_t(time_t &to) const;
426
 
 
427
 
  /**
428
 
   * Attempts to populate the Date instance based
429
 
   * on the contents of a supplied time_t
430
 
   *
431
 
   * Returns whether the conversion was
432
 
   * successful.
433
 
   *
434
 
   * @param time_t to convert from
435
 
   */
436
 
  virtual bool from_time_t(const time_t from);
437
 
 
438
 
  /**
439
 
   * Fills a supplied type::Decimal with a representation of
440
 
   * the Date value.
441
 
   *
442
 
   * @param Pointer to the type::Decimal to fill
443
 
   */
444
 
  virtual void to_decimal(type::Decimal *to) const;
445
 
 
446
 
  friend class TemporalInterval;
447
 
  friend class Timestamp;
448
 
};
449
 
 
450
 
/* Forward declare needed for friendship */
451
 
class DateTime;
452
 
 
453
 
/**
454
 
 * Class representing temporal components having only
455
 
 * a time component, with no date structure
456
 
 */
457
 
class Time: public Temporal
458
 
{
459
 
public:
460
 
  Time() :Temporal() {}
461
 
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
462
 
  static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
463
 
 
464
 
  /**
465
 
   * Comparison operator overloads to compare a Time against
466
 
   * another Time value.
467
 
   *
468
 
   * @param Time to compare against.
469
 
   */
470
 
  bool operator==(const Time &rhs);
471
 
  bool operator!=(const Time &rhs);
472
 
  bool operator>(const Time &rhs);
473
 
  bool operator>=(const Time &rhs);
474
 
  bool operator<(const Time &rhs);
475
 
  bool operator<=(const Time &rhs);
476
 
  /**
477
 
   * Operator to add/subtract a Time from a Time.
478
 
   * We can return a Time new temporal instance.
479
 
   *
480
 
   * @param Temporal instance to add/subtract to/from
481
 
   */
482
 
  const Time operator-(const Time &rhs);
483
 
  const Time operator+(const Time &rhs);
484
 
  Time& operator-=(const Time &rhs);
485
 
  Time& operator+=(const Time &rhs);
486
 
 
487
 
  bool is_valid_date() const {return false;}
488
 
  bool is_valid_datetime() const {return false;}
489
 
  bool is_valid_time() const {return is_valid();}
490
 
  bool is_valid_timestamp() const {return false;}
491
 
 
492
 
  /** Returns whether the temporal value is valid date. */
493
 
  bool is_valid() const;
494
 
  bool is_fuzzy_valid() const;
495
 
 
496
 
  /**
497
 
   * Fills a supplied char string with a
498
 
   * string representation of the Time
499
 
   * value.
500
 
   *
501
 
   * @param C-String to fill
502
 
   * @param Length of to C-String
503
 
   * @returns length of string written (not including trailing '\0').
504
 
   *          If output was truncated, returns length that would have
505
 
   *          been outputted.
506
 
   */
507
 
  int to_string(char *to, size_t to_len) const;
508
 
 
509
 
  /**
510
 
   * Maximum length of C-String needed to represent type
511
 
   * (including '\0').
512
 
   */
513
 
  static const int MAX_STRING_LENGTH= 9;
514
 
 
515
 
 
516
 
  /**
517
 
   * Attempts to populate the Time instance based
518
 
   * on the contents of a supplied string.
519
 
   *
520
 
   * Returns whether the conversion was
521
 
   * successful.
522
 
   *
523
 
   * @param String to convert from
524
 
   * @param Length of supplied string
525
 
   */
526
 
  bool from_string(const char *from, size_t from_len);
527
 
 
528
 
  /**
529
 
   * Fills a supplied 4-byte integer pointer with an
530
 
   * integer representation of the Time
531
 
   * value.
532
 
   *
533
 
   * @param Integer to fill.
534
 
   */
535
 
  void to_int32_t(int32_t *to) const;
536
 
 
537
 
  /**
538
 
   * Fills a supplied 8-byte integer pointer with an
539
 
   * integer representation of the Time
540
 
   * value. It is assume seconds past unix epoch
541
 
   *
542
 
   * @param Integer to fill.
543
 
   */
544
 
  void to_uint64_t(uint64_t &to) const;
545
 
 
546
 
  /**
547
 
   * Attempts to populate the Time instance based
548
 
   * on the contents of a supplied 4-byte integer.
549
 
   *
550
 
   * Returns whether the conversion was
551
 
   * successful.
552
 
   *
553
 
   * @param Integer to convert from
554
 
   */
555
 
  bool from_int32_t(const int32_t from);
556
 
 
557
 
  /**
558
 
   * Attempts to populate the Time instance based
559
 
   * on the contents of a supplied time_t
560
 
   *
561
 
   * Returns whether the conversion was
562
 
   * successful.
563
 
   *
564
 
   * @note
565
 
   *
566
 
   * We can only convert *from* a time_t, not back
567
 
   * to a time_t since it would be a lossy conversion.
568
 
   *
569
 
   * @param time_t to convert from
570
 
   */
571
 
  bool from_time_t(const time_t from);
572
 
 
573
 
  /**
574
 
   * Fills a supplied type::Decimal with a representation of
575
 
   * the Time value.
576
 
   *
577
 
   * @param Pointer to the type::Decimal to fill
578
 
   */
579
 
  void to_decimal(type::Decimal *to) const;
580
 
 
581
 
  friend class Date;
582
 
  friend class DateTime;
583
 
};
584
 
 
585
 
/**
586
 
 * Class representing temporal components in a valid
587
 
 * SQL datetime range, including a time component
588
 
 */
589
 
class DateTime: public Date
590
 
{
591
 
public:
592
 
  DateTime() :Date() {}
593
 
 
594
 
  friend class TemporalInterval;
595
 
 
596
 
  /** Returns whether the DateTime (or subclass) instance
597
 
   *  is in the Unix Epoch.
598
 
   */
599
 
  bool in_unix_epoch() const;
600
 
  /** Returns whether the temporal value is valid datetime. */
601
 
  virtual bool is_valid() const;
602
 
 
603
 
  /**
604
 
   * It's not possible to convert to and from a DateTime and
605
 
   * a 4-byte integer, so let us know if we try and do it!
606
 
   */
607
 
  void to_int32_t(int32_t *) const {assert(0);}
608
 
  bool from_int32_t(int32_t) {assert(0); return false;}
609
 
 
610
 
  /**
611
 
   * Fills a supplied char string with a
612
 
   * string representation of the DateTime
613
 
   * value.
614
 
   *
615
 
   * @param C-String to fill
616
 
   * @param Length of to C-String
617
 
   * @returns length of string written (not including trailing '\0').
618
 
   *          If output was truncated, returns length that would have
619
 
   *          been outputted.
620
 
   */
621
 
  virtual int to_string(char *to, size_t to_len) const;
622
 
 
623
 
  /**
624
 
   * Maximum length of C-String needed to represent type
625
 
   * (including '\0').
626
 
   */
627
 
  static const int MAX_STRING_LENGTH= 27;
628
 
 
629
 
  /**
630
 
   * Attempts to populate the DateTime instance based
631
 
   * on the contents of a supplied string.
632
 
   *
633
 
   * Returns whether the conversion was
634
 
   * successful.
635
 
   *
636
 
   * @param String to convert from
637
 
   * @param Length of supplied string
638
 
   */
639
 
  bool from_string(const char *from, size_t from_len);
640
 
 
641
 
  /**
642
 
   * Fills a supplied 8-byte integer pointer with an
643
 
   * integer representation of the DateTime
644
 
   * value.
645
 
   *
646
 
   * @param Integer to fill.
647
 
   */
648
 
  void to_int64_t(int64_t *to) const;
649
 
 
650
 
  /**
651
 
   * Attempts to populate the DateTime instance based
652
 
   * on the contents of a supplied time_t
653
 
   *
654
 
   * Returns whether the conversion was
655
 
   * successful.
656
 
   *
657
 
   * @param time_t to convert from
658
 
   */
659
 
  bool from_time_t(const time_t from);
660
 
  bool from_timeval(struct timeval &_timeval);
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 type::Decimal with a representation of
690
 
   * the DateTime value.
691
 
   *
692
 
   * @param Pointer to the type::Decimal to fill
693
 
   */
694
 
  void to_decimal(type::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 */