~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/drizzle_time.h

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:30:27 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103033027-lskb6gxwwforfz71
fix docs warning: underline/overline too short for replace.rst

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)
68
65
#define TIME_MAX_SECOND 59
69
66
#define TIME_MAX_VALUE (TIME_MAX_HOUR*10000 + TIME_MAX_MINUTE*100 + \
70
67
                        TIME_MAX_SECOND)
 
68
#define TIME_MAX_VALUE_SECONDS (TIME_MAX_HOUR * 3600L + \
 
69
                                TIME_MAX_MINUTE * 60L + TIME_MAX_SECOND)
 
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
 
71
77
 
72
78
/*
73
79
  Structure which is used to represent datetime values inside Drizzle.
76
82
  month <= 12, day <= 31, hour <= 23, hour <= 59, hour <= 59. Many functions
77
83
  in server such as my_system_gmt_sec() or make_time() family of functions
78
84
  rely on this (actually now usage of make_*() family relies on a bit weaker
79
 
  restriction). Also functions that produce type::Time as result ensure this.
 
85
  restriction). Also functions that produce DRIZZLE_TIME as result ensure this.
80
86
  There is one exception to this rule though if this structure holds time
81
87
  value (time_type == DRIZZLE_TIMESTAMP_TIME) days and hour member can hold
82
88
  bigger values.
83
89
*/
84
 
namespace type {
85
 
 
86
 
enum timestamp_t
87
 
{
88
 
  DRIZZLE_TIMESTAMP_NONE= -2, DRIZZLE_TIMESTAMP_ERROR= -1,
89
 
  DRIZZLE_TIMESTAMP_DATE= 0, DRIZZLE_TIMESTAMP_DATETIME= 1, DRIZZLE_TIMESTAMP_TIME= 2
90
 
};
91
 
 
92
 
enum cut_t
93
 
{
94
 
  VALID= 0,
95
 
  CUT= 1,
96
 
  INVALID= 2
97
 
};
98
 
 
99
 
/*
100
 
  datatime_t while being stored in an integer is actually a formatted value.
101
 
*/
102
 
typedef int64_t datetime_t;
103
 
typedef int64_t date_t;
104
 
 
105
 
inline bool is_valid(const datetime_t &value)
106
 
{
107
 
  if (value == -1L)
108
 
    return false;
109
 
 
110
 
  return true;
111
 
}
112
 
 
113
 
class Time
114
 
{
115
 
public:
116
 
  typedef uint32_t usec_t;
117
 
  typedef int64_t epoch_t;
118
 
 
119
 
  Time()
120
 
  {
121
 
    reset();
122
 
  }
123
 
 
124
 
  Time(uint32_t year_arg,
125
 
       uint32_t month_arg,
126
 
       uint32_t day_arg,
127
 
       uint32_t hour_arg,
128
 
       uint32_t minute_arg,
129
 
       uint32_t second_arg,
130
 
       usec_t second_part_arg,
131
 
       timestamp_t type_arg) :
132
 
    year(year_arg),
133
 
    month(month_arg),
134
 
    day(day_arg),
135
 
    hour(hour_arg),
136
 
    minute(minute_arg),
137
 
    second(second_arg),
138
 
    second_part(second_part_arg),
139
 
    neg(false),
140
 
    time_type(type_arg),
141
 
    _is_local_time(false)
142
 
  {
143
 
  }
144
 
 
145
 
  Time(uint32_t hour_arg,
146
 
       uint32_t minute_arg,
147
 
       uint32_t second_arg,
148
 
       usec_t second_part_arg,
149
 
       bool neg_arg) :
150
 
    year(0),
151
 
    month(0),
152
 
    day(0),
153
 
    hour(hour_arg),
154
 
    minute(minute_arg),
155
 
    second(second_arg),
156
 
    second_part(second_part_arg),
157
 
    neg(neg_arg),
158
 
    time_type(DRIZZLE_TIMESTAMP_TIME),
159
 
    _is_local_time(false)
160
 
  {
161
 
  }
162
 
 
163
 
  uint32_t year, month, day, hour, minute, second;
164
 
  usec_t second_part;
165
 
  bool neg;
166
 
  timestamp_t time_type;
167
 
  bool _is_local_time;
168
 
 
169
 
  void reset()
170
 
  {
171
 
    year= month= day= hour= minute= second= second_part= 0;
172
 
    neg= false;
173
 
    time_type= DRIZZLE_TIMESTAMP_DATE;
174
 
    _is_local_time= false;
175
 
  }
176
 
 
177
 
  timestamp_t type() const
178
 
  {
179
 
    return time_type;
180
 
  }
181
 
 
182
 
  void convert(drizzled::String &str, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
183
 
  void convert(char *str, size_t &to_length, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
184
 
  void convert(datetime_t &datetime, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
185
 
  void convert(datetime_t &ret, int64_t nr, uint32_t flags);
186
 
  void convert(datetime_t &ret, int64_t nr, uint32_t flags, type::cut_t &was_cut);
187
 
  void convert(type::Time::epoch_t &epoch, long *my_timezone,
188
 
               bool *in_dst_time_gap, bool skip_timezone= false) const;
189
 
 
190
 
  void truncate(const timestamp_t arg);
191
 
 
192
 
  bool store(const char *str,uint32_t length, int &warning, type::timestamp_t arg= DRIZZLE_TIMESTAMP_TIME);
193
 
  type::timestamp_t store(const char *str, uint32_t length, uint32_t flags, type::cut_t &was_cut);
194
 
  type::timestamp_t store(const char *str, uint32_t length, uint32_t flags);
195
 
  void store(const type::Time::epoch_t &from, bool use_localtime= false);
196
 
  void store(const type::Time::epoch_t &from, const usec_t &from_fractional_seconds, bool use_localtime= false);
197
 
  void store(const struct tm &from);
198
 
  void store(const struct timeval &from);
199
 
 
200
 
 
201
 
  static const uint32_t FRACTIONAL_DIGITS= 1000000;
202
 
  static const size_t MAX_STRING_LENGTH= 32;   // +32 to make my_snprintf_{8bit|ucs2} happy
203
 
 
204
 
  bool check(bool not_zero_date, uint32_t flags, type::cut_t &was_cut) const;
205
 
 
206
 
  inline bool isValidEpoch() const
207
 
  {
208
 
    if ((year < TIMESTAMP_MIN_YEAR) or (year == TIMESTAMP_MIN_YEAR && (month < 12 || day < 31)))
209
 
    {
210
 
      return false;
211
 
    }
212
 
 
213
 
    return true;
214
 
  }
215
 
};
216
 
 
217
 
}
 
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);
218
112
 
219
113
long calc_daynr(uint32_t year,uint32_t month,uint32_t day);
220
114
uint32_t calc_days_in_year(uint32_t year);
222
116
 
223
117
void init_time(void);
224
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
 
225
163
/*
226
164
  Available interval types used in any statement.
227
165
 
247
185
  INTERVAL_MINUTE_MICROSECOND, INTERVAL_SECOND_MICROSECOND, INTERVAL_LAST
248
186
};
249
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
 
250
192
} /* namespace drizzled */
251
193
 
252
 
#endif /* DRIZZLED_TYPE_TIME_H */
 
194
#endif /* DRIZZLED_DRIZZLE_TIME_H */