~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
  /**
288
   * Operator overload for adding/subtracting a TemporalInterval
289
   * instance to this temporal.
290
   *
291
   * @param TemporalInterval instance to add/subtract to/from
292
   */
293
  Date& operator+=(const TemporalIntervalYear &rhs);
294
  Date& operator+=(const TemporalIntervalDayOrLess &rhs);
295
  Date& operator+=(const TemporalIntervalDayOrWeek &rhs);
296
  Date& operator+=(const TemporalIntervalYearMonth &rhs);
297
  Date& operator-=(const TemporalIntervalYear &rhs);
298
  Date& operator-=(const TemporalIntervalDayOrLess &rhs);
299
  Date& operator-=(const TemporalIntervalDayOrWeek &rhs);
300
  Date& operator-=(const TemporalIntervalYearMonth &rhs);
301
302
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
303
  /**
304
   * Operator overload for when a DateTime instance is
305
   * assigned to a Date.  We do a copy of the DateTime's
306
   * date-related components.
307
   *
308
   * @param The DateTime to copy from
309
   */
310
  Date& operator=(const DateTime &rhs);
311
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
312
  virtual bool is_valid_date() const {return is_valid();}
313
  virtual bool is_valid_datetime() const {return is_valid();}
314
  virtual bool is_valid_time() const {return false;}
892.2.8 by Monty Taylor
Whitespace fixes.
315
  virtual bool is_valid_timestamp() const
316
  {
317
    return is_valid() && in_unix_epoch();
318
  }
319
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
320
  /** Returns whether the temporal value is valid date. */
321
  virtual bool is_valid() const;
322
  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
323
  virtual bool in_unix_epoch() const;
324
325
  /**
326
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
327
   * string representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
328
   * value.
329
   *
330
   * @param C-String to fill.
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
331
   * @param Length of to C-String
332
   * @returns length of string written (not including trailing '\0').
333
   *          If output was truncated, returns length that would have
334
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
335
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
336
  virtual int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
337
338
  /**
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.
339
   * Maximum length of C-String needed to represent type
340
   * (including '\0').
341
   */
342
  static const int MAX_STRING_LENGTH= 11;
343
344
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
345
   * Attempts to populate the Date instance based
346
   * on the contents of a supplied string.
347
   *
892.2.8 by Monty Taylor
Whitespace fixes.
348
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
349
   * successful.
350
   *
351
   * @param String to convert from
352
   * @param Length of supplied string
353
   */
354
  virtual bool from_string(const char *from, size_t from_len);
355
356
  /**
357
   * Fills a supplied 8-byte integer pointer with an
358
   * integer representation of the Date
359
   * value.
360
   *
361
   * @param Integer to fill.
362
   */
363
  virtual void to_int64_t(int64_t *to) const;
364
365
  /**
366
   * Fills a supplied 4-byte integer pointer with an
367
   * integer representation of the Date
368
   * value.
369
   *
370
   * @param Integer to fill.
371
   */
372
  virtual void to_int32_t(int32_t *to) const;
373
374
  /**
375
   * Attempts to populate the Date instance based
376
   * on the contents of a supplied 4-byte integer.
377
   *
892.2.8 by Monty Taylor
Whitespace fixes.
378
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
379
   * successful.
380
   *
381
   * @param Integer to convert from
382
   */
383
  virtual bool from_int32_t(const int32_t from);
384
385
  /**
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
386
   * Fills a supplied int64_t with the Julian Day Number
387
   * representation of this Date.
388
   *
389
   * @note Julian Day Number != julian day!
390
   *
391
   * Julian Day Number is the monotonically increasing number
392
   * of days from the start of the Julian calendar (~4713 B.C.)
393
   *
394
   * julian day is the ordinal day number of a day in a year.
395
   *
396
   * @param int64_t to fill
397
   */
398
  void to_julian_day_number(int64_t *to) const;
399
400
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
401
   * Attempts to populate the Date instance based
402
   * on the contents of a supplied Julian Day Number
403
   *
892.2.8 by Monty Taylor
Whitespace fixes.
404
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
405
   * successful.
406
   *
407
   * @param Integer to convert from
408
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
409
  bool from_julian_day_number(const int64_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
410
411
  /**
412
   * Fills a supplied tm pointer with an
892.2.8 by Monty Taylor
Whitespace fixes.
413
   * representation of the Date
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
414
   * value.
415
   *
416
   * @param tm to fill.
417
   */
418
  virtual void to_tm(struct tm *to) const;
419
420
  /**
421
   * Attempts to populate the Date instance based
422
   * on the contents of a supplied pointer to struct tm
423
   * (broken time).
424
   *
892.2.8 by Monty Taylor
Whitespace fixes.
425
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
426
   * successful.
427
   *
428
   * @param Pointe rto the struct tm to convert from
429
   */
430
  virtual bool from_tm(const struct tm *from);
431
432
  /**
433
   * Attempts to convert the Date value into
434
   * a supplied time_t.
435
   *
436
   * @param Pointer to a time_t to convert to
437
   */
438
  virtual void to_time_t(time_t *to) const;
439
440
  /**
441
   * Attempts to populate the Date instance based
442
   * on the contents of a supplied time_t
443
   *
892.2.8 by Monty Taylor
Whitespace fixes.
444
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
445
   * successful.
446
   *
447
   * @param time_t to convert from
448
   */
449
  virtual bool from_time_t(const time_t from);
450
451
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
452
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
453
   * the Date value.
454
   *
455
   * @param Pointer to the my_decimal to fill
456
   */
457
  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.. :)
458
459
  friend class TemporalInterval;
910.2.10 by Monty Taylor
Merged in jay's patch again.
460
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
461
};
462
463
/* Forward declare needed for friendship */
464
class DateTime;
465
466
/**
467
 * Class representing temporal components having only
468
 * a time component, with no date structure
469
 */
470
class Time: public Temporal
471
{
472
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
473
  Time() :Temporal() {}
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
474
  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
1240.9.5 by Monty Taylor
remark #82: storage class is not first
475
  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.. :)
476
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
477
  /**
478
   * Comparison operator overloads to compare a Time against
479
   * another Time value.
480
   *
481
   * @param Time to compare against.
482
   */
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
  bool operator<=(const Time &rhs);
489
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
490
   * Operator to add/subtract a Time from a Time.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
491
   * We can return a Time new temporal instance.
492
   *
493
   * @param Temporal instance to add/subtract to/from
494
   */
495
  const Time operator-(const Time &rhs);
496
  const Time operator+(const Time &rhs);
497
  Time& operator-=(const Time &rhs);
498
  Time& operator+=(const Time &rhs);
499
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
500
  bool is_valid_date() const {return false;}
501
  bool is_valid_datetime() const {return false;}
502
  bool is_valid_time() const {return is_valid();}
503
  bool is_valid_timestamp() const {return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
504
  /** 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.. :)
505
  bool is_valid() const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
506
507
  /**
508
   * Fills a supplied char string with a
892.2.8 by Monty Taylor
Whitespace fixes.
509
   * string representation of the Time
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
510
   * value.
511
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
512
   * @param C-String to fill
513
   * @param Length of to C-String
514
   * @returns length of string written (not including trailing '\0').
515
   *          If output was truncated, returns length that would have
516
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
517
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
518
  int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
519
520
  /**
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.
521
   * Maximum length of C-String needed to represent type
522
   * (including '\0').
523
   */
524
  static const int MAX_STRING_LENGTH= 9;
525
526
527
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
528
   * Attempts to populate the Time instance based
529
   * on the contents of a supplied string.
530
   *
892.2.8 by Monty Taylor
Whitespace fixes.
531
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
532
   * successful.
533
   *
534
   * @param String to convert from
535
   * @param Length of supplied string
536
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
537
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
538
539
  /**
540
   * Fills a supplied 4-byte integer pointer with an
541
   * integer representation of the Time
542
   * value.
543
   *
544
   * @param Integer to fill.
545
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
546
  void to_int32_t(int32_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
547
548
  /**
549
   * Attempts to populate the Time instance based
550
   * on the contents of a supplied 4-byte integer.
551
   *
892.2.8 by Monty Taylor
Whitespace fixes.
552
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
553
   * successful.
554
   *
555
   * @param Integer to convert from
556
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
557
  bool from_int32_t(const int32_t from);
558
559
  /**
560
   * Attempts to populate the Time instance based
561
   * on the contents of a supplied time_t
562
   *
892.2.8 by Monty Taylor
Whitespace fixes.
563
   * Returns whether the conversion was
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
564
   * successful.
565
   *
566
   * @note
567
   *
892.2.8 by Monty Taylor
Whitespace fixes.
568
   * 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.. :)
569
   * to a time_t since it would be a lossy conversion.
570
   *
571
   * @param time_t to convert from
572
   */
573
  bool from_time_t(const time_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
574
575
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
576
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
577
   * the Time value.
578
   *
579
   * @param Pointer to the my_decimal to fill
580
   */
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
581
  void to_decimal(my_decimal *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
582
892.2.10 by Monty Taylor
More raping of Jay's code.
583
  friend class Date;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
584
  friend class DateTime;
585
};
586
587
/**
588
 * Class representing temporal components in a valid
589
 * SQL datetime range, including a time component
590
 */
591
class DateTime: public Date
592
{
593
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
594
  DateTime() :Date() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
595
873.1.4 by Jay Pipes
Solaris integer truncation fixes - Thanks to MontyT for the warning output.. :)
596
  friend class TemporalInterval;
597
892.2.8 by Monty Taylor
Whitespace fixes.
598
  /** Returns whether the DateTime (or subclass) instance
599
   *  is in the Unix Epoch.
600
   */
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
601
  bool in_unix_epoch() const;
602
  /** Returns whether the temporal value is valid datetime. */
603
  virtual bool is_valid() const;
604
605
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
606
   * 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()
607
   * a 4-byte integer, so let us know if we try and do it!
608
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
609
  void to_int32_t(int32_t *) const {assert(0);}
610
  bool from_int32_t(int32_t) {assert(0); return false;}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
611
612
  /**
613
   * Fills a supplied char string with a
614
   * string representation of the DateTime
615
   * value.
616
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
617
   * @param C-String to fill
618
   * @param Length of to C-String
619
   * @returns length of string written (not including trailing '\0').
620
   *          If output was truncated, returns length that would have
621
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
622
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
623
  virtual int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
624
625
  /**
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.
626
   * Maximum length of C-String needed to represent type
627
   * (including '\0').
628
   */
629
  static const int MAX_STRING_LENGTH= 27;
630
631
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
632
   * Attempts to populate the DateTime instance based
633
   * on the contents of a supplied string.
634
   *
892.2.8 by Monty Taylor
Whitespace fixes.
635
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
636
   * successful.
637
   *
638
   * @param String to convert from
639
   * @param Length of supplied string
640
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
641
  bool from_string(const char *from, size_t from_len);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
642
643
  /**
644
   * Fills a supplied 8-byte integer pointer with an
645
   * integer representation of the DateTime
646
   * value.
647
   *
648
   * @param Integer to fill.
649
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
650
  void to_int64_t(int64_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
651
652
  /**
653
   * Attempts to populate the DateTime instance based
654
   * on the contents of a supplied time_t
655
   *
892.2.8 by Monty Taylor
Whitespace fixes.
656
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
657
   * successful.
658
   *
659
   * @param time_t to convert from
660
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
661
  bool from_time_t(const time_t from);
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
662
663
  /**
664
   * Attempts to populate the DateTime instance based
665
   * on the contents of a supplied 8-byte integer.
666
   *
892.2.8 by Monty Taylor
Whitespace fixes.
667
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
668
   * successful.
669
   *
670
   * @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.
671
   * @param convert if conversion to canonical representation
672
   *        should be attempted
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
673
   */
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.
674
  bool from_int64_t(const int64_t from, bool convert);
675
676
  bool from_int64_t(const int64_t from) {
677
    return from_int64_t(from, true);
678
  }
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
679
680
  /**
681
   * Fills a supplied tm pointer with an
682
   * representation of the DateTime
683
   * value.
684
   *
685
   * @param tm to fill.
686
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
687
  void to_tm(struct tm *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
688
689
  /**
892.2.8 by Monty Taylor
Whitespace fixes.
690
   * Fills a supplied my_decimal with a representation of
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
691
   * the DateTime value.
692
   *
693
   * @param Pointer to the my_decimal to fill
694
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
695
  void to_decimal(my_decimal *to) const;
910.2.10 by Monty Taylor
Merged in jay's patch again.
696
697
  friend class Timestamp;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
698
};
699
700
/**
701
 * Class representing temporal components in the UNIX epoch
702
 */
892.2.8 by Monty Taylor
Whitespace fixes.
703
class Timestamp: public DateTime
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
704
{
705
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
706
  Timestamp() :DateTime() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
707
907.1.7 by Jay Pipes
Merged in remove-timezone work
708
  /**
709
   * Comparison operator overloads to compare this against
910.2.10 by Monty Taylor
Merged in jay's patch again.
710
   * a Date value.
711
   *
712
   * @param Timestamp to compare against.
713
   */
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
  bool operator<=(const Date &rhs);
720
721
  /**
722
   * Comparison operator overloads to compare this against
723
   * a DateTime value.
724
   *
725
   * @param DateTime to compare against.
726
   */
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
  bool operator<=(const DateTime &rhs);
733
734
  /**
735
   * Comparison operator overloads to compare this against
907.1.7 by Jay Pipes
Merged in remove-timezone work
736
   * another Timestamp value.
737
   *
738
   * @param Timestamp to compare against.
739
   */
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
  bool operator<=(const Timestamp &rhs);
746
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
747
  bool is_valid_timestamp() const {return is_valid();}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
748
  /** Returns whether the temporal value is valid timestamp. */
749
  virtual bool is_valid() const;
750
751
  /**
752
   * Attempts to convert the Timestamp value into
753
   * a supplied time_t.
754
   *
755
   * @param Pointer to a time_t to convert to
756
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
757
  void to_time_t(time_t *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
758
};
759
760
/**
907.1.7 by Jay Pipes
Merged in remove-timezone work
761
 * Operator overload to an output stream for a Timestamp.
762
 */
763
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
764
765
/**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
766
 * Class representing temporal components in the UNIX epoch
767
 * with an additional microsecond component.
768
 */
769
class MicroTimestamp: public Timestamp
770
{
771
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
772
  MicroTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
773
  /** Returns whether the temporal value is valid micro-timestamp. */
774
  bool is_valid() const;
775
776
  /**
777
   * Fills a supplied char string with a
778
   * string representation of the MicroTimestamp
779
   * value.
780
   *
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
781
   * @param C-String to fill
782
   * @param Length of to C-String
783
   * @returns length of string written (not including trailing '\0').
784
   *          If output was truncated, returns length that would have
785
   *          been outputted.
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
786
   */
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
787
  int to_string(char *to, size_t to_len) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
788
789
  /**
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.
790
   * Maximum length of C-String needed to represent type
791
   * (including '\0').
792
   */
793
  static const int MAX_STRING_LENGTH= 27;
794
795
  /**
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
796
   * Fills a supplied timeval pointer with an
797
   * representation of the MicroTimestamp
798
   * value.
799
   *
892.2.8 by Monty Taylor
Whitespace fixes.
800
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
801
   * successful.
802
   *
803
   * @param timeval to fill.
804
   */
873.1.6 by Jay Pipes
More shadowing fixes. Good buddy Solaris is fixing improper virtuality in subclasses. Nice. :)
805
  void to_timeval(struct timeval *to) const;
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
806
};
807
808
/**
809
 * Class representing temporal components in the UNIX epoch
810
 * with an additional nanosecond component.
811
 */
812
class NanoTimestamp: public Timestamp
813
{
814
public:
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
815
  NanoTimestamp() :Timestamp() {}
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
816
  /** Returns whether the temporal value is valid nano-timestamp. */
817
  bool is_valid() const;
818
819
  /**
820
   * Fills a supplied timespec pointer with an
821
   * representation of the NanoTimestamp
822
   * value.
823
   *
892.2.8 by Monty Taylor
Whitespace fixes.
824
   * Returns whether the conversion was
813.1.2 by Jay Pipes
First function cleanup for temporal handling: YEAR()
825
   * successful.
826
   *
827
   * @param timespec to fill.
828
   */
829
  void to_timespec(struct timespec *to) const;
830
};
831
832
} /* end namespace drizzled */
833
834
#endif /* DRIZZLED_TEMPORAL_H */