~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
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
71
#include <cassert>
907.1.7 by Jay Pipes
Merged in remove-timezone work
72
#include <ostream>
73
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
74
/* Outside forward declarations */
75
class my_decimal;
76
892.2.8 by Monty Taylor
Whitespace fixes.
77
namespace drizzled
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
78
{
79
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
80
/* Forward declaration needed */
81
class TemporalInterval;
82
class TemporalIntervalYear;
83
class TemporalIntervalDayOrLess;
84
class TemporalIntervalDayOrWeek;
85
class TemporalIntervalYearMonth;
86
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
87
/**
88
 * Base class for all temporal data classes.
89
 */
90
class Temporal
91
{
92
protected:
93
  enum calendar _calendar;
94
  uint32_t _years;
95
  uint32_t _months;
96
  uint32_t _days;
97
  uint32_t _hours;
98
  uint32_t _minutes;
99
  uint32_t _seconds;
100
  time_t _epoch_seconds;
101
  uint32_t _useconds;
102
  uint32_t _nseconds;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
103
  /** Set on some operator overloads.  Indicates that an overflow occurred. */
104
  bool _overflow;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
105
  /** Returns number of seconds in time components (hour + minute + second) */
106
  uint64_t _cumulative_seconds_in_time() const;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
107
  /** Resets all temporal components to zero */
892.2.8 by Monty Taylor
Whitespace fixes.
108
  inline void _reset()
109
  {
110
    _years= _months= _days= _hours= _minutes=
111
      _seconds= _epoch_seconds= _useconds= _nseconds= 0;
112
  }
113
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
114
public:
115
  Temporal();
116
  virtual ~Temporal() {}
117
118
  /** Returns the calendar component. */
119
  inline enum calendar calendar() const {return _calendar;}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
120
  /** Sets the nseconds component. */
121
  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
122
  /** Returns the nanoseconds component. */
123
  inline uint32_t nseconds() const {return _nseconds;}
124
  /** Sets the useconds component. */
125
  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
126
  /** Returns the microsseconds component. */
127
  inline uint32_t useconds() const {return _useconds;}
892.2.8 by Monty Taylor
Whitespace fixes.
128
  /**
129
   * Sets the epoch_seconds component automatically,
130
   * based on the temporal's components.
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
131
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
132
  void set_epoch_seconds();
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
133
  /** Sets the epch_seconds component manually. */
892.2.8 by Monty Taylor
Whitespace fixes.
134
  inline void set_epoch_seconds(const uint32_t epoch_second)
135
  {_epoch_seconds= epoch_second;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
136
  /** Returns the UNIX epoch seconds component. */
137
  inline time_t epoch_seconds() const {return _epoch_seconds;}
138
  /** Sets the seconds component. */
139
  inline void set_seconds(const uint32_t second) {_seconds= second;}
140
  /** Returns the seconds component. */
141
  inline uint32_t seconds() const {return _seconds;}
142
  /** Sets the days component. */
143
  inline void set_minutes(const uint32_t minute) {_minutes= minute;}
144
  /** Returns the minutes component. */
145
  inline uint32_t minutes() const {return _minutes;}
146
  /** Sets the hours component. */
147
  inline void set_hours(const uint32_t hour) {_hours= hour;}
148
  /** Returns the hours component. */
149
  inline uint32_t hours() const {return _hours;}
150
  /** Sets the days component. */
151
  inline void set_days(const uint32_t day) {_days= day;}
152
  /** Returns the days component. */
153
  inline uint32_t days() const {return _days;}
154
  /** Sets the months component. */
155
  inline void set_months(const uint32_t month) {_months= month;}
156
  /** Returns the months component. */
157
  inline uint32_t months() const {return _months;}
158
  /** Sets the years component. */
159
  inline void set_years(const uint32_t year) {_years= year;}
160
  /** Returns the years component. */
161
  inline uint32_t years() const {return _years;}
892.2.8 by Monty Taylor
Whitespace fixes.
162
  /** Returns whether the overflow flag was set
163
   *  (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.. :)
164
  inline bool overflow() const {return _overflow;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
165
166
  /** Returns whether the temporal value is valid as a date. */
167
  virtual bool is_valid_date() const= 0;
168
  /** Returns whether the temporal value is valid as a datetime. */
169
  virtual bool is_valid_datetime() const= 0;
170
  /** Returns whether the temporal value is valid as a time. */
171
  virtual bool is_valid_time() const= 0;
172
  /** Returns whether the temporal value is valid as a UNIX timestamp. */
173
  virtual bool is_valid_timestamp() const= 0;
174
175
  /**
176
   * Returns whether the temporal
177
   * value is valid. Each subclass defines what is
178
   * valid for the range of temporal data it contains.
179
   */
180
  virtual bool is_valid() const= 0;
181
182
  /**
183
   * All Temporal derived classes must implement
184
   * conversion routines for converting to and from
892.2.8 by Monty Taylor
Whitespace fixes.
185
   * a string. Subclasses implement other conversion
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
186
   * routines, but should always follow these notes:
187
   *
188
   * 1) Ensure that ALL from_xxx methods call is_valid()
189
   * 2) Ensure that ALL to_xxx methods are void returns and
190
   *    do not call is_valid()
191
   *
192
   * This minimizes the repeated bounds-checking to
193
   * just the conversion from_xxx routines.
194
   */
195
  friend class TemporalFormat;
196
};
197
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
198
/* Forward declaration needed */
199
class DateTime;
892.2.9 by Monty Taylor
Raped Jay's code.
200
class Timestamp;
892.2.10 by Monty Taylor
More raping of Jay's code.
201
class Time;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
202
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
203
/**
204
 * Class representing temporal components in a valid
205
 * SQL date range, with no time component
206
 */
892.2.8 by Monty Taylor
Whitespace fixes.
207
class Date: public Temporal
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
208
{
209
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
210
  Date() :Temporal() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
211
  /**
212
   * Comparison operator overloads to compare a Date against
213
   * another Date value.
214
   *
215
   * @param Date to compare against.
216
   */
910.2.10 by Monty Taylor
Merged in jay's patch again.
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);
222
  virtual bool operator<=(const Date &rhs);
892.2.9 by Monty Taylor
Raped Jay's code.
223
224
  /**
225
   * Comparison operator overloads to compare a Date against
226
   * a DateTime value.
227
   *
228
   * @param DateTime to compare against.
229
   */
910.2.10 by Monty Taylor
Merged in jay's patch again.
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
  virtual bool operator<=(const DateTime &rhs);
236
237
  /**
238
   * Comparison operator overloads to compare this against
239
   * a Timestamp value.
240
   *
241
   * @param Timestamp to compare against.
242
   */
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);
248
  virtual bool operator<=(const Timestamp &rhs);
892.2.9 by Monty Taylor
Raped Jay's code.
249
250
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
251
   * Operator overload for adding/subtracting another Date
892.2.8 by Monty Taylor
Whitespace fixes.
252
   * (or subclass) to/from this temporal.  When subtracting
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
253
   * or adding two Dates, we return a new Date instance.
254
   *
892.2.9 by Monty Taylor
Raped Jay's code.
255
   * @param Date instance to add/subtract to/from
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
256
   */
892.1.1 by Monty Taylor
Fixed virtual overload warnings.
257
  const Date operator-(const Date &rhs);
258
  const Date operator+(const Date &rhs);
259
  Date& operator+=(const Date &rhs);
260
  Date& operator-=(const Date &rhs);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
261
892.2.10 by Monty Taylor
More raping of Jay's code.
262
  /**
263
   * Operator to add/subtract a Time from a Time.
264
   * We can return a Time new temporal instance.
265
   *
266
   * @param Temporal instance to add/subtract to/from
267
   */
268
  const Date operator-(const Time &rhs);
269
  const Date operator+(const Time &rhs);
270
  Date& operator-=(const Time &rhs);
271
  Date& operator+=(const Time &rhs);
272
892.2.9 by Monty Taylor
Raped Jay's code.
273
274
  /**
275
   * Operator overload for adding/subtracting a DateTime
276
   * (or subclass) to/from this temporal.  When subtracting
277
   * or adding two Dates, we return a new Date instance.
278
   *
279
   * @param DateTime instance to add/subtract to/from
280
   */
281
  const Date operator-(const DateTime &rhs);
282
  const Date operator+(const DateTime &rhs);
283
  Date& operator+=(const DateTime &rhs);
284
  Date& operator-=(const DateTime &rhs);
285
892.2.11 by Monty Taylor
One more temporal hack.
286
287
  /**
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
288
   * Operator overload for when a DateTime instance is
289
   * assigned to a Date.  We do a copy of the DateTime's
290
   * date-related components.
291
   *
292
   * @param The DateTime to copy from
293
   */
294
  Date& operator=(const DateTime &rhs);
295
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
296
  virtual bool is_valid_date() const {return is_valid();}
297
  virtual bool is_valid_datetime() const {return is_valid();}
298
  virtual bool is_valid_time() const {return false;}
892.2.8 by Monty Taylor
Whitespace fixes.
299
  virtual bool is_valid_timestamp() const
300
  {
301
    return is_valid() && in_unix_epoch();
302
  }
303
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
304
  /** Returns whether the temporal value is valid date. */
305
  virtual bool is_valid() const;
306
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
307
  virtual bool in_unix_epoch() const;
308
309
  /**
310
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
311
   * string representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
312
   * value.
313
   *
314
   * @param C-String to fill.
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
315
   * @param Length of to C-String
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
316
   * @returns length of string written (including trailing '\0').
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
317
   *          If output was truncated, returns length that would have
318
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
319
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
320
  virtual int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
321
322
  /**
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.
323
   * Maximum length of C-String needed to represent type
324
   * (including '\0').
325
   */
326
  static const int MAX_STRING_LENGTH= 11;
327
328
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
329
   * Attempts to populate the Date instance based
330
   * on the contents of a supplied string.
331
   *
892.2.8 by Monty Taylor
Whitespace fixes.
332
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
333
   * successful.
334
   *
335
   * @param String to convert from
1377.8.25 by Paweł Blokus
this was the first working build, and these changes make almost all the tests pass
336
   * @param Length of supplied string (not including trailing '\0').
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
337
   */
338
  virtual bool from_string(const char *from, size_t from_len);
339
340
  /**
341
   * Fills a supplied 8-byte integer pointer with an
342
   * integer representation of the Date
343
   * value.
344
   *
345
   * @param Integer to fill.
346
   */
347
  virtual void to_int64_t(int64_t *to) const;
348
349
  /**
350
   * Fills a supplied 4-byte integer pointer with an
351
   * integer representation of the Date
352
   * value.
353
   *
354
   * @param Integer to fill.
355
   */
356
  virtual void to_int32_t(int32_t *to) const;
357
358
  /**
359
   * Attempts to populate the Date instance based
360
   * on the contents of a supplied 4-byte integer.
361
   *
892.2.8 by Monty Taylor
Whitespace fixes.
362
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
363
   * successful.
364
   *
365
   * @param Integer to convert from
366
   */
367
  virtual bool from_int32_t(const int32_t from);
368
369
  /**
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
370
   * Fills a supplied int64_t with the Julian Day Number
371
   * representation of this Date.
372
   *
373
   * @note Julian Day Number != julian day!
374
   *
375
   * Julian Day Number is the monotonically increasing number
376
   * of days from the start of the Julian calendar (~4713 B.C.)
377
   *
378
   * julian day is the ordinal day number of a day in a year.
379
   *
380
   * @param int64_t to fill
381
   */
382
  void to_julian_day_number(int64_t *to) const;
383
384
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
385
   * Attempts to populate the Date instance based
386
   * on the contents of a supplied Julian Day Number
387
   *
892.2.8 by Monty Taylor
Whitespace fixes.
388
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
389
   * successful.
390
   *
391
   * @param Integer to convert from
392
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
393
  bool from_julian_day_number(const int64_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
394
395
  /**
396
   * Fills a supplied tm pointer with an
892.2.8 by Monty Taylor
Whitespace fixes.
397
   * representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
398
   * value.
399
   *
400
   * @param tm to fill.
401
   */
402
  virtual void to_tm(struct tm *to) const;
403
404
  /**
405
   * Attempts to populate the Date instance based
406
   * on the contents of a supplied pointer to struct tm
407
   * (broken time).
408
   *
892.2.8 by Monty Taylor
Whitespace fixes.
409
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
410
   * successful.
411
   *
412
   * @param Pointe rto the struct tm to convert from
413
   */
414
  virtual bool from_tm(const struct tm *from);
415
416
  /**
417
   * Attempts to convert the Date value into
418
   * a supplied time_t.
419
   *
420
   * @param Pointer to a time_t to convert to
421
   */
422
  virtual void to_time_t(time_t *to) const;
423
424
  /**
425
   * Attempts to populate the Date instance based
426
   * on the contents of a supplied time_t
427
   *
892.2.8 by Monty Taylor
Whitespace fixes.
428
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
429
   * successful.
430
   *
431
   * @param time_t to convert from
432
   */
433
  virtual bool from_time_t(const time_t from);
434
435
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
436
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
437
   * the Date value.
438
   *
439
   * @param Pointer to the my_decimal to fill
440
   */
441
  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.. :)
442
443
  friend class TemporalInterval;
910.2.10 by Monty Taylor
Merged in jay's patch again.
444
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
445
};
446
447
/* Forward declare needed for friendship */
448
class DateTime;
449
450
/**
451
 * Class representing temporal components having only
452
 * a time component, with no date structure
453
 */
454
class Time: public Temporal
455
{
456
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
457
  Time() :Temporal() {}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
458
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
1240.9.5 by Monty Taylor
remark #82: storage class is not first
459
  static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
460
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
461
  /**
462
   * Comparison operator overloads to compare a Time against
463
   * another Time value.
464
   *
465
   * @param Time to compare against.
466
   */
467
  bool operator==(const Time &rhs);
468
  bool operator!=(const Time &rhs);
469
  bool operator>(const Time &rhs);
470
  bool operator>=(const Time &rhs);
471
  bool operator<(const Time &rhs);
472
  bool operator<=(const Time &rhs);
473
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
474
   * Operator to add/subtract a Time from a Time.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
475
   * We can return a Time new temporal instance.
476
   *
477
   * @param Temporal instance to add/subtract to/from
478
   */
479
  const Time operator-(const Time &rhs);
480
  const Time operator+(const Time &rhs);
481
  Time& operator-=(const Time &rhs);
482
  Time& operator+=(const Time &rhs);
483
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
484
  bool is_valid_date() const {return false;}
485
  bool is_valid_datetime() const {return false;}
486
  bool is_valid_time() const {return is_valid();}
487
  bool is_valid_timestamp() const {return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
488
  /** 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.. :)
489
  bool is_valid() const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
490
491
  /**
492
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
493
   * string representation of the Time
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
494
   * value.
495
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
496
   * @param C-String to fill
497
   * @param Length of to C-String
498
   * @returns length of string written (not including trailing '\0').
499
   *          If output was truncated, returns length that would have
500
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
501
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
502
  int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
503
504
  /**
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.
505
   * Maximum length of C-String needed to represent type
506
   * (including '\0').
507
   */
508
  static const int MAX_STRING_LENGTH= 9;
509
510
511
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
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
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
601
   * @param C-String to fill
602
   * @param Length of to C-String
603
   * @returns length of string written (not including trailing '\0').
604
   *          If output was truncated, returns length that would have
605
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
606
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
607
  virtual int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
608
609
  /**
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.
610
   * Maximum length of C-String needed to represent type
611
   * (including '\0').
612
   */
613
  static const int MAX_STRING_LENGTH= 27;
614
615
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
616
   * Attempts to populate the DateTime instance based
617
   * on the contents of a supplied string.
618
   *
892.2.8 by Monty Taylor
Whitespace fixes.
619
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
620
   * successful.
621
   *
622
   * @param String to convert from
623
   * @param Length of supplied string
624
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
625
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
626
627
  /**
628
   * Fills a supplied 8-byte integer pointer with an
629
   * integer representation of the DateTime
630
   * value.
631
   *
632
   * @param Integer to fill.
633
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
634
  void to_int64_t(int64_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
635
636
  /**
637
   * Attempts to populate the DateTime instance based
638
   * on the contents of a supplied time_t
639
   *
892.2.8 by Monty Taylor
Whitespace fixes.
640
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
641
   * successful.
642
   *
643
   * @param time_t to convert from
644
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
645
  bool from_time_t(const time_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
646
647
  /**
648
   * Attempts to populate the DateTime instance based
649
   * on the contents of a supplied 8-byte integer.
650
   *
892.2.8 by Monty Taylor
Whitespace fixes.
651
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
652
   * successful.
653
   *
654
   * @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.
655
   * @param convert if conversion to canonical representation
656
   *        should be attempted
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
657
   */
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.
658
  bool from_int64_t(const int64_t from, bool convert);
659
660
  bool from_int64_t(const int64_t from) {
661
    return from_int64_t(from, true);
662
  }
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
663
664
  /**
665
   * Fills a supplied tm pointer with an
666
   * representation of the DateTime
667
   * value.
668
   *
669
   * @param tm to fill.
670
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
671
  void to_tm(struct tm *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
672
673
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
674
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
675
   * the DateTime value.
676
   *
677
   * @param Pointer to the my_decimal to fill
678
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
679
  void to_decimal(my_decimal *to) const;
910.2.10 by Monty Taylor
Merged in jay's patch again.
680
681
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
682
};
683
684
/**
685
 * Class representing temporal components in the UNIX epoch
686
 */
892.2.8 by Monty Taylor
Whitespace fixes.
687
class Timestamp: public DateTime
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
688
{
689
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
690
  Timestamp() :DateTime() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
691
907.1.7 by Jay Pipes
Merged in remove-timezone work
692
  /**
693
   * Comparison operator overloads to compare this against
910.2.10 by Monty Taylor
Merged in jay's patch again.
694
   * a Date value.
695
   *
696
   * @param Timestamp to compare against.
697
   */
698
  bool operator==(const Date &rhs);
699
  bool operator!=(const Date &rhs);
700
  bool operator>(const Date &rhs);
701
  bool operator>=(const Date &rhs);
702
  bool operator<(const Date &rhs);
703
  bool operator<=(const Date &rhs);
704
705
  /**
706
   * Comparison operator overloads to compare this against
707
   * a DateTime value.
708
   *
709
   * @param DateTime to compare against.
710
   */
711
  bool operator==(const DateTime &rhs);
712
  bool operator!=(const DateTime &rhs);
713
  bool operator>(const DateTime &rhs);
714
  bool operator>=(const DateTime &rhs);
715
  bool operator<(const DateTime &rhs);
716
  bool operator<=(const DateTime &rhs);
717
718
  /**
719
   * Comparison operator overloads to compare this against
907.1.7 by Jay Pipes
Merged in remove-timezone work
720
   * another Timestamp value.
721
   *
722
   * @param Timestamp to compare against.
723
   */
724
  bool operator==(const Timestamp &rhs);
725
  bool operator!=(const Timestamp &rhs);
726
  bool operator>(const Timestamp &rhs);
727
  bool operator>=(const Timestamp &rhs);
728
  bool operator<(const Timestamp &rhs);
729
  bool operator<=(const Timestamp &rhs);
730
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
731
  bool is_valid_timestamp() const {return is_valid();}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
732
  /** Returns whether the temporal value is valid timestamp. */
733
  virtual bool is_valid() const;
734
735
  /**
736
   * Attempts to convert the Timestamp value into
737
   * a supplied time_t.
738
   *
739
   * @param Pointer to a time_t to convert to
740
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
741
  void to_time_t(time_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
742
};
743
744
/**
907.1.7 by Jay Pipes
Merged in remove-timezone work
745
 * Operator overload to an output stream for a Timestamp.
746
 */
747
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
748
749
/**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
750
 * Class representing temporal components in the UNIX epoch
751
 * with an additional microsecond component.
752
 */
753
class MicroTimestamp: public Timestamp
754
{
755
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
756
  MicroTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
757
  /** Returns whether the temporal value is valid micro-timestamp. */
758
  bool is_valid() const;
759
760
  /**
761
   * Fills a supplied char string with a
762
   * string representation of the MicroTimestamp
763
   * value.
764
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
765
   * @param C-String to fill
766
   * @param Length of to C-String
767
   * @returns length of string written (not including trailing '\0').
768
   *          If output was truncated, returns length that would have
769
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
770
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
771
  int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
772
773
  /**
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.
774
   * Maximum length of C-String needed to represent type
775
   * (including '\0').
776
   */
777
  static const int MAX_STRING_LENGTH= 27;
778
779
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
780
   * Fills a supplied timeval pointer with an
781
   * representation of the MicroTimestamp
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 timeval to fill.
788
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
789
  void to_timeval(struct timeval *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
790
};
791
792
/**
793
 * Class representing temporal components in the UNIX epoch
794
 * with an additional nanosecond component.
795
 */
796
class NanoTimestamp: public Timestamp
797
{
798
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
799
  NanoTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
800
  /** Returns whether the temporal value is valid nano-timestamp. */
801
  bool is_valid() const;
802
803
  /**
804
   * Fills a supplied timespec pointer with an
805
   * representation of the NanoTimestamp
806
   * value.
807
   *
892.2.8 by Monty Taylor
Whitespace fixes.
808
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
809
   * successful.
810
   *
811
   * @param timespec to fill.
812
   */
813
  void to_timespec(struct timespec *to) const;
814
};
815
816
} /* end namespace drizzled */
817
818
#endif /* DRIZZLED_TEMPORAL_H */