~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/temporal.h

Merged fix-warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
/**
26
 
 * @file 
 
26
 * @file
27
27
 *
28
28
 * Defines the API for dealing with temporal data inside the server.
29
29
 *
30
30
 * The Temporal class is the base class for all data of any temporal
31
 
 * type.  A number of derived classes define specialized classes 
 
31
 * type.  A number of derived classes define specialized classes
32
32
 * representng various date, date-time, time, or timestamp types.
33
33
 *
34
34
 * All Temporal derived classes are ValueObjects.  That is to say that
75
75
/* Outside forward declarations */
76
76
class my_decimal;
77
77
 
78
 
namespace drizzled 
 
78
namespace drizzled
79
79
{
80
80
 
81
81
/* Forward declaration needed */
106
106
  /** Returns number of seconds in time components (hour + minute + second) */
107
107
  uint64_t _cumulative_seconds_in_time() const;
108
108
  /** Resets all temporal components to zero */
109
 
  inline void _reset() {_years= _months= _days= _hours= _minutes= _seconds= _epoch_seconds= _useconds= _nseconds= 0;}
 
109
  inline void _reset()
 
110
  {
 
111
    _years= _months= _days= _hours= _minutes=
 
112
      _seconds= _epoch_seconds= _useconds= _nseconds= 0;
 
113
  }
 
114
 
110
115
public:
111
116
  Temporal();
112
117
  virtual ~Temporal() {}
121
126
  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
122
127
  /** Returns the microsseconds component. */
123
128
  inline uint32_t useconds() const {return _useconds;}
124
 
  /** 
125
 
   * Sets the epoch_seconds component automatically, 
126
 
   * based on the temporal's components. 
 
129
  /**
 
130
   * Sets the epoch_seconds component automatically,
 
131
   * based on the temporal's components.
127
132
   */
128
133
  void set_epoch_seconds();
129
134
  /** Sets the epch_seconds component manually. */
130
 
  inline void set_epoch_seconds(const uint32_t epoch_second) {_epoch_seconds= epoch_second;}
 
135
  inline void set_epoch_seconds(const uint32_t epoch_second)
 
136
  {_epoch_seconds= epoch_second;}
131
137
  /** Returns the UNIX epoch seconds component. */
132
138
  inline time_t epoch_seconds() const {return _epoch_seconds;}
133
139
  /** Sets the seconds component. */
154
160
  inline void set_years(const uint32_t year) {_years= year;}
155
161
  /** Returns the years component. */
156
162
  inline uint32_t years() const {return _years;}
157
 
  /** Returns whether the overflow flag was set (which can occur during an overloaded operator's execution) */
 
163
  /** Returns whether the overflow flag was set
 
164
   *  (which can occur during an overloaded operator's execution) */
158
165
  inline bool overflow() const {return _overflow;}
159
166
 
160
167
  /** Returns whether the temporal value is valid as a date. */
176
183
  /**
177
184
   * All Temporal derived classes must implement
178
185
   * conversion routines for converting to and from
179
 
   * a string. Subclasses implement other conversion 
 
186
   * a string. Subclasses implement other conversion
180
187
   * routines, but should always follow these notes:
181
188
   *
182
189
   * 1) Ensure that ALL from_xxx methods call is_valid()
191
198
 
192
199
/* Forward declaration needed */
193
200
class DateTime;
 
201
class Timestamp;
 
202
class Time;
194
203
 
195
204
/**
196
205
 * Class representing temporal components in a valid
197
206
 * SQL date range, with no time component
198
207
 */
199
 
class Date: public Temporal 
 
208
class Date: public Temporal
200
209
{
201
210
public:
202
211
  Date() :Temporal() {}
212
221
  bool operator>=(const Date &rhs);
213
222
  bool operator<(const Date &rhs);
214
223
  bool operator<=(const Date &rhs);
 
224
 
 
225
  /**
 
226
   * Comparison operator overloads to compare a Date against
 
227
   * a DateTime value.
 
228
   *
 
229
   * @param DateTime to compare against.
 
230
   */
 
231
  bool operator==(const DateTime &rhs);
 
232
  bool operator!=(const DateTime &rhs);
 
233
  bool operator>(const DateTime &rhs);
 
234
  bool operator>=(const DateTime &rhs);
 
235
  bool operator<(const DateTime &rhs);
 
236
  bool operator<=(const DateTime &rhs);
 
237
 
 
238
  /**
 
239
   * Comparison operator overloads to compare a Date against
 
240
   * a Timestamp value.
 
241
   *
 
242
   * @param Timestamp to compare against.
 
243
   */
 
244
  bool operator==(const Timestamp &rhs);
 
245
  bool operator!=(const Timestamp &rhs);
 
246
  bool operator>(const Timestamp &rhs);
 
247
  bool operator>=(const Timestamp &rhs);
 
248
  bool operator<(const Timestamp &rhs);
 
249
  bool operator<=(const Timestamp &rhs);
 
250
 
215
251
  /**
216
252
   * Operator overload for adding/subtracting another Date
217
 
   * (or subclass) to/from this temporal.  When subtracting 
 
253
   * (or subclass) to/from this temporal.  When subtracting
218
254
   * or adding two Dates, we return a new Date instance.
219
255
   *
220
 
   * @param Temporal instance to add/subtract to/from
 
256
   * @param Date instance to add/subtract to/from
221
257
   */
222
258
  const Date operator-(const Date &rhs);
223
259
  const Date operator+(const Date &rhs);
225
261
  Date& operator-=(const Date &rhs);
226
262
 
227
263
  /**
 
264
   * Operator to add/subtract a Time from a Time.
 
265
   * We can return a Time new temporal instance.
 
266
   *
 
267
   * @param Temporal instance to add/subtract to/from
 
268
   */
 
269
  const Date operator-(const Time &rhs);
 
270
  const Date operator+(const Time &rhs);
 
271
  Date& operator-=(const Time &rhs);
 
272
  Date& operator+=(const Time &rhs);
 
273
 
 
274
 
 
275
  /**
 
276
   * Operator overload for adding/subtracting a DateTime
 
277
   * (or subclass) to/from this temporal.  When subtracting
 
278
   * or adding two Dates, we return a new Date instance.
 
279
   *
 
280
   * @param DateTime instance to add/subtract to/from
 
281
   */
 
282
  const Date operator-(const DateTime &rhs);
 
283
  const Date operator+(const DateTime &rhs);
 
284
  Date& operator+=(const DateTime &rhs);
 
285
  Date& operator-=(const DateTime &rhs);
 
286
 
 
287
 
 
288
  /**
 
289
   * Operator overload for adding/subtracting a TemporalInterval
 
290
   * instance to this temporal.
 
291
   *
 
292
   * @param TemporalInterval instance to add/subtract to/from
 
293
   */
 
294
  Date& operator+=(const TemporalIntervalYear &rhs);
 
295
  Date& operator+=(const TemporalIntervalDayOrLess &rhs);
 
296
  Date& operator+=(const TemporalIntervalDayOrWeek &rhs);
 
297
  Date& operator+=(const TemporalIntervalYearMonth &rhs);
 
298
  Date& operator-=(const TemporalIntervalYear &rhs);
 
299
  Date& operator-=(const TemporalIntervalDayOrLess &rhs);
 
300
  Date& operator-=(const TemporalIntervalDayOrWeek &rhs);
 
301
  Date& operator-=(const TemporalIntervalYearMonth &rhs);
 
302
 
 
303
 
 
304
  /**
228
305
   * Operator overload for when a DateTime instance is
229
306
   * assigned to a Date.  We do a copy of the DateTime's
230
307
   * date-related components.
236
313
  virtual bool is_valid_date() const {return is_valid();}
237
314
  virtual bool is_valid_datetime() const {return is_valid();}
238
315
  virtual bool is_valid_time() const {return false;}
239
 
  virtual bool is_valid_timestamp() const {return is_valid() && in_unix_epoch();}
 
316
  virtual bool is_valid_timestamp() const
 
317
  {
 
318
    return is_valid() && in_unix_epoch();
 
319
  }
 
320
 
240
321
  /** Returns whether the temporal value is valid date. */
241
322
  virtual bool is_valid() const;
242
323
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
244
325
 
245
326
  /**
246
327
   * Fills a supplied char string with a
247
 
   * string representation of the Date  
 
328
   * string representation of the Date
248
329
   * value.
249
330
   *
250
331
   * @param C-String to fill.
256
337
   * Attempts to populate the Date instance based
257
338
   * on the contents of a supplied string.
258
339
   *
259
 
   * Returns whether the conversion was 
 
340
   * Returns whether the conversion was
260
341
   * successful.
261
342
   *
262
343
   * @param String to convert from
286
367
   * Attempts to populate the Date instance based
287
368
   * on the contents of a supplied 4-byte integer.
288
369
   *
289
 
   * Returns whether the conversion was 
 
370
   * Returns whether the conversion was
290
371
   * successful.
291
372
   *
292
373
   * @param Integer to convert from
312
393
   * Attempts to populate the Date instance based
313
394
   * on the contents of a supplied Julian Day Number
314
395
   *
315
 
   * Returns whether the conversion was 
 
396
   * Returns whether the conversion was
316
397
   * successful.
317
398
   *
318
399
   * @param Integer to convert from
321
402
 
322
403
  /**
323
404
   * Fills a supplied tm pointer with an
324
 
   * representation of the Date 
 
405
   * representation of the Date
325
406
   * value.
326
407
   *
327
408
   * @param tm to fill.
333
414
   * on the contents of a supplied pointer to struct tm
334
415
   * (broken time).
335
416
   *
336
 
   * Returns whether the conversion was 
 
417
   * Returns whether the conversion was
337
418
   * successful.
338
419
   *
339
420
   * @param Pointe rto the struct tm to convert from
352
433
   * Attempts to populate the Date instance based
353
434
   * on the contents of a supplied time_t
354
435
   *
355
 
   * Returns whether the conversion was 
 
436
   * Returns whether the conversion was
356
437
   * successful.
357
438
   *
358
439
   * @param time_t to convert from
360
441
  virtual bool from_time_t(const time_t from);
361
442
 
362
443
  /**
363
 
   * Fills a supplied my_decimal with a representation of 
 
444
   * Fills a supplied my_decimal with a representation of
364
445
   * the Date value.
365
446
   *
366
447
   * @param Pointer to the my_decimal to fill
397
478
  bool operator<(const Time &rhs);
398
479
  bool operator<=(const Time &rhs);
399
480
  /**
400
 
   * Operator to add/subtract a Time from a Time.  
 
481
   * Operator to add/subtract a Time from a Time.
401
482
   * We can return a Time new temporal instance.
402
483
   *
403
484
   * @param Temporal instance to add/subtract to/from
416
497
 
417
498
  /**
418
499
   * Fills a supplied char string with a
419
 
   * string representation of the Time  
 
500
   * string representation of the Time
420
501
   * value.
421
502
   *
422
503
   * @param C-String to fill.
428
509
   * Attempts to populate the Time instance based
429
510
   * on the contents of a supplied string.
430
511
   *
431
 
   * Returns whether the conversion was 
 
512
   * Returns whether the conversion was
432
513
   * successful.
433
514
   *
434
515
   * @param String to convert from
449
530
   * Attempts to populate the Time instance based
450
531
   * on the contents of a supplied 4-byte integer.
451
532
   *
452
 
   * Returns whether the conversion was 
 
533
   * Returns whether the conversion was
453
534
   * successful.
454
535
   *
455
536
   * @param Integer to convert from
460
541
   * Attempts to populate the Time instance based
461
542
   * on the contents of a supplied time_t
462
543
   *
463
 
   * Returns whether the conversion was 
 
544
   * Returns whether the conversion was
464
545
   * successful.
465
546
   *
466
547
   * @note
467
548
   *
468
 
   * We can only convert *from* a time_t, not back 
 
549
   * We can only convert *from* a time_t, not back
469
550
   * to a time_t since it would be a lossy conversion.
470
551
   *
471
552
   * @param time_t to convert from
473
554
  bool from_time_t(const time_t from);
474
555
 
475
556
  /**
476
 
   * Fills a supplied my_decimal with a representation of 
 
557
   * Fills a supplied my_decimal with a representation of
477
558
   * the Time value.
478
559
   *
479
560
   * @param Pointer to the my_decimal to fill
480
561
   */
481
562
  void to_decimal(my_decimal *to) const;
482
563
 
 
564
  friend class Date;
483
565
  friend class DateTime;
484
566
};
485
567
 
491
573
{
492
574
public:
493
575
  DateTime() :Date() {}
494
 
  /**
495
 
   * Comparison operator overloads to compare a DateTime against
496
 
   * another DateTime value.
497
 
   *
498
 
   * @param DateTime to compare against.
499
 
   */
500
 
  bool operator==(const DateTime &rhs);
501
 
  bool operator!=(const DateTime &rhs);
502
 
  bool operator>(const DateTime &rhs);
503
 
  bool operator>=(const DateTime &rhs);
504
 
  bool operator<(const DateTime &rhs);
505
 
  bool operator<=(const DateTime &rhs);
506
 
  /**
507
 
   * Operator to add/subtract a Time from a Time.  
508
 
   * We can return a Time new temporal instance.
509
 
   *
510
 
   * @param Temporal instance to add/subtract to/from
511
 
   */
512
 
  const DateTime operator-(const Time &rhs);
513
 
  const DateTime operator+(const Time &rhs);
514
 
  DateTime& operator-=(const Time &rhs);
515
 
  DateTime& operator+=(const Time &rhs);
516
 
  /**
517
 
   * Operator overload for adding/subtracting another DateTime
518
 
   * (or subclass) to/from this temporal.  When subtracting 
519
 
   * or adding two DateTimes, we return a new DateTime instance.
520
 
   *
521
 
   * @param Temporal instance to add/subtract to/from
522
 
   */
523
 
  const DateTime operator-(const DateTime &rhs);
524
 
  const DateTime operator+(const DateTime &rhs);
525
 
  DateTime& operator+=(const DateTime &rhs);
526
 
  DateTime& operator-=(const DateTime &rhs);
527
 
 
528
 
  /**
529
 
   * Operator overload for adding/subtracting a TemporalInterval
530
 
   * instance to this temporal.
531
 
   *
532
 
   * @param TemporalInterval instance to add/subtract to/from
533
 
   */
534
 
  DateTime& operator+=(const TemporalIntervalYear &rhs);
535
 
  DateTime& operator+=(const TemporalIntervalDayOrLess &rhs);
536
 
  DateTime& operator+=(const TemporalIntervalDayOrWeek &rhs);
537
 
  DateTime& operator+=(const TemporalIntervalYearMonth &rhs);
538
 
  DateTime& operator-=(const TemporalIntervalYear &rhs);
539
 
  DateTime& operator-=(const TemporalIntervalDayOrLess &rhs);
540
 
  DateTime& operator-=(const TemporalIntervalDayOrWeek &rhs);
541
 
  DateTime& operator-=(const TemporalIntervalYearMonth &rhs);
542
576
 
543
577
  friend class TemporalInterval;
544
578
 
545
 
  /* Returns whether the DateTime (or subclass) instance is in the Unix Epoch. */
 
579
  /** Returns whether the DateTime (or subclass) instance
 
580
   *  is in the Unix Epoch.
 
581
   */
546
582
  bool in_unix_epoch() const;
547
583
  /** Returns whether the temporal value is valid datetime. */
548
584
  virtual bool is_valid() const;
549
585
 
550
586
  /**
551
 
   * It's not possible to convert to and from a DateTime and 
 
587
   * It's not possible to convert to and from a DateTime and
552
588
   * a 4-byte integer, so let us know if we try and do it!
553
589
   */
554
590
  void to_int32_t(int32_t *) const {assert(0);}
568
604
   * Attempts to populate the DateTime instance based
569
605
   * on the contents of a supplied string.
570
606
   *
571
 
   * Returns whether the conversion was 
 
607
   * Returns whether the conversion was
572
608
   * successful.
573
609
   *
574
610
   * @param String to convert from
589
625
   * Attempts to populate the DateTime instance based
590
626
   * on the contents of a supplied time_t
591
627
   *
592
 
   * Returns whether the conversion was 
 
628
   * Returns whether the conversion was
593
629
   * successful.
594
630
   *
595
631
   * @param time_t to convert from
600
636
   * Attempts to populate the DateTime instance based
601
637
   * on the contents of a supplied 8-byte integer.
602
638
   *
603
 
   * Returns whether the conversion was 
 
639
   * Returns whether the conversion was
604
640
   * successful.
605
641
   *
606
642
   * @param Integer to convert from
617
653
  void to_tm(struct tm *to) const;
618
654
 
619
655
  /**
620
 
   * Fills a supplied my_decimal with a representation of 
 
656
   * Fills a supplied my_decimal with a representation of
621
657
   * the DateTime value.
622
658
   *
623
659
   * @param Pointer to the my_decimal to fill
628
664
/**
629
665
 * Class representing temporal components in the UNIX epoch
630
666
 */
631
 
class Timestamp: public DateTime 
 
667
class Timestamp: public DateTime
632
668
{
633
669
public:
634
670
  Timestamp() :DateTime() {}
635
 
  /**
636
 
   * Comparison operator overloads to compare a Timestamp against
637
 
   * another Timestamp value.
638
 
   *
639
 
   * @param Timestamp to compare against.
640
 
   */
641
 
  bool operator==(const Timestamp &rhs);
642
 
  bool operator!=(const Timestamp &rhs);
643
 
  bool operator>(const Timestamp &rhs);
644
 
  bool operator>=(const Timestamp &rhs);
645
 
  bool operator<(const Timestamp &rhs);
646
 
  bool operator<=(const Timestamp &rhs);
647
671
 
648
672
  bool is_valid_timestamp() const {return is_valid();}
649
673
  /** Returns whether the temporal value is valid timestamp. */
684
708
   * representation of the MicroTimestamp
685
709
   * value.
686
710
   *
687
 
   * Returns whether the conversion was 
 
711
   * Returns whether the conversion was
688
712
   * successful.
689
713
   *
690
714
   * @param timeval to fill.
708
732
   * representation of the NanoTimestamp
709
733
   * value.
710
734
   *
711
 
   * Returns whether the conversion was 
 
735
   * Returns whether the conversion was
712
736
   * successful.
713
737
   *
714
738
   * @param timespec to fill.