~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzle_time.h

  • Committer: Brian Aker
  • Date: 2010-11-08 17:40:41 UTC
  • mto: (1921.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 1916.
  • Revision ID: brian@tangent.org-20101108174041-owap774v8l5vvnl4
Push some functions behind classes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#ifndef DRIZZLED_TYPE_TIME_H
22
 
#define DRIZZLED_TYPE_TIME_H
 
21
#ifndef DRIZZLED_DRIZZLE_TIME_H
 
22
#define DRIZZLED_DRIZZLE_TIME_H
23
23
 
24
24
#if TIME_WITH_SYS_TIME
25
25
# include <sys/time.h>
32
32
# endif
33
33
#endif
34
34
 
35
 
#include <drizzled/sql_string.h>
36
 
 
37
35
namespace drizzled
38
36
{
39
37
 
41
39
extern unsigned char days_in_month[];
42
40
 
43
41
/* Time handling defaults */
 
42
#define TIMESTAMP_MAX_YEAR 2038
44
43
#define TIMESTAMP_MIN_YEAR (1900 + YY_PART_YEAR - 1)
45
44
#define TIMESTAMP_MAX_VALUE INT32_MAX
46
45
#define TIMESTAMP_MIN_VALUE 1
51
50
/* Flags to str_to_datetime */
52
51
#define TIME_FUZZY_DATE         1
53
52
#define TIME_DATETIME_ONLY      2
54
 
 
55
53
/* Must be same as MODE_NO_ZERO_IN_DATE */
56
54
#define TIME_NO_ZERO_IN_DATE    (65536L*2*2*2*2*2*2*2)
57
 
 
58
55
/* Must be same as MODE_NO_ZERO_DATE */
59
56
#define TIME_NO_ZERO_DATE       (TIME_NO_ZERO_IN_DATE*2)
60
57
#define TIME_INVALID_DATES      (TIME_NO_ZERO_DATE*2)
71
68
#define TIME_MAX_VALUE_SECONDS (TIME_MAX_HOUR * 3600L + \
72
69
                                TIME_MAX_MINUTE * 60L + TIME_MAX_SECOND)
73
70
 
 
71
enum enum_drizzle_timestamp_type
 
72
{
 
73
  DRIZZLE_TIMESTAMP_NONE= -2, DRIZZLE_TIMESTAMP_ERROR= -1,
 
74
  DRIZZLE_TIMESTAMP_DATE= 0, DRIZZLE_TIMESTAMP_DATETIME= 1, DRIZZLE_TIMESTAMP_TIME= 2
 
75
};
 
76
 
 
77
 
74
78
/*
75
79
  Structure which is used to represent datetime values inside Drizzle.
76
80
 
78
82
  month <= 12, day <= 31, hour <= 23, hour <= 59, hour <= 59. Many functions
79
83
  in server such as my_system_gmt_sec() or make_time() family of functions
80
84
  rely on this (actually now usage of make_*() family relies on a bit weaker
81
 
  restriction). Also functions that produce type::Time as result ensure this.
 
85
  restriction). Also functions that produce DRIZZLE_TIME as result ensure this.
82
86
  There is one exception to this rule though if this structure holds time
83
87
  value (time_type == DRIZZLE_TIMESTAMP_TIME) days and hour member can hold
84
88
  bigger values.
85
89
*/
86
 
namespace type {
87
 
 
88
 
enum timestamp_t
89
 
{
90
 
  DRIZZLE_TIMESTAMP_NONE= -2, DRIZZLE_TIMESTAMP_ERROR= -1,
91
 
  DRIZZLE_TIMESTAMP_DATE= 0, DRIZZLE_TIMESTAMP_DATETIME= 1, DRIZZLE_TIMESTAMP_TIME= 2
92
 
};
93
 
 
94
 
enum cut_t
95
 
{
96
 
  VALID= 0,
97
 
  CUT= 1,
98
 
  INVALID= 2
99
 
};
100
 
 
101
 
/*
102
 
  datatime_t while being stored in an integer is actually a formatted value.
103
 
*/
104
 
typedef int64_t datetime_t;
105
 
typedef int64_t date_t;
106
 
 
107
 
inline bool is_valid(const datetime_t &value)
108
 
{
109
 
  if (value == -1L)
110
 
    return false;
111
 
 
112
 
  return true;
113
 
}
114
 
 
115
 
class Time
116
 
{
117
 
public:
118
 
  typedef uint32_t usec_t;
119
 
  typedef int64_t epoch_t;
120
 
 
121
 
  Time()
122
 
  {
123
 
    reset();
124
 
  }
125
 
 
126
 
  Time(uint32_t year_arg,
127
 
       uint32_t month_arg,
128
 
       uint32_t day_arg,
129
 
       uint32_t hour_arg,
130
 
       uint32_t minute_arg,
131
 
       uint32_t second_arg,
132
 
       usec_t second_part_arg,
133
 
       timestamp_t type_arg) :
134
 
    year(year_arg),
135
 
    month(month_arg),
136
 
    day(day_arg),
137
 
    hour(hour_arg),
138
 
    minute(minute_arg),
139
 
    second(second_arg),
140
 
    second_part(second_part_arg),
141
 
    neg(false),
142
 
    time_type(type_arg),
143
 
    _is_local_time(false)
144
 
  {
145
 
  }
146
 
 
147
 
  Time(uint32_t hour_arg,
148
 
       uint32_t minute_arg,
149
 
       uint32_t second_arg,
150
 
       usec_t second_part_arg,
151
 
       bool neg_arg) :
152
 
    year(0),
153
 
    month(0),
154
 
    day(0),
155
 
    hour(hour_arg),
156
 
    minute(minute_arg),
157
 
    second(second_arg),
158
 
    second_part(second_part_arg),
159
 
    neg(neg_arg),
160
 
    time_type(DRIZZLE_TIMESTAMP_TIME),
161
 
    _is_local_time(false)
162
 
  {
163
 
  }
164
 
 
165
 
  uint32_t year, month, day, hour, minute, second;
166
 
  usec_t second_part;
167
 
  bool neg;
168
 
  timestamp_t time_type;
169
 
  bool _is_local_time;
170
 
 
171
 
  void reset()
172
 
  {
173
 
    year= month= day= hour= minute= second= second_part= 0;
174
 
    neg= false;
175
 
    time_type= DRIZZLE_TIMESTAMP_DATE;
176
 
    _is_local_time= false;
177
 
  }
178
 
 
179
 
  timestamp_t type() const
180
 
  {
181
 
    return time_type;
182
 
  }
183
 
 
184
 
  void convert(drizzled::String &str, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
185
 
  void convert(char *str, size_t &to_length, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
186
 
  void convert(datetime_t &datetime, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
187
 
  void convert(datetime_t &ret, int64_t nr, uint32_t flags);
188
 
  void convert(datetime_t &ret, int64_t nr, uint32_t flags, type::cut_t &was_cut);
189
 
  void convert(type::Time::epoch_t &epoch, long *my_timezone,
190
 
               bool *in_dst_time_gap, bool skip_timezone= false) const;
191
 
 
192
 
  void truncate(const timestamp_t arg);
193
 
 
194
 
  bool store(const char *str,uint32_t length, int &warning, type::timestamp_t arg= DRIZZLE_TIMESTAMP_TIME);
195
 
  type::timestamp_t store(const char *str, uint32_t length, uint32_t flags, type::cut_t &was_cut);
196
 
  type::timestamp_t store(const char *str, uint32_t length, uint32_t flags);
197
 
  void store(const type::Time::epoch_t &from, bool use_localtime= false);
198
 
  void store(const type::Time::epoch_t &from, const usec_t &from_fractional_seconds, bool use_localtime= false);
199
 
  void store(const struct tm &from);
200
 
  void store(const struct timeval &from);
201
 
 
202
 
 
203
 
  static const uint32_t FRACTIONAL_DIGITS= 1000000;
204
 
  static const size_t MAX_STRING_LENGTH= 32;   // +32 to make my_snprintf_{8bit|ucs2} happy
205
 
 
206
 
  bool check(bool not_zero_date, uint32_t flags, type::cut_t &was_cut) const;
207
 
 
208
 
  inline bool isValidEpoch() const
209
 
  {
210
 
    if ((year < TIMESTAMP_MIN_YEAR) or (year == TIMESTAMP_MIN_YEAR && (month < 12 || day < 31)))
211
 
    {
212
 
      return false;
213
 
    }
214
 
 
215
 
    return true;
216
 
  }
217
 
};
218
 
 
219
 
}
 
90
typedef struct st_drizzle_time
 
91
{
 
92
  unsigned int  year, month, day, hour, minute, second;
 
93
  unsigned long second_part;
 
94
  bool       neg;
 
95
  enum enum_drizzle_timestamp_type time_type;
 
96
} DRIZZLE_TIME;
 
97
 
 
98
 
 
99
bool check_date(const DRIZZLE_TIME *ltime, bool not_zero_date,
 
100
                   uint32_t flags, int *was_cut);
 
101
enum enum_drizzle_timestamp_type
 
102
str_to_datetime(const char *str, uint32_t length, DRIZZLE_TIME *l_time,
 
103
                uint32_t flags, int *was_cut);
 
104
int64_t number_to_datetime(int64_t nr, DRIZZLE_TIME *time_res,
 
105
                            uint32_t flags, int *was_cut);
 
106
uint64_t TIME_to_uint64_t_datetime(const DRIZZLE_TIME *);
 
107
uint64_t TIME_to_uint64_t(const DRIZZLE_TIME *);
 
108
 
 
109
 
 
110
bool str_to_time(const char *str,uint32_t length, DRIZZLE_TIME *l_time,
 
111
                 int *warning);
220
112
 
221
113
long calc_daynr(uint32_t year,uint32_t month,uint32_t day);
222
114
uint32_t calc_days_in_year(uint32_t year);
224
116
 
225
117
void init_time(void);
226
118
 
 
119
 
 
120
/*
 
121
  Function to check sanity of a TIMESTAMP value
 
122
 
 
123
  DESCRIPTION
 
124
    Check if a given DRIZZLE_TIME value fits in TIMESTAMP range.
 
125
    This function doesn't make precise check, but rather a rough
 
126
    estimate.
 
127
 
 
128
  RETURN VALUES
 
129
    false   The value seems sane
 
130
    true    The DRIZZLE_TIME value is definitely out of range
 
131
*/
 
132
 
 
133
static inline bool validate_timestamp_range(const DRIZZLE_TIME *t)
 
134
{
 
135
  if ((t->year > TIMESTAMP_MAX_YEAR || t->year < TIMESTAMP_MIN_YEAR) ||
 
136
      (t->year == TIMESTAMP_MAX_YEAR && (t->month > 1 || t->day > 19)) ||
 
137
      (t->year == TIMESTAMP_MIN_YEAR && (t->month < 12 || t->day < 31)))
 
138
    return false;
 
139
 
 
140
  return true;
 
141
}
 
142
 
 
143
time_t
 
144
my_system_gmt_sec(const DRIZZLE_TIME *t, long *my_timezone,
 
145
                  bool *in_dst_time_gap);
 
146
 
 
147
void set_zero_time(DRIZZLE_TIME *tm, enum enum_drizzle_timestamp_type time_type);
 
148
 
 
149
/*
 
150
  Required buffer length for my_time_to_str, my_date_to_str,
 
151
  my_datetime_to_str and TIME_to_string functions. Note, that the
 
152
  caller is still responsible to check that given TIME structure
 
153
  has values in valid ranges, otherwise size of the buffer could
 
154
  be not enough. We also rely on the fact that even wrong values
 
155
  sent using binary protocol fit in this buffer.
 
156
*/
 
157
#define MAX_DATE_STRING_REP_LENGTH 30
 
158
 
 
159
int my_date_to_str(const DRIZZLE_TIME *l_time, char *to);
 
160
int my_datetime_to_str(const DRIZZLE_TIME *l_time, char *to);
 
161
int my_TIME_to_str(const DRIZZLE_TIME *l_time, char *to);
 
162
 
227
163
/*
228
164
  Available interval types used in any statement.
229
165
 
249
185
  INTERVAL_MINUTE_MICROSECOND, INTERVAL_SECOND_MICROSECOND, INTERVAL_LAST
250
186
};
251
187
 
 
188
extern uint64_t my_getsystime(void);
 
189
extern uint64_t my_micro_time(void);
 
190
extern uint64_t my_micro_time_and_time(time_t *time_arg);
 
191
 
252
192
} /* namespace drizzled */
253
193
 
254
 
#endif /* DRIZZLED_TYPE_TIME_H */
 
194
#endif /* DRIZZLED_DRIZZLE_TIME_H */