~drizzle-trunk/drizzle/development

1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2009 Sun Microsystems
5
 *
1093.1.36 by Jay Pipes
Fix indentation of temporal_interval.h per IRC conversation with clint
6
 *  Authors: 
7
 *
8
 *  Clint Byrum <clint@fewbar.com>
1097.2.2 by clint at fewbar
style/documentation cleanups and replacing copyright notices
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; version 2 of the License.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, write to the Free Software
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 */
1097.2.1 by clint at fewbar
refactoring INTERVAL into C++ class
23
#include <drizzled/item.h>
24
1093.1.36 by Jay Pipes
Fix indentation of temporal_interval.h per IRC conversation with clint
25
namespace drizzled 
26
{
27
28
/**
29
 * @brief
30
 *  Stores time interval for date/time manipulation
31
 */
32
class TemporalInterval
33
{
34
public:
35
36
  TemporalInterval(uint32_t in_year,
37
      uint32_t in_month,
38
      uint32_t in_day,
39
      uint32_t in_hour,
40
      uint64_t in_minute,
41
      uint64_t in_second,
42
      uint64_t in_second_part,
43
      bool in_neg)
44
    :
45
      year(in_year),
46
      month(in_month),
47
      day(in_day),
48
      hour(in_hour),
49
      minute(in_minute),
50
      second(in_second),
51
      second_part(in_second_part),
52
      neg(in_neg)
53
  {}
54
55
  TemporalInterval()
56
    :
57
      year(0),
58
      month(0),
59
      day(0),
60
      hour(0),
61
      minute(0),
62
      second(0),
63
      second_part(0),
64
      neg(false)
65
  {}
66
67
  /**
68
   * Sets whether or not this object specifies a negative interval
69
   * @param[in] in_neg true if this is a negative interval, false if not
70
   */
71
  void setNegative(bool in_neg= true)
72
  {
73
    neg= in_neg;
74
  }
75
76
  /**
77
   * reverse boolean value of the negative flag
78
   */
79
  void toggleNegative()
80
  {
81
    neg= !neg;
82
  }
83
84
  /**
85
   * @retval true this is a negative temporal interval
86
   * @retval false this is a positive temporal interval
87
   */
88
  bool getNegative() const
89
  {
90
    return neg;
91
  }
92
93
  /**
94
   * Populate this TemporalInterval from a string value
95
   *
96
   * To make code easy, allow interval objects without separators.
97
   *
98
   * @param args argument Item structure
99
   * @param int_type type of interval to create
100
   * @param str_value String pointer to the input value
101
   * @return true if the string would result in a null interval
102
   * 
103
   */
104
  bool initFromItem(Item *args, interval_type int_type, String *str_value);
105
106
  /**
107
   * Adds this interval to a DRIZZLE_LTIME structure
108
   *
109
   * @param[in,out] ltime the interval will be added to ltime directly in the ltime structure
110
   * @param[in] int_type the type of interval requested
111
   * @retval true date was added and value stored properly
112
   * @retval false result of addition is a null value
113
   */
114
  bool addDate(DRIZZLE_TIME *ltime, interval_type int_type);
115
116
private:
117
118
  /**
119
   * The maximum number of text elements to extract into a temporal interval
120
   */
121
  static const uint32_t MAX_STRING_ELEMENTS = 5;
122
123
  /**
124
   * Each of these corresponds to an 'interval_type'
125
   */
126
  static const uint32_t NUM_YEAR_MONTH_STRING_ELEMENTS         = 2;
127
  static const uint32_t NUM_DAY_HOUR_STRING_ELEMENTS           = 2; 
128
  static const uint32_t NUM_DAY_MICROSECOND_STRING_ELEMENTS    = 5;
129
  static const uint32_t NUM_DAY_MINUTE_STRING_ELEMENTS         = 3;
130
  static const uint32_t NUM_DAY_SECOND_STRING_ELEMENTS         = 4;
131
  static const uint32_t NUM_HOUR_MICROSECOND_STRING_ELEMENTS   = 4;
132
  static const uint32_t NUM_HOUR_MINUTE_STRING_ELEMENTS        = 2;
133
  static const uint32_t NUM_HOUR_SECOND_STRING_ELEMENTS        = 3;
134
  static const uint32_t NUM_MINUTE_MICROSECOND_STRING_ELEMENTS = 3;
135
  static const uint32_t NUM_MINUTE_SECOND_STRING_ELEMENTS      = 2;
136
  static const uint32_t NUM_SECOND_MICROSECOND_STRING_ELEMENTS = 2;
137
138
  /**
139
   *  @details
140
   *  Get a array of positive numbers from a string object.
141
   *  Each number is separated by 1 non digit character
142
   *  Return error if there is too many numbers.
143
   *  If there is too few numbers, assume that the numbers are left out
144
   *  from the high end. This allows one to give:
145
   *  DAY_TO_SECOND as "D MM:HH:SS", "MM:HH:SS" "HH:SS" or as seconds.
146
   *
147
   *  @param[in] length:         length of str
148
   *  @param[in] cs:             charset of str
149
   *  @param[out] values:         array of results
150
   *  @param[out] count:          count of elements in result array
151
   *  @param transform_msec: if value is true we suppose
152
   *  that the last part of string value is microseconds
153
   *  and we should transform value to six digit value.
154
   *  For example, '1.1' -> '1.100000'
155
   */
156
  bool getIntervalFromString(const char *str,
157
                             uint32_t length, 
158
                             const CHARSET_INFO * const cs,
159
                             uint32_t count, 
160
                             uint64_t *values,
161
                             bool transform_msec);
162
163
  uint32_t  year;
164
  uint32_t  month;
165
  uint32_t  day;
166
  uint32_t  hour;
167
  uint64_t  minute;
168
  uint64_t  second;
169
  uint64_t  second_part;
170
  bool      neg;
171
172
};
173
174
} /* end namespace drizzled */