1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
4
* Copyright (C) 2008-2009 Sun Microsystems
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
21
21
/* Functions to handle date and time */
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"
29
30
/* Some functions to calculate dates */
33
/* Calc weekday from daynr */
34
/* Returns 0 for monday, 1 for tuesday .... */
36
34
int calc_weekday(long daynr,bool sunday_first_day_of_week)
38
36
return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
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
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.
53
If set Week is in range 1-53.
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
59
WEEK_FIRST_WEEKDAY (2) If not set Weeks are numbered according
61
If set The week that contains the first
62
'first-day-of-week' is week 1.
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
70
40
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
76
46
bool week_year= test(week_behaviour & WEEK_YEAR);
77
47
bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
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;
82
52
if (l_time->month == 1 && l_time->day <= 7-weekday)
110
/* Change a daynr to year, month and day */
111
/* Daynr 0 is returned as date 00.00.00 */
113
void get_date_from_daynr(long daynr,uint32_t *ret_year,uint32_t *ret_month,
81
void get_date_from_daynr(long daynr,
116
86
uint32_t year,temp,leap_day,day_of_year,days_in_year;
117
87
unsigned char *month_pos;
155
Convert a timestamp string to a DRIZZLE_TIME value and produce a warning
156
if string was truncated during conversion.
159
See description of str_to_datetime() for more information.
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,
128
DRIZZLE_TIME *l_time,
182
Convert a time string to a DRIZZLE_TIME struct and produce a warning
183
if string was cut during conversion.
186
See str_to_time() for more info.
189
148
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
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,
237
193
enum enum_drizzle_timestamp_type time_type,
238
194
const char *field_name)
276
232
ER_TRUNCATED_WRONG_VALUE, warn_buff);
280
Calculate difference between two datetime values as seconds + microseconds.
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.
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).
299
Returns sign of difference.
300
1 means negative result
301
0 means positive result
306
237
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,