~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time.cc

  • Committer: Jay Pipes
  • Date: 2009-09-21 14:33:44 UTC
  • mfrom: (1126.10.26 dtrace-probes)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: jpipes@serialcoder-20090921143344-jnarp7gcn6zmg19c
Merge fixes from Trond and Padraig on dtrace probes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
20
20
 
21
21
/* Functions to handle date and time */
22
22
 
23
 
#include "config.h"
24
 
#include "drizzled/error.h"
25
 
#include "drizzled/util/test.h"
26
 
#include "drizzled/tztime.h"
27
 
#include "drizzled/session.h"
28
 
#include "drizzled/time_functions.h"
29
 
 
30
 
namespace drizzled
31
 
{
 
23
#include <drizzled/server_includes.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/util/test.h>
 
26
#include <drizzled/tztime.h>
 
27
#include <drizzled/session.h>
32
28
 
33
29
/* Some functions to calculate dates */
34
30
 
 
31
#ifndef TESTTIME
 
32
 
 
33
/*
 
34
  Name description of interval names used in statements.
 
35
 
 
36
  'interval_type_to_name' is ordered and sorted on interval size and
 
37
  interval complexity.
 
38
  Order of elements in 'interval_type_to_name' should correspond to
 
39
  the order of elements in 'interval_type' enum
 
40
 
 
41
  See also interval_type, interval_names
 
42
*/
 
43
 
 
44
LEX_STRING interval_type_to_name[INTERVAL_LAST] = {
 
45
  { C_STRING_WITH_LEN("YEAR")},
 
46
  { C_STRING_WITH_LEN("QUARTER")},
 
47
  { C_STRING_WITH_LEN("MONTH")},
 
48
  { C_STRING_WITH_LEN("WEEK")},
 
49
  { C_STRING_WITH_LEN("DAY")},
 
50
  { C_STRING_WITH_LEN("HOUR")},
 
51
  { C_STRING_WITH_LEN("MINUTE")},
 
52
  { C_STRING_WITH_LEN("SECOND")},
 
53
  { C_STRING_WITH_LEN("MICROSECOND")},
 
54
  { C_STRING_WITH_LEN("YEAR_MONTH")},
 
55
  { C_STRING_WITH_LEN("DAY_HOUR")},
 
56
  { C_STRING_WITH_LEN("DAY_MINUTE")},
 
57
  { C_STRING_WITH_LEN("DAY_SECOND")},
 
58
  { C_STRING_WITH_LEN("HOUR_MINUTE")},
 
59
  { C_STRING_WITH_LEN("HOUR_SECOND")},
 
60
  { C_STRING_WITH_LEN("MINUTE_SECOND")},
 
61
  { C_STRING_WITH_LEN("DAY_MICROSECOND")},
 
62
  { C_STRING_WITH_LEN("HOUR_MICROSECOND")},
 
63
  { C_STRING_WITH_LEN("MINUTE_MICROSECOND")},
 
64
  { C_STRING_WITH_LEN("SECOND_MICROSECOND")}
 
65
};
 
66
 
 
67
        /* Calc weekday from daynr */
 
68
        /* Returns 0 for monday, 1 for tuesday .... */
35
69
 
36
70
int calc_weekday(long daynr,bool sunday_first_day_of_week)
37
71
{
38
72
  return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
39
73
}
40
74
 
 
75
/*
 
76
  The bits in week_format has the following meaning:
 
77
   WEEK_MONDAY_FIRST (0)  If not set    Sunday is first day of week
 
78
                          If set        Monday is first day of week
 
79
   WEEK_YEAR (1)          If not set    Week is in range 0-53
 
80
 
 
81
        Week 0 is returned for the the last week of the previous year (for
 
82
        a date at start of january) In this case one can get 53 for the
 
83
        first week of next year.  This flag ensures that the week is
 
84
        relevant for the given year. Note that this flag is only
 
85
        releveant if WEEK_JANUARY is not set.
 
86
 
 
87
                          If set         Week is in range 1-53.
 
88
 
 
89
        In this case one may get week 53 for a date in January (when
 
90
        the week is that last week of previous year) and week 1 for a
 
91
        date in December.
 
92
 
 
93
  WEEK_FIRST_WEEKDAY (2)  If not set    Weeks are numbered according
 
94
                                        to ISO 8601:1988
 
95
                          If set        The week that contains the first
 
96
                                        'first-day-of-week' is week 1.
 
97
 
 
98
        ISO 8601:1988 means that if the week containing January 1 has
 
99
        four or more days in the new year, then it is week 1;
 
100
        Otherwise it is the last week of the previous year, and the
 
101
        next week is week 1.
 
102
*/
41
103
 
42
104
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
43
105
{
48
110
  bool week_year= test(week_behaviour & WEEK_YEAR);
49
111
  bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
50
112
 
51
 
  uint32_t weekday= calc_weekday(first_daynr, !monday_first);
 
113
  uint32_t weekday=calc_weekday(first_daynr, !monday_first);
52
114
  *year=l_time->year;
53
115
 
54
116
  if (l_time->month == 1 && l_time->day <= 7-weekday)
79
141
  return days/7+1;
80
142
}
81
143
 
 
144
        /* Change a daynr to year, month and day */
 
145
        /* Daynr 0 is returned as date 00.00.00 */
82
146
 
83
 
void get_date_from_daynr(long daynr,
84
 
                         uint32_t *ret_year,
85
 
                         uint32_t *ret_month,
86
 
                                           uint32_t *ret_day)
 
147
void get_date_from_daynr(long daynr,uint32_t *ret_year,uint32_t *ret_month,
 
148
                         uint32_t *ret_day)
87
149
{
88
150
  uint32_t year,temp,leap_day,day_of_year,days_in_year;
89
151
  unsigned char *month_pos;
123
185
  return;
124
186
}
125
187
 
 
188
/*
 
189
  Convert a timestamp string to a DRIZZLE_TIME value and produce a warning
 
190
  if string was truncated during conversion.
 
191
 
 
192
  NOTE
 
193
    See description of str_to_datetime() for more information.
 
194
*/
126
195
 
127
196
enum enum_drizzle_timestamp_type
128
 
str_to_datetime_with_warn(const char *str, 
129
 
                          uint32_t length, 
130
 
                          DRIZZLE_TIME *l_time,
 
197
str_to_datetime_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time,
131
198
                          uint32_t flags)
132
199
{
133
200
  int was_cut;
145
212
  return ts_type;
146
213
}
147
214
 
 
215
/*
 
216
  Convert a time string to a DRIZZLE_TIME struct and produce a warning
 
217
  if string was cut during conversion.
148
218
 
 
219
  NOTE
 
220
    See str_to_time() for more info.
 
221
*/
149
222
bool
150
223
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
151
224
{
158
231
}
159
232
 
160
233
 
 
234
/*
 
235
  Convert a system time structure to TIME
 
236
*/
 
237
 
161
238
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
162
239
{
163
240
  to->neg=0;
170
247
  to->second=   (int) from->tm_sec;
171
248
}
172
249
 
 
250
void calc_time_from_sec(DRIZZLE_TIME *to, long seconds, long microseconds)
 
251
{
 
252
  long t_seconds;
 
253
  // to->neg is not cleared, it may already be set to a useful value
 
254
  to->time_type= DRIZZLE_TIMESTAMP_TIME;
 
255
  to->year= 0;
 
256
  to->month= 0;
 
257
  to->day= 0;
 
258
  to->hour= seconds/3600L;
 
259
  t_seconds= seconds%3600L;
 
260
  to->minute= t_seconds/60L;
 
261
  to->second= t_seconds%60L;
 
262
  to->second_part= microseconds;
 
263
}
 
264
 
 
265
void make_time(const DRIZZLE_TIME *l_time, String *str)
 
266
{
 
267
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
 
268
  uint32_t length= (uint32_t) my_time_to_str(l_time, str->c_ptr());
 
269
  str->length(length);
 
270
  str->set_charset(&my_charset_bin);
 
271
}
 
272
 
 
273
 
173
274
void make_date(const DRIZZLE_TIME *l_time, String *str)
174
275
{
175
276
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
188
289
}
189
290
 
190
291
 
191
 
void make_truncated_value_warning(Session *session, 
192
 
                                  DRIZZLE_ERROR::enum_warning_level level,
 
292
void make_truncated_value_warning(Session *session, DRIZZLE_ERROR::enum_warning_level level,
193
293
                                  const char *str_val,
194
 
                                                          uint32_t str_length,
 
294
                                  uint32_t str_length,
195
295
                                  enum enum_drizzle_timestamp_type time_type,
196
296
                                  const char *field_name)
197
297
{
234
334
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
235
335
}
236
336
 
 
337
/*
 
338
  Calculate difference between two datetime values as seconds + microseconds.
 
339
 
 
340
  SYNOPSIS
 
341
    calc_time_diff()
 
342
      l_time1         - TIME/DATE/DATETIME value
 
343
      l_time2         - TIME/DATE/DATETIME value
 
344
      l_sign          - 1 absolute values are substracted,
 
345
                        -1 absolute values are added.
 
346
      seconds_out     - Out parameter where difference between
 
347
                        l_time1 and l_time2 in seconds is stored.
 
348
      microseconds_out- Out parameter where microsecond part of difference
 
349
                        between l_time1 and l_time2 is stored.
 
350
 
 
351
  NOTE
 
352
    This function calculates difference between l_time1 and l_time2 absolute
 
353
    values. So one should set l_sign and correct result if he want to take
 
354
    signs into account (i.e. for DRIZZLE_TIME values).
 
355
 
 
356
  RETURN VALUES
 
357
    Returns sign of difference.
 
358
    1 means negative result
 
359
    0 means positive result
 
360
 
 
361
*/
237
362
 
238
363
bool
239
364
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
284
409
  return neg;
285
410
}
286
411
 
287
 
} /* namespace drizzled */
 
412
 
 
413
/*
 
414
  Compares 2 DRIZZLE_TIME structures
 
415
 
 
416
  SYNOPSIS
 
417
    my_time_compare()
 
418
 
 
419
      a - first time
 
420
      b - second time
 
421
 
 
422
  RETURN VALUE
 
423
   -1   - a < b
 
424
    0   - a == b
 
425
    1   - a > b
 
426
 
 
427
  NOTES
 
428
    TIME.second_part is not considered during comparison
 
429
*/
 
430
 
 
431
int
 
432
my_time_compare(DRIZZLE_TIME *a, DRIZZLE_TIME *b)
 
433
{
 
434
  uint64_t a_t= TIME_to_uint64_t_datetime(a);
 
435
  uint64_t b_t= TIME_to_uint64_t_datetime(b);
 
436
 
 
437
  if (a_t > b_t)
 
438
    return 1;
 
439
  else if (a_t < b_t)
 
440
    return -1;
 
441
 
 
442
  return 0;
 
443
}
 
444
 
 
445
#endif