~drizzle-trunk/drizzle/development

813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  Authors:
7
 *
8
 *  Jay Pipes <jay.pipes@sun.com>
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program; if not, write to the Free Software
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
 */
24
25
/**
892.2.8 by Monty Taylor
Whitespace fixes.
26
 * @file
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
27
 *
28
 * Defines the API for dealing with temporal data inside the server.
29
 *
30
 * The Temporal class is the base class for all data of any temporal
892.2.8 by Monty Taylor
Whitespace fixes.
31
 * type.  A number of derived classes define specialized classes
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
32
 * representng various date, date-time, time, or timestamp types.
33
 *
34
 * All Temporal derived classes are ValueObjects.  That is to say that
35
 * Temporal class instances are not part of the Item hierarchy and serve
36
 * <em>only</em> to represent a time or date-related piece of data.
37
 *
38
 * @note
39
 *
40
 * Low-level calendrical calculations are done via routines in the
41
 * calendar.cc file.
42
 *
43
 * @see drizzled/calendar.cc
44
 */
45
46
#ifndef DRIZZLED_TEMPORAL_H
47
#define DRIZZLED_TEMPORAL_H
48
49
#define DRIZZLE_MAX_SECONDS 59
50
#define DRIZZLE_MAX_MINUTES 59
51
#define DRIZZLE_MAX_HOURS 23
52
#define DRIZZLE_MAX_DAYS 31
53
#define DRIZZLE_MAX_MONTHS 12
54
#define DRIZZLE_MAX_YEARS_SQL 9999
55
#define DRIZZLE_MAX_YEARS_EPOCH 2038
56
#define DRIZZLE_MIN_SECONDS 0
57
#define DRIZZLE_MIN_MINUTES 0
58
#define DRIZZLE_MIN_HOURS 0
59
#define DRIZZLE_MIN_DAYS 1
60
#define DRIZZLE_MIN_MONTHS 1
61
#define DRIZZLE_MIN_YEARS_SQL 1
62
#define DRIZZLE_MIN_YEARS_EPOCH 1970
63
64
#define DRIZZLE_SECONDS_IN_MINUTE 60
65
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
66
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
67
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
68
813.1.12 by Jay Pipes
Fixes for SECOND() function to use new Temporal system. Because
69
#define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
70
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
71
#define DRIZZLE_YY_PART_YEAR  70
72
73
#include "drizzled/calendar.h"
74
907.1.7 by Jay Pipes
Merged in remove-timezone work
75
#include <ostream>
76
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
77
/* Outside forward declarations */
78
class my_decimal;
79
892.2.8 by Monty Taylor
Whitespace fixes.
80
namespace drizzled
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
81
{
82
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
83
/* Forward declaration needed */
84
class TemporalInterval;
85
class TemporalIntervalYear;
86
class TemporalIntervalDayOrLess;
87
class TemporalIntervalDayOrWeek;
88
class TemporalIntervalYearMonth;
89
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
106
  /** Set on some operator overloads.  Indicates that an overflow occurred. */
107
  bool _overflow;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
108
  /** Returns number of seconds in time components (hour + minute + second) */
109
  uint64_t _cumulative_seconds_in_time() const;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
110
  /** Resets all temporal components to zero */
892.2.8 by Monty Taylor
Whitespace fixes.
111
  inline void _reset()
112
  {
113
    _years= _months= _days= _hours= _minutes=
114
      _seconds= _epoch_seconds= _useconds= _nseconds= 0;
115
  }
116
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
117
public:
118
  Temporal();
119
  virtual ~Temporal() {}
120
121
  /** Returns the calendar component. */
122
  inline enum calendar calendar() const {return _calendar;}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
123
  /** Sets the nseconds component. */
124
  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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;}
892.2.8 by Monty Taylor
Whitespace fixes.
131
  /**
132
   * Sets the epoch_seconds component automatically,
133
   * based on the temporal's components.
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
134
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
135
  void set_epoch_seconds();
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
136
  /** Sets the epch_seconds component manually. */
892.2.8 by Monty Taylor
Whitespace fixes.
137
  inline void set_epoch_seconds(const uint32_t epoch_second)
138
  {_epoch_seconds= epoch_second;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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;}
892.2.8 by Monty Taylor
Whitespace fixes.
165
  /** Returns whether the overflow flag was set
166
   *  (which can occur during an overloaded operator's execution) */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
167
  inline bool overflow() const {return _overflow;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
892.2.8 by Monty Taylor
Whitespace fixes.
188
   * a string. Subclasses implement other conversion
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
201
/* Forward declaration needed */
202
class DateTime;
892.2.9 by Monty Taylor
Raped Jay's code.
203
class Timestamp;
892.2.10 by Monty Taylor
More raping of Jay's code.
204
class Time;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
205
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
206
/**
207
 * Class representing temporal components in a valid
208
 * SQL date range, with no time component
209
 */
892.2.8 by Monty Taylor
Whitespace fixes.
210
class Date: public Temporal
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
211
{
212
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
213
  Date() :Temporal() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
214
  /**
215
   * Comparison operator overloads to compare a Date against
216
   * another Date value.
217
   *
218
   * @param Date to compare against.
219
   */
910.2.10 by Monty Taylor
Merged in jay's patch again.
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);
892.2.9 by Monty Taylor
Raped Jay's code.
226
227
  /**
228
   * Comparison operator overloads to compare a Date against
229
   * a DateTime value.
230
   *
231
   * @param DateTime to compare against.
232
   */
910.2.10 by Monty Taylor
Merged in jay's patch again.
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);
892.2.9 by Monty Taylor
Raped Jay's code.
252
253
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
254
   * Operator overload for adding/subtracting another Date
892.2.8 by Monty Taylor
Whitespace fixes.
255
   * (or subclass) to/from this temporal.  When subtracting
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
256
   * or adding two Dates, we return a new Date instance.
257
   *
892.2.9 by Monty Taylor
Raped Jay's code.
258
   * @param Date instance to add/subtract to/from
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
259
   */
892.1.1 by Monty Taylor
Fixed virtual overload warnings.
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);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
264
892.2.10 by Monty Taylor
More raping of Jay's code.
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
892.2.9 by Monty Taylor
Raped Jay's code.
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
892.2.11 by Monty Taylor
One more temporal hack.
289
290
  /**
291
   * Operator overload for adding/subtracting a TemporalInterval
292
   * instance to this temporal.
293
   *
294
   * @param TemporalInterval instance to add/subtract to/from
295
   */
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
  Date& operator-=(const TemporalIntervalYear &rhs);
301
  Date& operator-=(const TemporalIntervalDayOrLess &rhs);
302
  Date& operator-=(const TemporalIntervalDayOrWeek &rhs);
303
  Date& operator-=(const TemporalIntervalYearMonth &rhs);
304
305
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
306
  /**
307
   * Operator overload for when a DateTime instance is
308
   * assigned to a Date.  We do a copy of the DateTime's
309
   * date-related components.
310
   *
311
   * @param The DateTime to copy from
312
   */
313
  Date& operator=(const DateTime &rhs);
314
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
315
  virtual bool is_valid_date() const {return is_valid();}
316
  virtual bool is_valid_datetime() const {return is_valid();}
317
  virtual bool is_valid_time() const {return false;}
892.2.8 by Monty Taylor
Whitespace fixes.
318
  virtual bool is_valid_timestamp() const
319
  {
320
    return is_valid() && in_unix_epoch();
321
  }
322
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
323
  /** Returns whether the temporal value is valid date. */
324
  virtual bool is_valid() const;
325
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
326
  virtual bool in_unix_epoch() const;
327
328
  /**
329
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
330
   * string representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
331
   * value.
332
   *
333
   * @param C-String to fill.
334
   * @param Length of filled string (out param)
335
   */
336
  virtual void to_string(char *to, size_t *to_len) const;
337
338
  /**
339
   * Attempts to populate the Date instance based
340
   * on the contents of a supplied string.
341
   *
892.2.8 by Monty Taylor
Whitespace fixes.
342
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
343
   * successful.
344
   *
345
   * @param String to convert from
346
   * @param Length of supplied string
347
   */
348
  virtual bool from_string(const char *from, size_t from_len);
349
350
  /**
351
   * Fills a supplied 8-byte integer pointer with an
352
   * integer representation of the Date
353
   * value.
354
   *
355
   * @param Integer to fill.
356
   */
357
  virtual void to_int64_t(int64_t *to) const;
358
359
  /**
360
   * Fills a supplied 4-byte integer pointer with an
361
   * integer representation of the Date
362
   * value.
363
   *
364
   * @param Integer to fill.
365
   */
366
  virtual void to_int32_t(int32_t *to) const;
367
368
  /**
369
   * Attempts to populate the Date instance based
370
   * on the contents of a supplied 4-byte integer.
371
   *
892.2.8 by Monty Taylor
Whitespace fixes.
372
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
373
   * successful.
374
   *
375
   * @param Integer to convert from
376
   */
377
  virtual bool from_int32_t(const int32_t from);
378
379
  /**
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
380
   * Fills a supplied int64_t with the Julian Day Number
381
   * representation of this Date.
382
   *
383
   * @note Julian Day Number != julian day!
384
   *
385
   * Julian Day Number is the monotonically increasing number
386
   * of days from the start of the Julian calendar (~4713 B.C.)
387
   *
388
   * julian day is the ordinal day number of a day in a year.
389
   *
390
   * @param int64_t to fill
391
   */
392
  void to_julian_day_number(int64_t *to) const;
393
394
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
395
   * Attempts to populate the Date instance based
396
   * on the contents of a supplied Julian Day Number
397
   *
892.2.8 by Monty Taylor
Whitespace fixes.
398
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
399
   * successful.
400
   *
401
   * @param Integer to convert from
402
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
403
  bool from_julian_day_number(const int64_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
404
405
  /**
406
   * Fills a supplied tm pointer with an
892.2.8 by Monty Taylor
Whitespace fixes.
407
   * representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
408
   * value.
409
   *
410
   * @param tm to fill.
411
   */
412
  virtual void to_tm(struct tm *to) const;
413
414
  /**
415
   * Attempts to populate the Date instance based
416
   * on the contents of a supplied pointer to struct tm
417
   * (broken time).
418
   *
892.2.8 by Monty Taylor
Whitespace fixes.
419
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
420
   * successful.
421
   *
422
   * @param Pointe rto the struct tm to convert from
423
   */
424
  virtual bool from_tm(const struct tm *from);
425
426
  /**
427
   * Attempts to convert the Date value into
428
   * a supplied time_t.
429
   *
430
   * @param Pointer to a time_t to convert to
431
   */
432
  virtual void to_time_t(time_t *to) const;
433
434
  /**
435
   * Attempts to populate the Date instance based
436
   * on the contents of a supplied time_t
437
   *
892.2.8 by Monty Taylor
Whitespace fixes.
438
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
439
   * successful.
440
   *
441
   * @param time_t to convert from
442
   */
443
  virtual bool from_time_t(const time_t from);
444
445
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
446
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
447
   * the Date value.
448
   *
449
   * @param Pointer to the my_decimal to fill
450
   */
451
  virtual void to_decimal(my_decimal *to) const;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
452
453
  friend class TemporalInterval;
910.2.10 by Monty Taylor
Merged in jay's patch again.
454
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
455
};
456
457
/* Forward declare needed for friendship */
458
class DateTime;
459
460
/**
461
 * Class representing temporal components having only
462
 * a time component, with no date structure
463
 */
464
class Time: public Temporal
465
{
466
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
467
  Time() :Temporal() {}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
468
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
469
  const static uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
470
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
471
  /**
472
   * Comparison operator overloads to compare a Time against
473
   * another Time value.
474
   *
475
   * @param Time to compare against.
476
   */
477
  bool operator==(const Time &rhs);
478
  bool operator!=(const Time &rhs);
479
  bool operator>(const Time &rhs);
480
  bool operator>=(const Time &rhs);
481
  bool operator<(const Time &rhs);
482
  bool operator<=(const Time &rhs);
483
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
484
   * Operator to add/subtract a Time from a Time.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
485
   * We can return a Time new temporal instance.
486
   *
487
   * @param Temporal instance to add/subtract to/from
488
   */
489
  const Time operator-(const Time &rhs);
490
  const Time operator+(const Time &rhs);
491
  Time& operator-=(const Time &rhs);
492
  Time& operator+=(const Time &rhs);
493
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
494
  bool is_valid_date() const {return false;}
495
  bool is_valid_datetime() const {return false;}
496
  bool is_valid_time() const {return is_valid();}
497
  bool is_valid_timestamp() const {return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
498
  /** Returns whether the temporal value is valid date. */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
499
  bool is_valid() const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
500
501
  /**
502
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
503
   * string representation of the Time
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
504
   * value.
505
   *
506
   * @param C-String to fill.
507
   * @param Length of filled string (out param)
508
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
509
  void to_string(char *to, size_t *to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
510
511
  /**
512
   * Attempts to populate the Time instance based
513
   * on the contents of a supplied string.
514
   *
892.2.8 by Monty Taylor
Whitespace fixes.
515
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
516
   * successful.
517
   *
518
   * @param String to convert from
519
   * @param Length of supplied string
520
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
521
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
530
  void to_int32_t(int32_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
531
532
  /**
533
   * Attempts to populate the Time instance based
534
   * on the contents of a supplied 4-byte integer.
535
   *
892.2.8 by Monty Taylor
Whitespace fixes.
536
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
537
   * successful.
538
   *
539
   * @param Integer to convert from
540
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
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
   *
892.2.8 by Monty Taylor
Whitespace fixes.
547
   * Returns whether the conversion was
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
548
   * successful.
549
   *
550
   * @note
551
   *
892.2.8 by Monty Taylor
Whitespace fixes.
552
   * We can only convert *from* a time_t, not back
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
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);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
558
559
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
560
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
561
   * the Time value.
562
   *
563
   * @param Pointer to the my_decimal to fill
564
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
565
  void to_decimal(my_decimal *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
566
892.2.10 by Monty Taylor
More raping of Jay's code.
567
  friend class Date;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
578
  DateTime() :Date() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
579
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
580
  friend class TemporalInterval;
581
892.2.8 by Monty Taylor
Whitespace fixes.
582
  /** Returns whether the DateTime (or subclass) instance
583
   *  is in the Unix Epoch.
584
   */
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
585
  bool in_unix_epoch() const;
586
  /** Returns whether the temporal value is valid datetime. */
587
  virtual bool is_valid() const;
588
589
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
590
   * It's not possible to convert to and from a DateTime and
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
591
   * a 4-byte integer, so let us know if we try and do it!
592
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
593
  void to_int32_t(int32_t *) const {assert(0);}
594
  bool from_int32_t(int32_t) {assert(0); return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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 filled string (out param)
603
   */
604
  virtual void to_string(char *to, size_t *to_len) const;
605
606
  /**
607
   * Attempts to populate the DateTime instance based
608
   * on the contents of a supplied string.
609
   *
892.2.8 by Monty Taylor
Whitespace fixes.
610
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
611
   * successful.
612
   *
613
   * @param String to convert from
614
   * @param Length of supplied string
615
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
616
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
617
618
  /**
619
   * Fills a supplied 8-byte integer pointer with an
620
   * integer representation of the DateTime
621
   * value.
622
   *
623
   * @param Integer to fill.
624
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
625
  void to_int64_t(int64_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
626
627
  /**
628
   * Attempts to populate the DateTime instance based
629
   * on the contents of a supplied time_t
630
   *
892.2.8 by Monty Taylor
Whitespace fixes.
631
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
632
   * successful.
633
   *
634
   * @param time_t to convert from
635
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
636
  bool from_time_t(const time_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
637
638
  /**
639
   * Attempts to populate the DateTime instance based
640
   * on the contents of a supplied 8-byte integer.
641
   *
892.2.8 by Monty Taylor
Whitespace fixes.
642
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
643
   * successful.
644
   *
645
   * @param Integer to convert from
646
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
647
  bool from_int64_t(const int64_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
648
649
  /**
650
   * Fills a supplied tm pointer with an
651
   * representation of the DateTime
652
   * value.
653
   *
654
   * @param tm to fill.
655
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
656
  void to_tm(struct tm *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
657
658
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
659
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
660
   * the DateTime value.
661
   *
662
   * @param Pointer to the my_decimal to fill
663
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
664
  void to_decimal(my_decimal *to) const;
910.2.10 by Monty Taylor
Merged in jay's patch again.
665
666
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
667
};
668
669
/**
670
 * Class representing temporal components in the UNIX epoch
671
 */
892.2.8 by Monty Taylor
Whitespace fixes.
672
class Timestamp: public DateTime
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
673
{
674
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
675
  Timestamp() :DateTime() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
676
907.1.7 by Jay Pipes
Merged in remove-timezone work
677
  /**
678
   * Comparison operator overloads to compare this against
910.2.10 by Monty Taylor
Merged in jay's patch again.
679
   * a Date value.
680
   *
681
   * @param Timestamp to compare against.
682
   */
683
  bool operator==(const Date &rhs);
684
  bool operator!=(const Date &rhs);
685
  bool operator>(const Date &rhs);
686
  bool operator>=(const Date &rhs);
687
  bool operator<(const Date &rhs);
688
  bool operator<=(const Date &rhs);
689
690
  /**
691
   * Comparison operator overloads to compare this against
692
   * a DateTime value.
693
   *
694
   * @param DateTime to compare against.
695
   */
696
  bool operator==(const DateTime &rhs);
697
  bool operator!=(const DateTime &rhs);
698
  bool operator>(const DateTime &rhs);
699
  bool operator>=(const DateTime &rhs);
700
  bool operator<(const DateTime &rhs);
701
  bool operator<=(const DateTime &rhs);
702
703
  /**
704
   * Comparison operator overloads to compare this against
907.1.7 by Jay Pipes
Merged in remove-timezone work
705
   * another Timestamp value.
706
   *
707
   * @param Timestamp to compare against.
708
   */
709
  bool operator==(const Timestamp &rhs);
710
  bool operator!=(const Timestamp &rhs);
711
  bool operator>(const Timestamp &rhs);
712
  bool operator>=(const Timestamp &rhs);
713
  bool operator<(const Timestamp &rhs);
714
  bool operator<=(const Timestamp &rhs);
715
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
716
  bool is_valid_timestamp() const {return is_valid();}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
717
  /** Returns whether the temporal value is valid timestamp. */
718
  virtual bool is_valid() const;
719
720
  /**
721
   * Attempts to convert the Timestamp value into
722
   * a supplied time_t.
723
   *
724
   * @param Pointer to a time_t to convert to
725
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
726
  void to_time_t(time_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
727
};
728
729
/**
907.1.7 by Jay Pipes
Merged in remove-timezone work
730
 * Operator overload to an output stream for a Timestamp.
731
 */
732
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
733
734
/**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
735
 * Class representing temporal components in the UNIX epoch
736
 * with an additional microsecond component.
737
 */
738
class MicroTimestamp: public Timestamp
739
{
740
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
741
  MicroTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
742
  /** Returns whether the temporal value is valid micro-timestamp. */
743
  bool is_valid() const;
744
745
  /**
746
   * Fills a supplied char string with a
747
   * string representation of the MicroTimestamp
748
   * value.
749
   *
750
   * @param C-String to fill.
751
   * @param Length of filled string (out param)
752
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
753
  void to_string(char *to, size_t *to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
754
755
  /**
756
   * Fills a supplied timeval pointer with an
757
   * representation of the MicroTimestamp
758
   * value.
759
   *
892.2.8 by Monty Taylor
Whitespace fixes.
760
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
761
   * successful.
762
   *
763
   * @param timeval to fill.
764
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
765
  void to_timeval(struct timeval *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
766
};
767
768
/**
769
 * Class representing temporal components in the UNIX epoch
770
 * with an additional nanosecond component.
771
 */
772
class NanoTimestamp: public Timestamp
773
{
774
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
775
  NanoTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
776
  /** Returns whether the temporal value is valid nano-timestamp. */
777
  bool is_valid() const;
778
779
  /**
780
   * Fills a supplied timespec pointer with an
781
   * representation of the NanoTimestamp
782
   * value.
783
   *
892.2.8 by Monty Taylor
Whitespace fixes.
784
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
785
   * successful.
786
   *
787
   * @param timespec to fill.
788
   */
789
  void to_timespec(struct timespec *to) const;
790
};
791
792
} /* end namespace drizzled */
793
794
#endif /* DRIZZLED_TEMPORAL_H */