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