21
21
/* Functions to handle date and time */
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"
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>
33
29
/* Some functions to calculate dates */
34
Name description of interval names used in statements.
36
'interval_type_to_name' is ordered and sorted on interval size and
38
Order of elements in 'interval_type_to_name' should correspond to
39
the order of elements in 'interval_type' enum
41
See also interval_type, interval_names
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")}
67
/* Calc weekday from daynr */
68
/* Returns 0 for monday, 1 for tuesday .... */
36
70
int calc_weekday(long daynr,bool sunday_first_day_of_week)
38
72
return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
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
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.
87
If set Week is in range 1-53.
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
93
WEEK_FIRST_WEEKDAY (2) If not set Weeks are numbered according
95
If set The week that contains the first
96
'first-day-of-week' is week 1.
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
42
104
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
170
247
to->second= (int) from->tm_sec;
250
void calc_time_from_sec(DRIZZLE_TIME *to, long seconds, long microseconds)
253
// to->neg is not cleared, it may already be set to a useful value
254
to->time_type= DRIZZLE_TIMESTAMP_TIME;
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;
265
void make_time(const DRIZZLE_TIME *l_time, String *str)
267
str->alloc(MAX_DATE_STRING_REP_LENGTH);
268
uint32_t length= (uint32_t) my_time_to_str(l_time, str->c_ptr());
270
str->set_charset(&my_charset_bin);
173
274
void make_date(const DRIZZLE_TIME *l_time, String *str)
175
276
str->alloc(MAX_DATE_STRING_REP_LENGTH);
234
334
ER_TRUNCATED_WRONG_VALUE, warn_buff);
338
Calculate difference between two datetime values as seconds + microseconds.
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.
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).
357
Returns sign of difference.
358
1 means negative result
359
0 means positive result
239
364
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,