You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

988 lines
28 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
23 years ago
23 years ago
19 years ago
19 years ago
19 years ago
  1. /* Copyright (C) 2000-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Functions to handle date and time */
  13. #include "mysql_priv.h"
  14. #include <m_ctype.h>
  15. /* Some functions to calculate dates */
  16. #ifndef TESTTIME
  17. /*
  18. Name description of interval names used in statements.
  19. 'interval_type_to_name' is ordered and sorted on interval size and
  20. interval complexity.
  21. Order of elements in 'interval_type_to_name' should correspond to
  22. the order of elements in 'interval_type' enum
  23. See also interval_type, interval_names
  24. */
  25. LEX_STRING interval_type_to_name[INTERVAL_LAST] = {
  26. { C_STRING_WITH_LEN("YEAR")},
  27. { C_STRING_WITH_LEN("QUARTER")},
  28. { C_STRING_WITH_LEN("MONTH")},
  29. { C_STRING_WITH_LEN("WEEK")},
  30. { C_STRING_WITH_LEN("DAY")},
  31. { C_STRING_WITH_LEN("HOUR")},
  32. { C_STRING_WITH_LEN("MINUTE")},
  33. { C_STRING_WITH_LEN("SECOND")},
  34. { C_STRING_WITH_LEN("MICROSECOND")},
  35. { C_STRING_WITH_LEN("YEAR_MONTH")},
  36. { C_STRING_WITH_LEN("DAY_HOUR")},
  37. { C_STRING_WITH_LEN("DAY_MINUTE")},
  38. { C_STRING_WITH_LEN("DAY_SECOND")},
  39. { C_STRING_WITH_LEN("HOUR_MINUTE")},
  40. { C_STRING_WITH_LEN("HOUR_SECOND")},
  41. { C_STRING_WITH_LEN("MINUTE_SECOND")},
  42. { C_STRING_WITH_LEN("DAY_MICROSECOND")},
  43. { C_STRING_WITH_LEN("HOUR_MICROSECOND")},
  44. { C_STRING_WITH_LEN("MINUTE_MICROSECOND")},
  45. { C_STRING_WITH_LEN("SECOND_MICROSECOND")}
  46. };
  47. /* Calc weekday from daynr */
  48. /* Returns 0 for monday, 1 for tuesday .... */
  49. int calc_weekday(long daynr,bool sunday_first_day_of_week)
  50. {
  51. DBUG_ENTER("calc_weekday");
  52. DBUG_RETURN ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
  53. }
  54. /*
  55. The bits in week_format has the following meaning:
  56. WEEK_MONDAY_FIRST (0) If not set Sunday is first day of week
  57. If set Monday is first day of week
  58. WEEK_YEAR (1) If not set Week is in range 0-53
  59. Week 0 is returned for the the last week of the previous year (for
  60. a date at start of january) In this case one can get 53 for the
  61. first week of next year. This flag ensures that the week is
  62. relevant for the given year. Note that this flag is only
  63. releveant if WEEK_JANUARY is not set.
  64. If set Week is in range 1-53.
  65. In this case one may get week 53 for a date in January (when
  66. the week is that last week of previous year) and week 1 for a
  67. date in December.
  68. WEEK_FIRST_WEEKDAY (2) If not set Weeks are numbered according
  69. to ISO 8601:1988
  70. If set The week that contains the first
  71. 'first-day-of-week' is week 1.
  72. ISO 8601:1988 means that if the week containing January 1 has
  73. four or more days in the new year, then it is week 1;
  74. Otherwise it is the last week of the previous year, and the
  75. next week is week 1.
  76. */
  77. uint calc_week(MYSQL_TIME *l_time, uint week_behaviour, uint *year)
  78. {
  79. uint days;
  80. ulong daynr=calc_daynr(l_time->year,l_time->month,l_time->day);
  81. ulong first_daynr=calc_daynr(l_time->year,1,1);
  82. bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST);
  83. bool week_year= test(week_behaviour & WEEK_YEAR);
  84. bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
  85. uint weekday=calc_weekday(first_daynr, !monday_first);
  86. *year=l_time->year;
  87. if (l_time->month == 1 && l_time->day <= 7-weekday)
  88. {
  89. if (!week_year &&
  90. ((first_weekday && weekday != 0) ||
  91. (!first_weekday && weekday >= 4)))
  92. return 0;
  93. week_year= 1;
  94. (*year)--;
  95. first_daynr-= (days=calc_days_in_year(*year));
  96. weekday= (weekday + 53*7- days) % 7;
  97. }
  98. if ((first_weekday && weekday != 0) ||
  99. (!first_weekday && weekday >= 4))
  100. days= daynr - (first_daynr+ (7-weekday));
  101. else
  102. days= daynr - (first_daynr - weekday);
  103. if (week_year && days >= 52*7)
  104. {
  105. weekday= (weekday + calc_days_in_year(*year)) % 7;
  106. if ((!first_weekday && weekday < 4) ||
  107. (first_weekday && weekday == 0))
  108. {
  109. (*year)++;
  110. return 1;
  111. }
  112. }
  113. return days/7+1;
  114. }
  115. /* Change a daynr to year, month and day */
  116. /* Daynr 0 is returned as date 00.00.00 */
  117. void get_date_from_daynr(long daynr,uint *ret_year,uint *ret_month,
  118. uint *ret_day)
  119. {
  120. uint year,temp,leap_day,day_of_year,days_in_year;
  121. uchar *month_pos;
  122. DBUG_ENTER("get_date_from_daynr");
  123. if (daynr <= 365L || daynr >= 3652500)
  124. { /* Fix if wrong daynr */
  125. *ret_year= *ret_month = *ret_day =0;
  126. }
  127. else
  128. {
  129. year= (uint) (daynr*100 / 36525L);
  130. temp=(((year-1)/100+1)*3)/4;
  131. day_of_year=(uint) (daynr - (long) year * 365L) - (year-1)/4 +temp;
  132. while (day_of_year > (days_in_year= calc_days_in_year(year)))
  133. {
  134. day_of_year-=days_in_year;
  135. (year)++;
  136. }
  137. leap_day=0;
  138. if (days_in_year == 366)
  139. {
  140. if (day_of_year > 31+28)
  141. {
  142. day_of_year--;
  143. if (day_of_year == 31+28)
  144. leap_day=1; /* Handle leapyears leapday */
  145. }
  146. }
  147. *ret_month=1;
  148. for (month_pos= days_in_month ;
  149. day_of_year > (uint) *month_pos ;
  150. day_of_year-= *(month_pos++), (*ret_month)++)
  151. ;
  152. *ret_year=year;
  153. *ret_day=day_of_year+leap_day;
  154. }
  155. DBUG_VOID_RETURN;
  156. }
  157. /* Functions to handle periods */
  158. ulong convert_period_to_month(ulong period)
  159. {
  160. ulong a,b;
  161. if (period == 0)
  162. return 0L;
  163. if ((a=period/100) < YY_PART_YEAR)
  164. a+=2000;
  165. else if (a < 100)
  166. a+=1900;
  167. b=period%100;
  168. return a*12+b-1;
  169. }
  170. ulong convert_month_to_period(ulong month)
  171. {
  172. ulong year;
  173. if (month == 0L)
  174. return 0L;
  175. if ((year=month/12) < 100)
  176. {
  177. year+=(year < YY_PART_YEAR) ? 2000 : 1900;
  178. }
  179. return year*100+month%12+1;
  180. }
  181. /*
  182. Convert a timestamp string to a MYSQL_TIME value and produce a warning
  183. if string was truncated during conversion.
  184. NOTE
  185. See description of str_to_datetime() for more information.
  186. */
  187. timestamp_type
  188. str_to_datetime_with_warn(const char *str, uint length, MYSQL_TIME *l_time,
  189. uint flags)
  190. {
  191. int was_cut;
  192. THD *thd= current_thd;
  193. timestamp_type ts_type;
  194. ts_type= str_to_datetime(str, length, l_time,
  195. (flags | (thd->variables.sql_mode &
  196. (MODE_INVALID_DATES |
  197. MODE_NO_ZERO_DATE))),
  198. &was_cut);
  199. if (was_cut || ts_type <= MYSQL_TIMESTAMP_ERROR)
  200. make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  201. str, length, ts_type, NullS);
  202. return ts_type;
  203. }
  204. /*
  205. Convert a datetime from broken-down MYSQL_TIME representation to corresponding
  206. TIMESTAMP value.
  207. SYNOPSIS
  208. TIME_to_timestamp()
  209. thd - current thread
  210. t - datetime in broken-down representation,
  211. in_dst_time_gap - pointer to bool which is set to true if t represents
  212. value which doesn't exists (falls into the spring
  213. time-gap) or to false otherwise.
  214. RETURN
  215. Number seconds in UTC since start of Unix Epoch corresponding to t.
  216. 0 - t contains datetime value which is out of TIMESTAMP range.
  217. */
  218. my_time_t TIME_to_timestamp(THD *thd, const MYSQL_TIME *t, my_bool *in_dst_time_gap)
  219. {
  220. my_time_t timestamp;
  221. *in_dst_time_gap= 0;
  222. thd->time_zone_used= 1;
  223. timestamp= thd->variables.time_zone->TIME_to_gmt_sec(t, in_dst_time_gap);
  224. if (timestamp)
  225. {
  226. return timestamp;
  227. }
  228. /* If we are here we have range error. */
  229. return(0);
  230. }
  231. /*
  232. Convert a time string to a MYSQL_TIME struct and produce a warning
  233. if string was cut during conversion.
  234. NOTE
  235. See str_to_time() for more info.
  236. */
  237. bool
  238. str_to_time_with_warn(const char *str, uint length, MYSQL_TIME *l_time)
  239. {
  240. int warning;
  241. bool ret_val= str_to_time(str, length, l_time, &warning);
  242. if (ret_val || warning)
  243. make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  244. str, length, MYSQL_TIMESTAMP_TIME, NullS);
  245. return ret_val;
  246. }
  247. /*
  248. Convert a system time structure to TIME
  249. */
  250. void localtime_to_TIME(MYSQL_TIME *to, struct tm *from)
  251. {
  252. to->neg=0;
  253. to->second_part=0;
  254. to->year= (int) ((from->tm_year+1900) % 10000);
  255. to->month= (int) from->tm_mon+1;
  256. to->day= (int) from->tm_mday;
  257. to->hour= (int) from->tm_hour;
  258. to->minute= (int) from->tm_min;
  259. to->second= (int) from->tm_sec;
  260. }
  261. void calc_time_from_sec(MYSQL_TIME *to, long seconds, long microseconds)
  262. {
  263. long t_seconds;
  264. // to->neg is not cleared, it may already be set to a useful value
  265. to->time_type= MYSQL_TIMESTAMP_TIME;
  266. to->year= 0;
  267. to->month= 0;
  268. to->day= 0;
  269. to->hour= seconds/3600L;
  270. t_seconds= seconds%3600L;
  271. to->minute= t_seconds/60L;
  272. to->second= t_seconds%60L;
  273. to->second_part= microseconds;
  274. }
  275. /*
  276. Parse a format string specification
  277. SYNOPSIS
  278. parse_date_time_format()
  279. format_type Format of string (time, date or datetime)
  280. format_str String to parse
  281. format_length Length of string
  282. date_time_format Format to fill in
  283. NOTES
  284. Fills in date_time_format->positions for all date time parts.
  285. positions marks the position for a datetime element in the format string.
  286. The position array elements are in the following order:
  287. YYYY-DD-MM HH-MM-DD.FFFFFF AM
  288. 0 1 2 3 4 5 6 7
  289. If positions[0]= 5, it means that year will be the forth element to
  290. read from the parsed date string.
  291. RETURN
  292. 0 ok
  293. 1 error
  294. */
  295. bool parse_date_time_format(timestamp_type format_type,
  296. const char *format, uint format_length,
  297. DATE_TIME_FORMAT *date_time_format)
  298. {
  299. uint offset= 0, separators= 0;
  300. const char *ptr= format, *format_str;
  301. const char *end= ptr+format_length;
  302. uchar *dt_pos= date_time_format->positions;
  303. /* need_p is set if we are using AM/PM format */
  304. bool need_p= 0, allow_separator= 0;
  305. ulong part_map= 0, separator_map= 0;
  306. const char *parts[16];
  307. date_time_format->time_separator= 0;
  308. date_time_format->flag= 0; // For future
  309. /*
  310. Fill position with 'dummy' arguments to found out if a format tag is
  311. used twice (This limit's the format to 255 characters, but this is ok)
  312. */
  313. dt_pos[0]= dt_pos[1]= dt_pos[2]= dt_pos[3]=
  314. dt_pos[4]= dt_pos[5]= dt_pos[6]= dt_pos[7]= 255;
  315. for (; ptr != end; ptr++)
  316. {
  317. if (*ptr == '%' && ptr+1 != end)
  318. {
  319. uint position;
  320. LINT_INIT(position);
  321. switch (*++ptr) {
  322. case 'y': // Year
  323. case 'Y':
  324. position= 0;
  325. break;
  326. case 'c': // Month
  327. case 'm':
  328. position= 1;
  329. break;
  330. case 'd':
  331. case 'e':
  332. position= 2;
  333. break;
  334. case 'h':
  335. case 'I':
  336. case 'l':
  337. need_p= 1; // Need AM/PM
  338. /* Fall through */
  339. case 'k':
  340. case 'H':
  341. position= 3;
  342. break;
  343. case 'i':
  344. position= 4;
  345. break;
  346. case 's':
  347. case 'S':
  348. position= 5;
  349. break;
  350. case 'f':
  351. position= 6;
  352. if (dt_pos[5] != offset-1 || ptr[-2] != '.')
  353. return 1; // Wrong usage of %f
  354. break;
  355. case 'p': // AM/PM
  356. if (offset == 0) // Can't be first
  357. return 0;
  358. position= 7;
  359. break;
  360. default:
  361. return 1; // Unknown controll char
  362. }
  363. if (dt_pos[position] != 255) // Don't allow same tag twice
  364. return 1;
  365. parts[position]= ptr-1;
  366. /*
  367. If switching from time to date, ensure that all time parts
  368. are used
  369. */
  370. if (part_map && position <= 2 && !(part_map & (1 | 2 | 4)))
  371. offset=5;
  372. part_map|= (ulong) 1 << position;
  373. dt_pos[position]= offset++;
  374. allow_separator= 1;
  375. }
  376. else
  377. {
  378. /*
  379. Don't allow any characters in format as this could easily confuse
  380. the date reader
  381. */
  382. if (!allow_separator)
  383. return 1; // No separator here
  384. allow_separator= 0; // Don't allow two separators
  385. separators++;
  386. /* Store in separator_map which parts are punct characters */
  387. if (my_ispunct(&my_charset_latin1, *ptr))
  388. separator_map|= (ulong) 1 << (offset-1);
  389. else if (!my_isspace(&my_charset_latin1, *ptr))
  390. return 1;
  391. }
  392. }
  393. /* If no %f, specify it after seconds. Move %p up, if necessary */
  394. if ((part_map & 32) && !(part_map & 64))
  395. {
  396. dt_pos[6]= dt_pos[5] +1;
  397. parts[6]= parts[5]; // For later test in (need_p)
  398. if (dt_pos[6] == dt_pos[7]) // Move %p one step up if used
  399. dt_pos[7]++;
  400. }
  401. /*
  402. Check that we have not used a non legal format specifier and that all
  403. format specifiers have been used
  404. The last test is to ensure that %p is used if and only if
  405. it's needed.
  406. */
  407. if ((format_type == MYSQL_TIMESTAMP_DATETIME &&
  408. !test_all_bits(part_map, (1 | 2 | 4 | 8 | 16 | 32))) ||
  409. (format_type == MYSQL_TIMESTAMP_DATE && part_map != (1 | 2 | 4)) ||
  410. (format_type == MYSQL_TIMESTAMP_TIME &&
  411. !test_all_bits(part_map, 8 | 16 | 32)) ||
  412. !allow_separator || // %option should be last
  413. (need_p && dt_pos[6] +1 != dt_pos[7]) ||
  414. (need_p ^ (dt_pos[7] != 255)))
  415. return 1;
  416. if (dt_pos[6] != 255) // If fractional seconds
  417. {
  418. /* remove fractional seconds from later tests */
  419. uint pos= dt_pos[6] -1;
  420. /* Remove separator before %f from sep map */
  421. separator_map= ((separator_map & ((ulong) (1 << pos)-1)) |
  422. ((separator_map & ~((ulong) (1 << pos)-1)) >> 1));
  423. if (part_map & 64)
  424. {
  425. separators--; // There is always a separator
  426. need_p= 1; // force use of separators
  427. }
  428. }
  429. /*
  430. Remove possible separator before %p from sep_map
  431. (This can either be at position 3, 4, 6 or 7) h.m.d.%f %p
  432. */
  433. if (dt_pos[7] != 255)
  434. {
  435. if (need_p && parts[7] != parts[6]+2)
  436. separators--;
  437. }
  438. /*
  439. Calculate if %p is in first or last part of the datetime field
  440. At this point we have either %H-%i-%s %p 'year parts' or
  441. 'year parts' &H-%i-%s %p" as %f was removed above
  442. */
  443. offset= dt_pos[6] <= 3 ? 3 : 6;
  444. /* Remove separator before %p from sep map */
  445. separator_map= ((separator_map & ((ulong) (1 << offset)-1)) |
  446. ((separator_map & ~((ulong) (1 << offset)-1)) >> 1));
  447. format_str= 0;
  448. switch (format_type) {
  449. case MYSQL_TIMESTAMP_DATE:
  450. format_str= known_date_time_formats[INTERNAL_FORMAT].date_format;
  451. /* fall through */
  452. case MYSQL_TIMESTAMP_TIME:
  453. if (!format_str)
  454. format_str=known_date_time_formats[INTERNAL_FORMAT].time_format;
  455. /*
  456. If there is no separators, allow the internal format as we can read
  457. this. If separators are used, they must be between each part
  458. */
  459. if (format_length == 6 && !need_p &&
  460. !my_strnncoll(&my_charset_bin,
  461. (const uchar *) format, 6,
  462. (const uchar *) format_str, 6))
  463. return 0;
  464. if (separator_map == (1 | 2))
  465. {
  466. if (format_type == MYSQL_TIMESTAMP_TIME)
  467. {
  468. if (*(format+2) != *(format+5))
  469. break; // Error
  470. /* Store the character used for time formats */
  471. date_time_format->time_separator= *(format+2);
  472. }
  473. return 0;
  474. }
  475. break;
  476. case MYSQL_TIMESTAMP_DATETIME:
  477. /*
  478. If there is no separators, allow the internal format as we can read
  479. this. If separators are used, they must be between each part.
  480. Between DATE and TIME we also allow space as separator
  481. */
  482. if ((format_length == 12 && !need_p &&
  483. !my_strnncoll(&my_charset_bin,
  484. (const uchar *) format, 12,
  485. (const uchar*) known_date_time_formats[INTERNAL_FORMAT].datetime_format,
  486. 12)) ||
  487. (separators == 5 && separator_map == (1 | 2 | 8 | 16)))
  488. return 0;
  489. break;
  490. default:
  491. DBUG_ASSERT(1);
  492. break;
  493. }
  494. return 1; // Error
  495. }
  496. /*
  497. Create a DATE_TIME_FORMAT object from a format string specification
  498. SYNOPSIS
  499. date_time_format_make()
  500. format_type Format to parse (time, date or datetime)
  501. format_str String to parse
  502. format_length Length of string
  503. NOTES
  504. The returned object should be freed with my_free()
  505. RETURN
  506. NULL ponter: Error
  507. new object
  508. */
  509. DATE_TIME_FORMAT
  510. *date_time_format_make(timestamp_type format_type,
  511. const char *format_str, uint format_length)
  512. {
  513. DATE_TIME_FORMAT tmp;
  514. if (format_length && format_length < 255 &&
  515. !parse_date_time_format(format_type, format_str,
  516. format_length, &tmp))
  517. {
  518. tmp.format.str= (char*) format_str;
  519. tmp.format.length= format_length;
  520. return date_time_format_copy((THD *)0, &tmp);
  521. }
  522. return 0;
  523. }
  524. /*
  525. Create a copy of a DATE_TIME_FORMAT object
  526. SYNOPSIS
  527. date_and_time_format_copy()
  528. thd Set if variable should be allocated in thread mem
  529. format format to copy
  530. NOTES
  531. The returned object should be freed with my_free()
  532. RETURN
  533. NULL ponter: Error
  534. new object
  535. */
  536. DATE_TIME_FORMAT *date_time_format_copy(THD *thd, DATE_TIME_FORMAT *format)
  537. {
  538. DATE_TIME_FORMAT *new_format;
  539. ulong length= sizeof(*format) + format->format.length + 1;
  540. if (thd)
  541. new_format= (DATE_TIME_FORMAT *) thd->alloc(length);
  542. else
  543. new_format= (DATE_TIME_FORMAT *) my_malloc(length, MYF(MY_WME));
  544. if (new_format)
  545. {
  546. /* Put format string after current pos */
  547. new_format->format.str= (char*) (new_format+1);
  548. memcpy((char*) new_format->positions, (char*) format->positions,
  549. sizeof(format->positions));
  550. new_format->time_separator= format->time_separator;
  551. /* We make the string null terminated for easy printf in SHOW VARIABLES */
  552. memcpy((char*) new_format->format.str, format->format.str,
  553. format->format.length);
  554. new_format->format.str[format->format.length]= 0;
  555. new_format->format.length= format->format.length;
  556. }
  557. return new_format;
  558. }
  559. KNOWN_DATE_TIME_FORMAT known_date_time_formats[6]=
  560. {
  561. {"USA", "%m.%d.%Y", "%Y-%m-%d %H.%i.%s", "%h:%i:%s %p" },
  562. {"JIS", "%Y-%m-%d", "%Y-%m-%d %H:%i:%s", "%H:%i:%s" },
  563. {"ISO", "%Y-%m-%d", "%Y-%m-%d %H:%i:%s", "%H:%i:%s" },
  564. {"EUR", "%d.%m.%Y", "%Y-%m-%d %H.%i.%s", "%H.%i.%s" },
  565. {"INTERNAL", "%Y%m%d", "%Y%m%d%H%i%s", "%H%i%s" },
  566. { 0, 0, 0, 0 }
  567. };
  568. /*
  569. Return format string according format name.
  570. If name is unknown, result is NULL
  571. */
  572. const char *get_date_time_format_str(KNOWN_DATE_TIME_FORMAT *format,
  573. timestamp_type type)
  574. {
  575. switch (type) {
  576. case MYSQL_TIMESTAMP_DATE:
  577. return format->date_format;
  578. case MYSQL_TIMESTAMP_DATETIME:
  579. return format->datetime_format;
  580. case MYSQL_TIMESTAMP_TIME:
  581. return format->time_format;
  582. default:
  583. DBUG_ASSERT(0); // Impossible
  584. return 0;
  585. }
  586. }
  587. /****************************************************************************
  588. Functions to create default time/date/datetime strings
  589. NOTE:
  590. For the moment the DATE_TIME_FORMAT argument is ignored becasue
  591. MySQL doesn't support comparing of date/time/datetime strings that
  592. are not in arbutary order as dates are compared as strings in some
  593. context)
  594. This functions don't check that given MYSQL_TIME structure members are
  595. in valid range. If they are not, return value won't reflect any
  596. valid date either. Additionally, make_time doesn't take into
  597. account time->day member: it's assumed that days have been converted
  598. to hours already.
  599. ****************************************************************************/
  600. void make_time(const DATE_TIME_FORMAT *format __attribute__((unused)),
  601. const MYSQL_TIME *l_time, String *str)
  602. {
  603. uint length= (uint) my_time_to_str(l_time, (char*) str->ptr());
  604. str->length(length);
  605. str->set_charset(&my_charset_bin);
  606. }
  607. void make_date(const DATE_TIME_FORMAT *format __attribute__((unused)),
  608. const MYSQL_TIME *l_time, String *str)
  609. {
  610. uint length= (uint) my_date_to_str(l_time, (char*) str->ptr());
  611. str->length(length);
  612. str->set_charset(&my_charset_bin);
  613. }
  614. void make_datetime(const DATE_TIME_FORMAT *format __attribute__((unused)),
  615. const MYSQL_TIME *l_time, String *str)
  616. {
  617. uint length= (uint) my_datetime_to_str(l_time, (char*) str->ptr());
  618. str->length(length);
  619. str->set_charset(&my_charset_bin);
  620. }
  621. void make_truncated_value_warning(THD *thd, MYSQL_ERROR::enum_warning_level level,
  622. const char *str_val,
  623. uint str_length, timestamp_type time_type,
  624. const char *field_name)
  625. {
  626. char warn_buff[MYSQL_ERRMSG_SIZE];
  627. const char *type_str;
  628. CHARSET_INFO *cs= &my_charset_latin1;
  629. char buff[128];
  630. String str(buff,(uint32) sizeof(buff), system_charset_info);
  631. str.copy(str_val, str_length, system_charset_info);
  632. str[str_length]= 0; // Ensure we have end 0 for snprintf
  633. switch (time_type) {
  634. case MYSQL_TIMESTAMP_DATE:
  635. type_str= "date";
  636. break;
  637. case MYSQL_TIMESTAMP_TIME:
  638. type_str= "time";
  639. break;
  640. case MYSQL_TIMESTAMP_DATETIME: // FALLTHROUGH
  641. default:
  642. type_str= "datetime";
  643. break;
  644. }
  645. if (field_name)
  646. cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
  647. ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
  648. type_str, str.c_ptr(), field_name,
  649. (ulong) thd->row_count);
  650. else
  651. {
  652. if (time_type > MYSQL_TIMESTAMP_ERROR)
  653. cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
  654. ER(ER_TRUNCATED_WRONG_VALUE),
  655. type_str, str.c_ptr());
  656. else
  657. cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
  658. ER(ER_WRONG_VALUE), type_str, str.c_ptr());
  659. }
  660. push_warning(thd, level,
  661. ER_TRUNCATED_WRONG_VALUE, warn_buff);
  662. }
  663. /* Daynumber from year 0 to 9999-12-31 */
  664. #define MAX_DAY_NUMBER 3652424L
  665. bool date_add_interval(MYSQL_TIME *ltime, interval_type int_type, INTERVAL interval)
  666. {
  667. long period, sign;
  668. ltime->neg= 0;
  669. sign= (interval.neg ? -1 : 1);
  670. switch (int_type) {
  671. case INTERVAL_SECOND:
  672. case INTERVAL_SECOND_MICROSECOND:
  673. case INTERVAL_MICROSECOND:
  674. case INTERVAL_MINUTE:
  675. case INTERVAL_HOUR:
  676. case INTERVAL_MINUTE_MICROSECOND:
  677. case INTERVAL_MINUTE_SECOND:
  678. case INTERVAL_HOUR_MICROSECOND:
  679. case INTERVAL_HOUR_SECOND:
  680. case INTERVAL_HOUR_MINUTE:
  681. case INTERVAL_DAY_MICROSECOND:
  682. case INTERVAL_DAY_SECOND:
  683. case INTERVAL_DAY_MINUTE:
  684. case INTERVAL_DAY_HOUR:
  685. {
  686. longlong sec, days, daynr, microseconds, extra_sec;
  687. ltime->time_type= MYSQL_TIMESTAMP_DATETIME; // Return full date
  688. microseconds= ltime->second_part + sign*interval.second_part;
  689. extra_sec= microseconds/1000000L;
  690. microseconds= microseconds%1000000L;
  691. sec=((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+
  692. ltime->second +
  693. sign* (longlong) (interval.day*3600*24L +
  694. interval.hour*LL(3600)+interval.minute*LL(60)+
  695. interval.second))+ extra_sec;
  696. if (microseconds < 0)
  697. {
  698. microseconds+= LL(1000000);
  699. sec--;
  700. }
  701. days= sec/(3600*LL(24));
  702. sec-= days*3600*LL(24);
  703. if (sec < 0)
  704. {
  705. days--;
  706. sec+= 3600*LL(24);
  707. }
  708. ltime->second_part= (uint) microseconds;
  709. ltime->second= (uint) (sec % 60);
  710. ltime->minute= (uint) (sec/60 % 60);
  711. ltime->hour= (uint) (sec/3600);
  712. daynr= calc_daynr(ltime->year,ltime->month,1) + days;
  713. /* Day number from year 0 to 9999-12-31 */
  714. if ((ulonglong) daynr > MAX_DAY_NUMBER)
  715. goto invalid_date;
  716. get_date_from_daynr((long) daynr, &ltime->year, &ltime->month,
  717. &ltime->day);
  718. break;
  719. }
  720. case INTERVAL_DAY:
  721. case INTERVAL_WEEK:
  722. period= (calc_daynr(ltime->year,ltime->month,ltime->day) +
  723. sign * (long) interval.day);
  724. /* Daynumber from year 0 to 9999-12-31 */
  725. if ((ulong) period > MAX_DAY_NUMBER)
  726. goto invalid_date;
  727. get_date_from_daynr((long) period,&ltime->year,&ltime->month,&ltime->day);
  728. break;
  729. case INTERVAL_YEAR:
  730. ltime->year+= sign * (long) interval.year;
  731. if ((ulong) ltime->year >= 10000L)
  732. goto invalid_date;
  733. if (ltime->month == 2 && ltime->day == 29 &&
  734. calc_days_in_year(ltime->year) != 366)
  735. ltime->day=28; // Was leap-year
  736. break;
  737. case INTERVAL_YEAR_MONTH:
  738. case INTERVAL_QUARTER:
  739. case INTERVAL_MONTH:
  740. period= (ltime->year*12 + sign * (long) interval.year*12 +
  741. ltime->month-1 + sign * (long) interval.month);
  742. if ((ulong) period >= 120000L)
  743. goto invalid_date;
  744. ltime->year= (uint) (period / 12);
  745. ltime->month= (uint) (period % 12L)+1;
  746. /* Adjust day if the new month doesn't have enough days */
  747. if (ltime->day > days_in_month[ltime->month-1])
  748. {
  749. ltime->day = days_in_month[ltime->month-1];
  750. if (ltime->month == 2 && calc_days_in_year(ltime->year) == 366)
  751. ltime->day++; // Leap-year
  752. }
  753. break;
  754. default:
  755. goto null_date;
  756. }
  757. return 0; // Ok
  758. invalid_date:
  759. push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  760. ER_DATETIME_FUNCTION_OVERFLOW,
  761. ER(ER_DATETIME_FUNCTION_OVERFLOW),
  762. "datetime");
  763. null_date:
  764. return 1;
  765. }
  766. /*
  767. Calculate difference between two datetime values as seconds + microseconds.
  768. SYNOPSIS
  769. calc_time_diff()
  770. l_time1 - TIME/DATE/DATETIME value
  771. l_time2 - TIME/DATE/DATETIME value
  772. l_sign - 1 absolute values are substracted,
  773. -1 absolute values are added.
  774. seconds_out - Out parameter where difference between
  775. l_time1 and l_time2 in seconds is stored.
  776. microseconds_out- Out parameter where microsecond part of difference
  777. between l_time1 and l_time2 is stored.
  778. NOTE
  779. This function calculates difference between l_time1 and l_time2 absolute
  780. values. So one should set l_sign and correct result if he want to take
  781. signs into account (i.e. for MYSQL_TIME values).
  782. RETURN VALUES
  783. Returns sign of difference.
  784. 1 means negative result
  785. 0 means positive result
  786. */
  787. bool
  788. calc_time_diff(MYSQL_TIME *l_time1, MYSQL_TIME *l_time2, int l_sign, longlong *seconds_out,
  789. long *microseconds_out)
  790. {
  791. long days;
  792. bool neg;
  793. longlong microseconds;
  794. /*
  795. We suppose that if first argument is MYSQL_TIMESTAMP_TIME
  796. the second argument should be TIMESTAMP_TIME also.
  797. We should check it before calc_time_diff call.
  798. */
  799. if (l_time1->time_type == MYSQL_TIMESTAMP_TIME) // Time value
  800. days= (long)l_time1->day - l_sign * (long)l_time2->day;
  801. else
  802. {
  803. days= calc_daynr((uint) l_time1->year,
  804. (uint) l_time1->month,
  805. (uint) l_time1->day);
  806. if (l_time2->time_type == MYSQL_TIMESTAMP_TIME)
  807. days-= l_sign * (long)l_time2->day;
  808. else
  809. days-= l_sign*calc_daynr((uint) l_time2->year,
  810. (uint) l_time2->month,
  811. (uint) l_time2->day);
  812. }
  813. microseconds= ((longlong)days*LL(86400) +
  814. (longlong)(l_time1->hour*3600L +
  815. l_time1->minute*60L +
  816. l_time1->second) -
  817. l_sign*(longlong)(l_time2->hour*3600L +
  818. l_time2->minute*60L +
  819. l_time2->second)) * LL(1000000) +
  820. (longlong)l_time1->second_part -
  821. l_sign*(longlong)l_time2->second_part;
  822. neg= 0;
  823. if (microseconds < 0)
  824. {
  825. microseconds= -microseconds;
  826. neg= 1;
  827. }
  828. *seconds_out= microseconds/1000000L;
  829. *microseconds_out= (long) (microseconds%1000000L);
  830. return neg;
  831. }
  832. /*
  833. Compares 2 MYSQL_TIME structures
  834. SYNOPSIS
  835. my_time_compare()
  836. a - first time
  837. b - second time
  838. RETURN VALUE
  839. -1 - a < b
  840. 0 - a == b
  841. 1 - a > b
  842. */
  843. int my_time_compare(MYSQL_TIME *a, MYSQL_TIME *b)
  844. {
  845. ulonglong a_t= TIME_to_ulonglong_datetime(a);
  846. ulonglong b_t= TIME_to_ulonglong_datetime(b);
  847. if (a_t < b_t)
  848. return -1;
  849. if (a_t > b_t)
  850. return 1;
  851. if (a->second_part < b->second_part)
  852. return -1;
  853. if (a->second_part > b->second_part)
  854. return 1;
  855. return 0;
  856. }
  857. #endif