~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.h

  • Committer: Monty Taylor
  • Date: 2009-02-11 04:57:40 UTC
  • mfrom: (873.1.5 temporal-new)
  • mto: This revision was merged to the branch mainline in revision 879.
  • Revision ID: mordred@inaugust.com-20090211045740-wmv6ucfj7tjr3ngg
Merged from Jay.

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
namespace drizzled 
79
79
{
80
80
 
 
81
/* Forward declaration needed */
 
82
class TemporalInterval;
 
83
class TemporalIntervalYear;
 
84
class TemporalIntervalDayOrLess;
 
85
class TemporalIntervalDayOrWeek;
 
86
class TemporalIntervalYearMonth;
 
87
 
81
88
/**
82
89
 * Base class for all temporal data classes.
83
90
 */
94
101
  time_t _epoch_seconds;
95
102
  uint32_t _useconds;
96
103
  uint32_t _nseconds;
 
104
  /** Set on some operator overloads.  Indicates that an overflow occurred. */
 
105
  bool _overflow;
97
106
  /** Returns number of seconds in time components (hour + minute + second) */
98
107
  uint64_t _cumulative_seconds_in_time() const;
 
108
  /** Resets all temporal components to zero */
 
109
  inline void _reset() {_years= _months= _days= _hours= _minutes= _seconds= _epoch_seconds= _useconds= _nseconds= 0;}
99
110
public:
100
111
  Temporal();
101
112
  virtual ~Temporal() {}
102
113
 
103
114
  /** Returns the calendar component. */
104
115
  inline enum calendar calendar() const {return _calendar;}
 
116
  /** Sets the nseconds component. */
 
117
  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
105
118
  /** Returns the nanoseconds component. */
106
119
  inline uint32_t nseconds() const {return _nseconds;}
107
120
  /** Sets the useconds component. */
108
121
  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
109
122
  /** Returns the microsseconds component. */
110
123
  inline uint32_t useconds() const {return _useconds;}
111
 
  /** Sets the epoch_seconds component. */
 
124
  /** 
 
125
   * Sets the epoch_seconds component automatically, 
 
126
   * based on the temporal's components. 
 
127
   */
112
128
  virtual void set_epoch_seconds();
 
129
  /** Sets the epch_seconds component manually. */
 
130
  inline void set_epoch_seconds(const uint32_t epoch_second) {_epoch_seconds= epoch_second;}
113
131
  /** Returns the UNIX epoch seconds component. */
114
132
  inline time_t epoch_seconds() const {return _epoch_seconds;}
115
133
  /** Sets the seconds component. */
136
154
  inline void set_years(const uint32_t year) {_years= year;}
137
155
  /** Returns the years component. */
138
156
  inline uint32_t years() const {return _years;}
 
157
  /** Returns whether the overflow flag was set (which can occur during an overloaded operator's execution) */
 
158
  inline bool overflow() const {return _overflow;}
139
159
 
140
160
  /** Returns whether the temporal value is valid as a date. */
141
161
  virtual bool is_valid_date() const= 0;
166
186
   * This minimizes the repeated bounds-checking to
167
187
   * just the conversion from_xxx routines.
168
188
   */
169
 
 
170
189
  friend class TemporalFormat;
171
190
};
172
191
 
 
192
/* Forward declaration needed */
 
193
class DateTime;
 
194
 
173
195
/**
174
196
 * Class representing temporal components in a valid
175
197
 * SQL date range, with no time component
183
205
   *
184
206
   * @param Date to compare against.
185
207
   */
186
 
  bool operator==(const Date &rhs);
187
 
  bool operator!=(const Date &rhs);
188
 
  bool operator>(const Date &rhs);
189
 
  bool operator>=(const Date &rhs);
190
 
  bool operator<(const Date &rhs);
191
 
  bool operator<=(const Date &rhs);
 
208
  virtual bool operator==(const Date &rhs);
 
209
  virtual bool operator!=(const Date &rhs);
 
210
  virtual bool operator>(const Date &rhs);
 
211
  virtual bool operator>=(const Date &rhs);
 
212
  virtual bool operator<(const Date &rhs);
 
213
  virtual bool operator<=(const Date &rhs);
192
214
  /**
193
215
   * Operator overload for adding/subtracting another Date
194
216
   * (or subclass) to/from this temporal.  When subtracting 
201
223
  Date& operator+=(const Date &rhs);
202
224
  Date& operator-=(const Date &rhs);
203
225
 
 
226
  /**
 
227
   * Operator overload for when a DateTime instance is
 
228
   * assigned to a Date.  We do a copy of the DateTime's
 
229
   * date-related components.
 
230
   *
 
231
   * @param The DateTime to copy from
 
232
   */
 
233
  Date& operator=(const DateTime &rhs);
 
234
 
204
235
  virtual bool is_valid_date() const {return is_valid();}
205
236
  virtual bool is_valid_datetime() const {return is_valid();}
206
237
  virtual bool is_valid_time() const {return false;}
262
293
  virtual bool from_int32_t(const int32_t from);
263
294
 
264
295
  /**
 
296
   * Fills a supplied int64_t with the Julian Day Number
 
297
   * representation of this Date.
 
298
   *
 
299
   * @note Julian Day Number != julian day!
 
300
   *
 
301
   * Julian Day Number is the monotonically increasing number
 
302
   * of days from the start of the Julian calendar (~4713 B.C.)
 
303
   *
 
304
   * julian day is the ordinal day number of a day in a year.
 
305
   *
 
306
   * @param int64_t to fill
 
307
   */
 
308
  void to_julian_day_number(int64_t *to) const;
 
309
 
 
310
  /**
265
311
   * Attempts to populate the Date instance based
266
312
   * on the contents of a supplied Julian Day Number
267
313
   *
270
316
   *
271
317
   * @param Integer to convert from
272
318
   */
273
 
  virtual bool from_julian_day_number(const int64_t from);
 
319
  bool from_julian_day_number(const int64_t from);
274
320
 
275
321
  /**
276
322
   * Fills a supplied tm pointer with an
319
365
   * @param Pointer to the my_decimal to fill
320
366
   */
321
367
  virtual void to_decimal(my_decimal *to) const;
 
368
 
 
369
  friend class TemporalInterval;
322
370
};
323
371
 
324
372
/* Forward declare needed for friendship */
332
380
{
333
381
public:
334
382
 
 
383
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
 
384
  const static uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
 
385
 
335
386
  /**
336
387
   * Comparison operator overloads to compare a Time against
337
388
   * another Time value.
355
406
  Time& operator-=(const Time &rhs);
356
407
  Time& operator+=(const Time &rhs);
357
408
 
358
 
  virtual bool is_valid_date() const {return false;}
359
 
  virtual bool is_valid_datetime() const {return false;}
360
 
  virtual bool is_valid_time() const {return is_valid();}
361
 
  virtual bool is_valid_timestamp() const {return false;}
 
409
  bool is_valid_date() const {return false;}
 
410
  bool is_valid_datetime() const {return false;}
 
411
  bool is_valid_time() const {return is_valid();}
 
412
  bool is_valid_timestamp() const {return false;}
362
413
  /** Returns whether the temporal value is valid date. */
363
 
  virtual bool is_valid() const;
 
414
  bool is_valid() const;
364
415
 
365
416
  /**
366
417
   * Fills a supplied char string with a
370
421
   * @param C-String to fill.
371
422
   * @param Length of filled string (out param)
372
423
   */
373
 
  virtual void to_string(char *to, size_t *to_len) const;
 
424
  void to_string(char *to, size_t *to_len) const;
374
425
 
375
426
  /**
376
427
   * Attempts to populate the Time instance based
382
433
   * @param String to convert from
383
434
   * @param Length of supplied string
384
435
   */
385
 
  virtual bool from_string(const char *from, size_t from_len);
 
436
  bool from_string(const char *from, size_t from_len);
386
437
 
387
438
  /**
388
439
   * Fills a supplied 4-byte integer pointer with an
391
442
   *
392
443
   * @param Integer to fill.
393
444
   */
394
 
  virtual void to_int32_t(int32_t *to) const;
 
445
  void to_int32_t(int32_t *to) const;
395
446
 
396
447
  /**
397
448
   * Attempts to populate the Time instance based
402
453
   *
403
454
   * @param Integer to convert from
404
455
   */
405
 
  virtual bool from_int32_t(const int32_t from);
 
456
  bool from_int32_t(const int32_t from);
 
457
 
 
458
  /**
 
459
   * Attempts to populate the Time instance based
 
460
   * on the contents of a supplied time_t
 
461
   *
 
462
   * Returns whether the conversion was 
 
463
   * successful.
 
464
   *
 
465
   * @note
 
466
   *
 
467
   * We can only convert *from* a time_t, not back 
 
468
   * to a time_t since it would be a lossy conversion.
 
469
   *
 
470
   * @param time_t to convert from
 
471
   */
 
472
  bool from_time_t(const time_t from);
406
473
 
407
474
  /**
408
475
   * Fills a supplied my_decimal with a representation of 
410
477
   *
411
478
   * @param Pointer to the my_decimal to fill
412
479
   */
413
 
  virtual void to_decimal(my_decimal *to) const;
 
480
  void to_decimal(my_decimal *to) const;
414
481
 
415
482
  friend class DateTime;
416
483
};
456
523
  DateTime& operator+=(const DateTime &rhs);
457
524
  DateTime& operator-=(const DateTime &rhs);
458
525
 
 
526
  /**
 
527
   * Operator overload for adding/subtracting a TemporalInterval
 
528
   * instance to this temporal.
 
529
   *
 
530
   * @param TemporalInterval instance to add/subtract to/from
 
531
   */
 
532
  DateTime& operator+=(const TemporalIntervalYear &rhs);
 
533
  DateTime& operator+=(const TemporalIntervalDayOrLess &rhs);
 
534
  DateTime& operator+=(const TemporalIntervalDayOrWeek &rhs);
 
535
  DateTime& operator+=(const TemporalIntervalYearMonth &rhs);
 
536
  DateTime& operator-=(const TemporalIntervalYear &rhs);
 
537
  DateTime& operator-=(const TemporalIntervalDayOrLess &rhs);
 
538
  DateTime& operator-=(const TemporalIntervalDayOrWeek &rhs);
 
539
  DateTime& operator-=(const TemporalIntervalYearMonth &rhs);
 
540
 
 
541
  friend class TemporalInterval;
 
542
 
459
543
  /* Returns whether the DateTime (or subclass) instance is in the Unix Epoch. */
460
544
  bool in_unix_epoch() const;
461
545
  /** Returns whether the temporal value is valid datetime. */