summaryrefslogtreecommitdiff
path: root/sql/item_timefunc.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/item_timefunc.cc')
-rw-r--r--sql/item_timefunc.cc73
1 files changed, 55 insertions, 18 deletions
diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc
index a5372a4ae60..9799c5814c4 100644
--- a/sql/item_timefunc.cc
+++ b/sql/item_timefunc.cc
@@ -526,8 +526,11 @@ bool make_date_time(DATE_TIME_FORMAT *format, TIME *l_time,
uint year;
if (type == TIMESTAMP_TIME)
return 1;
- length= int10_to_str(calc_week(l_time, 0, (*ptr) == 'U', &year),
- intbuff, 10) - intbuff;
+ length= int10_to_str(calc_week(l_time,
+ (*ptr) == 'U' ?
+ WEEK_FIRST_WEEKDAY : WEEK_MONDAY_FIRST,
+ &year),
+ intbuff, 10) - intbuff;
str->append_with_prefill(intbuff, length, 2, '0');
}
break;
@@ -537,8 +540,12 @@ bool make_date_time(DATE_TIME_FORMAT *format, TIME *l_time,
uint year;
if (type == TIMESTAMP_TIME)
return 1;
- length= int10_to_str(calc_week(l_time, 1, (*ptr) == 'V', &year),
- intbuff, 10) - intbuff;
+ length= int10_to_str(calc_week(l_time,
+ ((*ptr) == 'V' ?
+ (WEEK_YEAR | WEEK_FIRST_WEEKDAY) :
+ (WEEK_YEAR | WEEK_MONDAY_FIRST)),
+ &year),
+ intbuff, 10) - intbuff;
str->append_with_prefill(intbuff, length, 2, '0');
}
break;
@@ -548,7 +555,11 @@ bool make_date_time(DATE_TIME_FORMAT *format, TIME *l_time,
uint year;
if (type == TIMESTAMP_TIME)
return 1;
- (void) calc_week(l_time, 1, (*ptr) == 'X', &year);
+ (void) calc_week(l_time,
+ ((*ptr) == 'X' ?
+ WEEK_YEAR | WEEK_FIRST_WEEKDAY :
+ WEEK_YEAR | WEEK_MONDAY_FIRST),
+ &year);
length= int10_to_str(year, intbuff, 10) - intbuff;
str->append_with_prefill(intbuff, length, 4, '0');
}
@@ -718,27 +729,51 @@ longlong Item_func_second::val_int()
}
-/*
- Returns the week of year.
+uint week_mode(uint mode)
+{
+ uint week_format= (mode & 7);
+ if (!(week_format & WEEK_MONDAY_FIRST))
+ week_format^= WEEK_FIRST_WEEKDAY;
+ return week_format;
+}
- The bits in week_format has the following meaning:
- 0 If not set: USA format: Sunday is first day of week
- If set: ISO format: Monday is first day of week
- 1 If not set: Week is in range 0-53
- If set Week is in range 1-53.
+/*
+ The bits in week_format(for calc_week() function) has the following meaning:
+ WEEK_MONDAY_FIRST (0) If not set Sunday is first day of week
+ If set Monday is first day of week
+ WEEK_YEAR (1) If not set Week is in range 0-53
+
+ Week 0 is returned for the the last week of the previous year (for
+ a date at start of january) In this case one can get 53 for the
+ first week of next year. This flag ensures that the week is
+ relevant for the given year. Note that this flag is only
+ releveant if WEEK_JANUARY is not set.
+
+ If set Week is in range 1-53.
+
+ In this case one may get week 53 for a date in January (when
+ the week is that last week of previous year) and week 1 for a
+ date in December.
+
+ WEEK_FIRST_WEEKDAY (2) If not set Weeks are numbered according
+ to ISO 8601:1988
+ If set The week that contains the first
+ 'first-day-of-week' is week 1.
+
+ ISO 8601:1988 means that if the week containing January 1 has
+ four or more days in the new year, then it is week 1;
+ Otherwise it is the last week of the previous year, and the
+ next week is week 1.
*/
longlong Item_func_week::val_int()
{
uint year;
- uint week_format;
TIME ltime;
if (get_arg0_date(&ltime,0))
return 0;
- week_format= (uint) args[1]->val_int();
- return (longlong) calc_week(&ltime,
- (week_format & 2) != 0,
- (week_format & 1) == 0,
+ return (longlong) calc_week(&ltime,
+ week_mode((uint) args[1]->val_int()),
&year);
}
@@ -749,7 +784,9 @@ longlong Item_func_yearweek::val_int()
TIME ltime;
if (get_arg0_date(&ltime,0))
return 0;
- week=calc_week(&ltime, 1, (args[1]->val_int() & 1) == 0, &year);
+ week= calc_week(&ltime,
+ (week_mode((uint) args[1]->val_int()) | WEEK_YEAR),
+ &year);
return week+year*100;
}