~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
 *
1039.5.1 by Jay Pipes
* New serial event log plugin
4
 *  Copyright (C) 2008-2009 Sun Microsystems
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
/**
892.2.8 by Monty Taylor
Whitespace fixes.
22
 * @file
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
892.2.8 by Monty Taylor
Whitespace fixes.
27
 * type.  A number of derived classes define specialized classes
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
28
 * representng various date, date-time, time, or timestamp types.
29
 *
30
 * All Temporal derived classes are ValueObjects.  That is to say that
31
 * Temporal class instances are not part of the Item hierarchy and serve
32
 * <em>only</em> to represent a time or date-related piece of data.
33
 *
34
 * @note
35
 *
36
 * Low-level calendrical calculations are done via routines in the
37
 * calendar.cc file.
38
 *
39
 * @see drizzled/calendar.cc
40
 */
41
42
#ifndef DRIZZLED_TEMPORAL_H
43
#define DRIZZLED_TEMPORAL_H
44
45
#define DRIZZLE_MAX_SECONDS 59
46
#define DRIZZLE_MAX_MINUTES 59
47
#define DRIZZLE_MAX_HOURS 23
48
#define DRIZZLE_MAX_DAYS 31
49
#define DRIZZLE_MAX_MONTHS 12
50
#define DRIZZLE_MAX_YEARS_SQL 9999
51
#define DRIZZLE_MAX_YEARS_EPOCH 2038
52
#define DRIZZLE_MIN_SECONDS 0
53
#define DRIZZLE_MIN_MINUTES 0
54
#define DRIZZLE_MIN_HOURS 0
55
#define DRIZZLE_MIN_DAYS 1
56
#define DRIZZLE_MIN_MONTHS 1
57
#define DRIZZLE_MIN_YEARS_SQL 1
58
#define DRIZZLE_MIN_YEARS_EPOCH 1970
59
60
#define DRIZZLE_SECONDS_IN_MINUTE 60
61
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
62
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
63
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
64
813.1.12 by Jay Pipes
Fixes for SECOND() function to use new Temporal system. Because
65
#define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
66
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
67
#define DRIZZLE_YY_PART_YEAR  70
68
69
#include "drizzled/calendar.h"
70
907.1.7 by Jay Pipes
Merged in remove-timezone work
71
#include <ostream>
72
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
73
/* Outside forward declarations */
74
class my_decimal;
75
892.2.8 by Monty Taylor
Whitespace fixes.
76
namespace drizzled
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
77
{
78
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
79
/* Forward declaration needed */
80
class TemporalInterval;
81
class TemporalIntervalYear;
82
class TemporalIntervalDayOrLess;
83
class TemporalIntervalDayOrWeek;
84
class TemporalIntervalYearMonth;
85
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
86
/**
87
 * Base class for all temporal data classes.
88
 */
89
class Temporal
90
{
91
protected:
92
  enum calendar _calendar;
93
  uint32_t _years;
94
  uint32_t _months;
95
  uint32_t _days;
96
  uint32_t _hours;
97
  uint32_t _minutes;
98
  uint32_t _seconds;
99
  time_t _epoch_seconds;
100
  uint32_t _useconds;
101
  uint32_t _nseconds;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
102
  /** Set on some operator overloads.  Indicates that an overflow occurred. */
103
  bool _overflow;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
104
  /** Returns number of seconds in time components (hour + minute + second) */
105
  uint64_t _cumulative_seconds_in_time() const;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
106
  /** Resets all temporal components to zero */
892.2.8 by Monty Taylor
Whitespace fixes.
107
  inline void _reset()
108
  {
109
    _years= _months= _days= _hours= _minutes=
110
      _seconds= _epoch_seconds= _useconds= _nseconds= 0;
111
  }
112
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
113
public:
114
  Temporal();
115
  virtual ~Temporal() {}
116
117
  /** Returns the calendar component. */
118
  inline enum calendar calendar() const {return _calendar;}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
119
  /** Sets the nseconds component. */
120
  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
121
  /** Returns the nanoseconds component. */
122
  inline uint32_t nseconds() const {return _nseconds;}
123
  /** Sets the useconds component. */
124
  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
125
  /** Returns the microsseconds component. */
126
  inline uint32_t useconds() const {return _useconds;}
892.2.8 by Monty Taylor
Whitespace fixes.
127
  /**
128
   * Sets the epoch_seconds component automatically,
129
   * based on the temporal's components.
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
130
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
131
  void set_epoch_seconds();
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
132
  /** Sets the epch_seconds component manually. */
892.2.8 by Monty Taylor
Whitespace fixes.
133
  inline void set_epoch_seconds(const uint32_t epoch_second)
134
  {_epoch_seconds= epoch_second;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
135
  /** Returns the UNIX epoch seconds component. */
136
  inline time_t epoch_seconds() const {return _epoch_seconds;}
137
  /** Sets the seconds component. */
138
  inline void set_seconds(const uint32_t second) {_seconds= second;}
139
  /** Returns the seconds component. */
140
  inline uint32_t seconds() const {return _seconds;}
141
  /** Sets the days component. */
142
  inline void set_minutes(const uint32_t minute) {_minutes= minute;}
143
  /** Returns the minutes component. */
144
  inline uint32_t minutes() const {return _minutes;}
145
  /** Sets the hours component. */
146
  inline void set_hours(const uint32_t hour) {_hours= hour;}
147
  /** Returns the hours component. */
148
  inline uint32_t hours() const {return _hours;}
149
  /** Sets the days component. */
150
  inline void set_days(const uint32_t day) {_days= day;}
151
  /** Returns the days component. */
152
  inline uint32_t days() const {return _days;}
153
  /** Sets the months component. */
154
  inline void set_months(const uint32_t month) {_months= month;}
155
  /** Returns the months component. */
156
  inline uint32_t months() const {return _months;}
157
  /** Sets the years component. */
158
  inline void set_years(const uint32_t year) {_years= year;}
159
  /** Returns the years component. */
160
  inline uint32_t years() const {return _years;}
892.2.8 by Monty Taylor
Whitespace fixes.
161
  /** Returns whether the overflow flag was set
162
   *  (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.. :)
163
  inline bool overflow() const {return _overflow;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
164
165
  /** Returns whether the temporal value is valid as a date. */
166
  virtual bool is_valid_date() const= 0;
167
  /** Returns whether the temporal value is valid as a datetime. */
168
  virtual bool is_valid_datetime() const= 0;
169
  /** Returns whether the temporal value is valid as a time. */
170
  virtual bool is_valid_time() const= 0;
171
  /** Returns whether the temporal value is valid as a UNIX timestamp. */
172
  virtual bool is_valid_timestamp() const= 0;
173
174
  /**
175
   * Returns whether the temporal
176
   * value is valid. Each subclass defines what is
177
   * valid for the range of temporal data it contains.
178
   */
179
  virtual bool is_valid() const= 0;
180
181
  /**
182
   * All Temporal derived classes must implement
183
   * conversion routines for converting to and from
892.2.8 by Monty Taylor
Whitespace fixes.
184
   * a string. Subclasses implement other conversion
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
185
   * routines, but should always follow these notes:
186
   *
187
   * 1) Ensure that ALL from_xxx methods call is_valid()
188
   * 2) Ensure that ALL to_xxx methods are void returns and
189
   *    do not call is_valid()
190
   *
191
   * This minimizes the repeated bounds-checking to
192
   * just the conversion from_xxx routines.
193
   */
194
  friend class TemporalFormat;
195
};
196
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
197
/* Forward declaration needed */
198
class DateTime;
892.2.9 by Monty Taylor
Raped Jay's code.
199
class Timestamp;
892.2.10 by Monty Taylor
More raping of Jay's code.
200
class Time;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
201
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
202
/**
203
 * Class representing temporal components in a valid
204
 * SQL date range, with no time component
205
 */
892.2.8 by Monty Taylor
Whitespace fixes.
206
class Date: public Temporal
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
207
{
208
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
209
  Date() :Temporal() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
210
  /**
211
   * Comparison operator overloads to compare a Date against
212
   * another Date value.
213
   *
214
   * @param Date to compare against.
215
   */
910.2.10 by Monty Taylor
Merged in jay's patch again.
216
  virtual bool operator==(const Date &rhs);
217
  virtual bool operator!=(const Date &rhs);
218
  virtual bool operator>(const Date &rhs);
219
  virtual bool operator>=(const Date &rhs);
220
  virtual bool operator<(const Date &rhs);
221
  virtual bool operator<=(const Date &rhs);
892.2.9 by Monty Taylor
Raped Jay's code.
222
223
  /**
224
   * Comparison operator overloads to compare a Date against
225
   * a DateTime value.
226
   *
227
   * @param DateTime to compare against.
228
   */
910.2.10 by Monty Taylor
Merged in jay's patch again.
229
  virtual bool operator==(const DateTime &rhs);
230
  virtual bool operator!=(const DateTime &rhs);
231
  virtual bool operator>(const DateTime &rhs);
232
  virtual bool operator>=(const DateTime &rhs);
233
  virtual bool operator<(const DateTime &rhs);
234
  virtual bool operator<=(const DateTime &rhs);
235
236
  /**
237
   * Comparison operator overloads to compare this against
238
   * a Timestamp value.
239
   *
240
   * @param Timestamp to compare against.
241
   */
242
  virtual bool operator==(const Timestamp &rhs);
243
  virtual bool operator!=(const Timestamp &rhs);
244
  virtual bool operator>(const Timestamp &rhs);
245
  virtual bool operator>=(const Timestamp &rhs);
246
  virtual bool operator<(const Timestamp &rhs);
247
  virtual bool operator<=(const Timestamp &rhs);
892.2.9 by Monty Taylor
Raped Jay's code.
248
249
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
250
   * Operator overload for adding/subtracting another Date
892.2.8 by Monty Taylor
Whitespace fixes.
251
   * (or subclass) to/from this temporal.  When subtracting
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
252
   * or adding two Dates, we return a new Date instance.
253
   *
892.2.9 by Monty Taylor
Raped Jay's code.
254
   * @param Date instance to add/subtract to/from
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
255
   */
892.1.1 by Monty Taylor
Fixed virtual overload warnings.
256
  const Date operator-(const Date &rhs);
257
  const Date operator+(const Date &rhs);
258
  Date& operator+=(const Date &rhs);
259
  Date& operator-=(const Date &rhs);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
260
892.2.10 by Monty Taylor
More raping of Jay's code.
261
  /**
262
   * Operator to add/subtract a Time from a Time.
263
   * We can return a Time new temporal instance.
264
   *
265
   * @param Temporal instance to add/subtract to/from
266
   */
267
  const Date operator-(const Time &rhs);
268
  const Date operator+(const Time &rhs);
269
  Date& operator-=(const Time &rhs);
270
  Date& operator+=(const Time &rhs);
271
892.2.9 by Monty Taylor
Raped Jay's code.
272
273
  /**
274
   * Operator overload for adding/subtracting a DateTime
275
   * (or subclass) to/from this temporal.  When subtracting
276
   * or adding two Dates, we return a new Date instance.
277
   *
278
   * @param DateTime instance to add/subtract to/from
279
   */
280
  const Date operator-(const DateTime &rhs);
281
  const Date operator+(const DateTime &rhs);
282
  Date& operator+=(const DateTime &rhs);
283
  Date& operator-=(const DateTime &rhs);
284
892.2.11 by Monty Taylor
One more temporal hack.
285
286
  /**
287
   * Operator overload for adding/subtracting a TemporalInterval
288
   * instance to this temporal.
289
   *
290
   * @param TemporalInterval instance to add/subtract to/from
291
   */
292
  Date& operator+=(const TemporalIntervalYear &rhs);
293
  Date& operator+=(const TemporalIntervalDayOrLess &rhs);
294
  Date& operator+=(const TemporalIntervalDayOrWeek &rhs);
295
  Date& operator+=(const TemporalIntervalYearMonth &rhs);
296
  Date& operator-=(const TemporalIntervalYear &rhs);
297
  Date& operator-=(const TemporalIntervalDayOrLess &rhs);
298
  Date& operator-=(const TemporalIntervalDayOrWeek &rhs);
299
  Date& operator-=(const TemporalIntervalYearMonth &rhs);
300
301
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
302
  /**
303
   * Operator overload for when a DateTime instance is
304
   * assigned to a Date.  We do a copy of the DateTime's
305
   * date-related components.
306
   *
307
   * @param The DateTime to copy from
308
   */
309
  Date& operator=(const DateTime &rhs);
310
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
311
  virtual bool is_valid_date() const {return is_valid();}
312
  virtual bool is_valid_datetime() const {return is_valid();}
313
  virtual bool is_valid_time() const {return false;}
892.2.8 by Monty Taylor
Whitespace fixes.
314
  virtual bool is_valid_timestamp() const
315
  {
316
    return is_valid() && in_unix_epoch();
317
  }
318
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
319
  /** Returns whether the temporal value is valid date. */
320
  virtual bool is_valid() const;
321
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
322
  virtual bool in_unix_epoch() const;
323
324
  /**
325
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
326
   * string representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
327
   * value.
328
   *
329
   * @param C-String to fill.
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
330
   * @param Length of to C-String
331
   * @returns length of string written (not including trailing '\0').
332
   *          If output was truncated, returns length that would have
333
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
334
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
335
  virtual int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
336
337
  /**
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
338
   * Maximum length of C-String needed to represent type
339
   * (including '\0').
340
   */
341
  static const int MAX_STRING_LENGTH= 11;
342
343
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
344
   * Attempts to populate the Date instance based
345
   * on the contents of a supplied string.
346
   *
892.2.8 by Monty Taylor
Whitespace fixes.
347
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
348
   * successful.
349
   *
350
   * @param String to convert from
351
   * @param Length of supplied string
352
   */
353
  virtual bool from_string(const char *from, size_t from_len);
354
355
  /**
356
   * Fills a supplied 8-byte integer pointer with an
357
   * integer representation of the Date
358
   * value.
359
   *
360
   * @param Integer to fill.
361
   */
362
  virtual void to_int64_t(int64_t *to) const;
363
364
  /**
365
   * Fills a supplied 4-byte integer pointer with an
366
   * integer representation of the Date
367
   * value.
368
   *
369
   * @param Integer to fill.
370
   */
371
  virtual void to_int32_t(int32_t *to) const;
372
373
  /**
374
   * Attempts to populate the Date instance based
375
   * on the contents of a supplied 4-byte integer.
376
   *
892.2.8 by Monty Taylor
Whitespace fixes.
377
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
378
   * successful.
379
   *
380
   * @param Integer to convert from
381
   */
382
  virtual bool from_int32_t(const int32_t from);
383
384
  /**
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
385
   * Fills a supplied int64_t with the Julian Day Number
386
   * representation of this Date.
387
   *
388
   * @note Julian Day Number != julian day!
389
   *
390
   * Julian Day Number is the monotonically increasing number
391
   * of days from the start of the Julian calendar (~4713 B.C.)
392
   *
393
   * julian day is the ordinal day number of a day in a year.
394
   *
395
   * @param int64_t to fill
396
   */
397
  void to_julian_day_number(int64_t *to) const;
398
399
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
400
   * Attempts to populate the Date instance based
401
   * on the contents of a supplied Julian Day Number
402
   *
892.2.8 by Monty Taylor
Whitespace fixes.
403
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
404
   * successful.
405
   *
406
   * @param Integer to convert from
407
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
408
  bool from_julian_day_number(const int64_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
409
410
  /**
411
   * Fills a supplied tm pointer with an
892.2.8 by Monty Taylor
Whitespace fixes.
412
   * representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
413
   * value.
414
   *
415
   * @param tm to fill.
416
   */
417
  virtual void to_tm(struct tm *to) const;
418
419
  /**
420
   * Attempts to populate the Date instance based
421
   * on the contents of a supplied pointer to struct tm
422
   * (broken time).
423
   *
892.2.8 by Monty Taylor
Whitespace fixes.
424
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
425
   * successful.
426
   *
427
   * @param Pointe rto the struct tm to convert from
428
   */
429
  virtual bool from_tm(const struct tm *from);
430
431
  /**
432
   * Attempts to convert the Date value into
433
   * a supplied time_t.
434
   *
435
   * @param Pointer to a time_t to convert to
436
   */
437
  virtual void to_time_t(time_t *to) const;
438
439
  /**
440
   * Attempts to populate the Date instance based
441
   * on the contents of a supplied time_t
442
   *
892.2.8 by Monty Taylor
Whitespace fixes.
443
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
444
   * successful.
445
   *
446
   * @param time_t to convert from
447
   */
448
  virtual bool from_time_t(const time_t from);
449
450
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
451
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
452
   * the Date value.
453
   *
454
   * @param Pointer to the my_decimal to fill
455
   */
456
  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.. :)
457
458
  friend class TemporalInterval;
910.2.10 by Monty Taylor
Merged in jay's patch again.
459
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
460
};
461
462
/* Forward declare needed for friendship */
463
class DateTime;
464
465
/**
466
 * Class representing temporal components having only
467
 * a time component, with no date structure
468
 */
469
class Time: public Temporal
470
{
471
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
472
  Time() :Temporal() {}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
473
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
474
  const static uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
475
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
476
  /**
477
   * Comparison operator overloads to compare a Time against
478
   * another Time value.
479
   *
480
   * @param Time to compare against.
481
   */
482
  bool operator==(const Time &rhs);
483
  bool operator!=(const Time &rhs);
484
  bool operator>(const Time &rhs);
485
  bool operator>=(const Time &rhs);
486
  bool operator<(const Time &rhs);
487
  bool operator<=(const Time &rhs);
488
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
489
   * Operator to add/subtract a Time from a Time.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
490
   * We can return a Time new temporal instance.
491
   *
492
   * @param Temporal instance to add/subtract to/from
493
   */
494
  const Time operator-(const Time &rhs);
495
  const Time operator+(const Time &rhs);
496
  Time& operator-=(const Time &rhs);
497
  Time& operator+=(const Time &rhs);
498
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
499
  bool is_valid_date() const {return false;}
500
  bool is_valid_datetime() const {return false;}
501
  bool is_valid_time() const {return is_valid();}
502
  bool is_valid_timestamp() const {return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
503
  /** 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.. :)
504
  bool is_valid() const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
505
506
  /**
507
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
508
   * string representation of the Time
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
509
   * value.
510
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
511
   * @param C-String to fill
512
   * @param Length of to C-String
513
   * @returns length of string written (not including trailing '\0').
514
   *          If output was truncated, returns length that would have
515
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
516
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
517
  int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
518
519
  /**
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
520
   * Maximum length of C-String needed to represent type
521
   * (including '\0').
522
   */
523
  static const int MAX_STRING_LENGTH= 9;
524
525
526
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
527
   * Attempts to populate the Time instance based
528
   * on the contents of a supplied string.
529
   *
892.2.8 by Monty Taylor
Whitespace fixes.
530
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
531
   * successful.
532
   *
533
   * @param String to convert from
534
   * @param Length of supplied string
535
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
536
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
537
538
  /**
539
   * Fills a supplied 4-byte integer pointer with an
540
   * integer representation of the Time
541
   * value.
542
   *
543
   * @param Integer to fill.
544
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
545
  void to_int32_t(int32_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
546
547
  /**
548
   * Attempts to populate the Time instance based
549
   * on the contents of a supplied 4-byte integer.
550
   *
892.2.8 by Monty Taylor
Whitespace fixes.
551
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
552
   * successful.
553
   *
554
   * @param Integer to convert from
555
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
556
  bool from_int32_t(const int32_t from);
557
558
  /**
559
   * Attempts to populate the Time instance based
560
   * on the contents of a supplied time_t
561
   *
892.2.8 by Monty Taylor
Whitespace fixes.
562
   * Returns whether the conversion was
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
563
   * successful.
564
   *
565
   * @note
566
   *
892.2.8 by Monty Taylor
Whitespace fixes.
567
   * 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.. :)
568
   * to a time_t since it would be a lossy conversion.
569
   *
570
   * @param time_t to convert from
571
   */
572
  bool from_time_t(const time_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
573
574
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
575
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
576
   * the Time value.
577
   *
578
   * @param Pointer to the my_decimal to fill
579
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
580
  void to_decimal(my_decimal *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
581
892.2.10 by Monty Taylor
More raping of Jay's code.
582
  friend class Date;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
583
  friend class DateTime;
584
};
585
586
/**
587
 * Class representing temporal components in a valid
588
 * SQL datetime range, including a time component
589
 */
590
class DateTime: public Date
591
{
592
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
593
  DateTime() :Date() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
594
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
595
  friend class TemporalInterval;
596
892.2.8 by Monty Taylor
Whitespace fixes.
597
  /** Returns whether the DateTime (or subclass) instance
598
   *  is in the Unix Epoch.
599
   */
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
600
  bool in_unix_epoch() const;
601
  /** Returns whether the temporal value is valid datetime. */
602
  virtual bool is_valid() const;
603
604
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
605
   * 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()
606
   * a 4-byte integer, so let us know if we try and do it!
607
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
608
  void to_int32_t(int32_t *) const {assert(0);}
609
  bool from_int32_t(int32_t) {assert(0); return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
610
611
  /**
612
   * Fills a supplied char string with a
613
   * string representation of the DateTime
614
   * value.
615
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
616
   * @param C-String to fill
617
   * @param Length of to C-String
618
   * @returns length of string written (not including trailing '\0').
619
   *          If output was truncated, returns length that would have
620
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
621
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
622
  virtual int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
623
624
  /**
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
625
   * Maximum length of C-String needed to represent type
626
   * (including '\0').
627
   */
628
  static const int MAX_STRING_LENGTH= 27;
629
630
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
631
   * Attempts to populate the DateTime instance based
632
   * on the contents of a supplied string.
633
   *
892.2.8 by Monty Taylor
Whitespace fixes.
634
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
635
   * successful.
636
   *
637
   * @param String to convert from
638
   * @param Length of supplied string
639
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
640
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
641
642
  /**
643
   * Fills a supplied 8-byte integer pointer with an
644
   * integer representation of the DateTime
645
   * value.
646
   *
647
   * @param Integer to fill.
648
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
649
  void to_int64_t(int64_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
650
651
  /**
652
   * Attempts to populate the DateTime instance based
653
   * on the contents of a supplied time_t
654
   *
892.2.8 by Monty Taylor
Whitespace fixes.
655
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
656
   * successful.
657
   *
658
   * @param time_t to convert from
659
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
660
  bool from_time_t(const time_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
661
662
  /**
663
   * Attempts to populate the DateTime instance based
664
   * on the contents of a supplied 8-byte integer.
665
   *
892.2.8 by Monty Taylor
Whitespace fixes.
666
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
667
   * successful.
668
   *
669
   * @param Integer to convert from
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
670
   * @param convert if conversion to canonical representation
671
   *        should be attempted
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
672
   */
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
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
  }
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
678
679
  /**
680
   * Fills a supplied tm pointer with an
681
   * representation of the DateTime
682
   * value.
683
   *
684
   * @param tm to fill.
685
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
686
  void to_tm(struct tm *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
687
688
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
689
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
690
   * the DateTime value.
691
   *
692
   * @param Pointer to the my_decimal to fill
693
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
694
  void to_decimal(my_decimal *to) const;
910.2.10 by Monty Taylor
Merged in jay's patch again.
695
696
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
697
};
698
699
/**
700
 * Class representing temporal components in the UNIX epoch
701
 */
892.2.8 by Monty Taylor
Whitespace fixes.
702
class Timestamp: public DateTime
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
703
{
704
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
705
  Timestamp() :DateTime() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
706
907.1.7 by Jay Pipes
Merged in remove-timezone work
707
  /**
708
   * Comparison operator overloads to compare this against
910.2.10 by Monty Taylor
Merged in jay's patch again.
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
907.1.7 by Jay Pipes
Merged in remove-timezone work
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
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
746
  bool is_valid_timestamp() const {return is_valid();}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
756
  void to_time_t(time_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
757
};
758
759
/**
907.1.7 by Jay Pipes
Merged in remove-timezone work
760
 * Operator overload to an output stream for a Timestamp.
761
 */
762
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
763
764
/**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
765
 * Class representing temporal components in the UNIX epoch
766
 * with an additional microsecond component.
767
 */
768
class MicroTimestamp: public Timestamp
769
{
770
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
771
  MicroTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
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.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
785
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
786
  int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
787
788
  /**
1079.3.2 by Stewart Smith
Replace MAX_(DATE|TIME).*_WIDTH defines in definitions.h with real (and correct) static const members to Temporal types.
789
   * Maximum length of C-String needed to represent type
790
   * (including '\0').
791
   */
792
  static const int MAX_STRING_LENGTH= 27;
793
794
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
795
   * Fills a supplied timeval pointer with an
796
   * representation of the MicroTimestamp
797
   * value.
798
   *
892.2.8 by Monty Taylor
Whitespace fixes.
799
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
800
   * successful.
801
   *
802
   * @param timeval to fill.
803
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
804
  void to_timeval(struct timeval *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
814
  NanoTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
   *
892.2.8 by Monty Taylor
Whitespace fixes.
823
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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 */