~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/time_functions.cc

  • Committer: Jay Pipes
  • Date: 2009-12-08 17:46:38 UTC
  • mfrom: (1240 staging)
  • mto: This revision was merged to the branch mainline in revision 1245.
  • Revision ID: jpipes@serialcoder-20091208174638-exwaq2vbq0bzvyze
Merge trunk

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 Sun Microsystems
 
4
 *  Copyright (C) 2008-2009 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 <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>
 
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"
 
28
#include "drizzled/time_functions.h"
28
29
 
29
30
/* Some functions to calculate dates */
30
31
 
31
32
#ifndef TESTTIME
32
33
 
33
 
        /* Calc weekday from daynr */
34
 
        /* Returns 0 for monday, 1 for tuesday .... */
35
 
 
36
34
int calc_weekday(long daynr,bool sunday_first_day_of_week)
37
35
{
38
36
  return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
39
37
}
40
38
 
41
 
/*
42
 
  The bits in week_format has the following meaning:
43
 
   WEEK_MONDAY_FIRST (0)  If not set    Sunday is first day of week
44
 
                          If set        Monday is first day of week
45
 
   WEEK_YEAR (1)          If not set    Week is in range 0-53
46
 
 
47
 
        Week 0 is returned for the the last week of the previous year (for
48
 
        a date at start of january) In this case one can get 53 for the
49
 
        first week of next year.  This flag ensures that the week is
50
 
        relevant for the given year. Note that this flag is only
51
 
        releveant if WEEK_JANUARY is not set.
52
 
 
53
 
                          If set         Week is in range 1-53.
54
 
 
55
 
        In this case one may get week 53 for a date in January (when
56
 
        the week is that last week of previous year) and week 1 for a
57
 
        date in December.
58
 
 
59
 
  WEEK_FIRST_WEEKDAY (2)  If not set    Weeks are numbered according
60
 
                                        to ISO 8601:1988
61
 
                          If set        The week that contains the first
62
 
                                        'first-day-of-week' is week 1.
63
 
 
64
 
        ISO 8601:1988 means that if the week containing January 1 has
65
 
        four or more days in the new year, then it is week 1;
66
 
        Otherwise it is the last week of the previous year, and the
67
 
        next week is week 1.
68
 
*/
69
39
 
70
40
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
71
41
{
76
46
  bool week_year= test(week_behaviour & WEEK_YEAR);
77
47
  bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
78
48
 
79
 
  uint32_t weekday=calc_weekday(first_daynr, !monday_first);
 
49
  uint32_t weekday= calc_weekday(first_daynr, !monday_first);
80
50
  *year=l_time->year;
81
51
 
82
52
  if (l_time->month == 1 && l_time->day <= 7-weekday)
107
77
  return days/7+1;
108
78
}
109
79
 
110
 
        /* Change a daynr to year, month and day */
111
 
        /* Daynr 0 is returned as date 00.00.00 */
112
80
 
113
 
void get_date_from_daynr(long daynr,uint32_t *ret_year,uint32_t *ret_month,
114
 
                         uint32_t *ret_day)
 
81
void get_date_from_daynr(long daynr,
 
82
                         uint32_t *ret_year,
 
83
                         uint32_t *ret_month,
 
84
                                           uint32_t *ret_day)
115
85
{
116
86
  uint32_t year,temp,leap_day,day_of_year,days_in_year;
117
87
  unsigned char *month_pos;
151
121
  return;
152
122
}
153
123
 
154
 
/*
155
 
  Convert a timestamp string to a DRIZZLE_TIME value and produce a warning
156
 
  if string was truncated during conversion.
157
 
 
158
 
  NOTE
159
 
    See description of str_to_datetime() for more information.
160
 
*/
161
124
 
162
125
enum enum_drizzle_timestamp_type
163
 
str_to_datetime_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time,
 
126
str_to_datetime_with_warn(const char *str, 
 
127
                          uint32_t length, 
 
128
                          DRIZZLE_TIME *l_time,
164
129
                          uint32_t flags)
165
130
{
166
131
  int was_cut;
178
143
  return ts_type;
179
144
}
180
145
 
181
 
/*
182
 
  Convert a time string to a DRIZZLE_TIME struct and produce a warning
183
 
  if string was cut during conversion.
184
146
 
185
 
  NOTE
186
 
    See str_to_time() for more info.
187
 
*/
188
147
bool
189
148
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
190
149
{
197
156
}
198
157
 
199
158
 
200
 
/*
201
 
  Convert a system time structure to TIME
202
 
*/
203
 
 
204
159
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
205
160
{
206
161
  to->neg=0;
231
186
}
232
187
 
233
188
 
234
 
void make_truncated_value_warning(Session *session, DRIZZLE_ERROR::enum_warning_level level,
 
189
void make_truncated_value_warning(Session *session, 
 
190
                                  DRIZZLE_ERROR::enum_warning_level level,
235
191
                                  const char *str_val,
236
 
                                  uint32_t str_length,
 
192
                                                          uint32_t str_length,
237
193
                                  enum enum_drizzle_timestamp_type time_type,
238
194
                                  const char *field_name)
239
195
{
276
232
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
277
233
}
278
234
 
279
 
/*
280
 
  Calculate difference between two datetime values as seconds + microseconds.
281
 
 
282
 
  SYNOPSIS
283
 
    calc_time_diff()
284
 
      l_time1         - TIME/DATE/DATETIME value
285
 
      l_time2         - TIME/DATE/DATETIME value
286
 
      l_sign          - 1 absolute values are substracted,
287
 
                        -1 absolute values are added.
288
 
      seconds_out     - Out parameter where difference between
289
 
                        l_time1 and l_time2 in seconds is stored.
290
 
      microseconds_out- Out parameter where microsecond part of difference
291
 
                        between l_time1 and l_time2 is stored.
292
 
 
293
 
  NOTE
294
 
    This function calculates difference between l_time1 and l_time2 absolute
295
 
    values. So one should set l_sign and correct result if he want to take
296
 
    signs into account (i.e. for DRIZZLE_TIME values).
297
 
 
298
 
  RETURN VALUES
299
 
    Returns sign of difference.
300
 
    1 means negative result
301
 
    0 means positive result
302
 
 
303
 
*/
304
235
 
305
236
bool
306
237
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,