43 #if defined (SUPPRESS_STRLEN_WARNING) 44 #define strlen(s1) ((int) strlen(s1)) 48 #define IGREG1 (15 + 31L * (10 + 12L * 1582)) 50 #define IGREG2 2299161 52 #define IGREG_SPECIAL 0 54 #define FLOOR(d1) (int) (d1 + 4000000.0) - 4000000 62 #define DATETIME_FIELD_LIMIT 1000000 75 static void decode_time (
int timeval,
int *hourp,
int *minutep,
int *secondp);
76 static int encode_time (
int hour,
int minute,
int second);
77 static void decode_mtime (
int mtimeval,
int *hourp,
int *minutep,
int *secondp,
int *millisecondp);
78 static unsigned int encode_mtime (
int hour,
int minute,
int second,
int millisecond);
79 static int init_tm (
struct tm *);
83 static const char *
parse_mtime (
const char *buf,
int buf_len,
unsigned int *mtime,
bool * is_msec,
bool * is_explicit);
87 int *year_digits,
char *sep_ch);
89 char const **syntax_check,
int *time_parts,
char *sep_ch,
90 bool * has_explicit_msec,
bool is_datetime);
92 char const **syntax_check,
bool * has_explicit_msec);
95 bool * has_explicit_time,
bool * has_explicit_msec);
97 char const **syntax_check,
bool * has_explicit_msec);
123 if (m == 1 && d == 1 && y == -4713)
145 jul = (int) (
FLOOR (365.25 * jy) +
FLOOR (30.6001 * jm) + d + 1720995);
150 if ((d + 31L * (m + 12L * y)) >=
IGREG1)
152 ja = (int) (0.01 * jy);
153 jul += 2 - ja + (int) (0.25 * ja);
182 return ((
int) ((jul_day + 1) % 7));
198 int ja, jalpha, jb, jc, jd, je;
199 int day, month, year;
204 jalpha = (int) (((
float) (jul - 1867216) - 0.25) / 36524.25);
205 ja = jul + 1 + jalpha - (int) (0.25 * jalpha);
214 month = day = year = 0;
219 jc = (int) (6680.0 + ((
float) (jb - 2439870) - 122.1) / 365.25);
220 jd = (int) (365 * jc + (0.25 * jc));
221 je = (int) ((jb - jd) / 30.6001);
223 day = jb - jd - (int) (30.6001 * je);
258 *weekp = (int) ((jul + 1) % 7);
278 int tmp_month, tmp_day, tmp_year;
287 if (year < 0 || year > 9999 || month < 0 || month > 12 || day < 0 || day > 31)
301 if (month == tmp_month && day == tmp_day && year == tmp_year)
357 return ((((hour * 60) + minute) * 60) + second);
378 if (hour >= 0 && minute >= 0 && second >= 0 && hour < 24 && minute < 60 && second < 60)
380 *timeval = (((hour * 60) + minute) * 60) + second;
403 int minutes, hours, seconds;
405 seconds = timeval % 60;
406 minutes = (timeval / 60) % 60;
407 hours = (timeval / 3600) % 24;
460 int mon, day, year, hour,
min, sec;
465 if (c_time_struct ==
NULL || ((date ==
NULL) && (timeval ==
NULL)) ||
init_tm (c_time_struct) == -1)
474 c_time_struct->tm_mon = mon;
475 c_time_struct->tm_mday = day;
476 c_time_struct->tm_year = year;
478 if (c_time_struct->tm_year < 1900)
483 c_time_struct->tm_year -= 1900;
484 c_time_struct->tm_mon -= 1;
485 c_time_struct->tm_isdst = -1;
491 c_time_struct->tm_hour = hour;
492 c_time_struct->tm_min =
min;
493 c_time_struct->tm_sec = sec;
496 loc = *c_time_struct;
499 *retval = mktime (&loc);
500 if (*retval < (time_t) 0)
507 if (loc.tm_isdst == 0)
512 c_time_struct->tm_isdst = loc.tm_isdst;
514 *retval = mktime (c_time_struct);
515 if (*retval < (time_t) 0)
539 return tm_encode (c_time_struct, &retval, date, timeval);
638 int mon, day, year, hour,
min, sec;
639 const int year_century = 1900;
640 const int year_max_epoch = 2040;
641 const int secs_per_day = 24 * 3600;
642 const int secs_in_a_year = secs_per_day * 365;
643 const int days_up_to_month[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
655 year -= year_century;
656 if (year < 70 || year > year_max_epoch - year_century)
668 t = ((year - 70) * secs_in_a_year + ((year - 69) / 4) * secs_per_day - ((year - 1) / 100) * secs_per_day
669 + ((year + 299) / 400) * secs_per_day);
673 t += days_up_to_month[mon - 1] * secs_per_day;
680 t += (day - 1) * secs_per_day;
726 datetime.
date = *date;
727 datetime.
time = *timeval * 1000;
741 utc_datetime = datetime;
742 if (dest_tz_id !=
NULL)
748 utc_date = utc_datetime.
date;
749 utc_time = utc_datetime.
time / 1000;
784 int year, months, day;
785 int hours, minutes, seconds;
850 utc_datetime.
date = tmp_date;
851 utc_datetime.
time = tmp_time * 1000;
860 datetime = utc_datetime;
879 *date = datetime.
date;
884 *timeval = datetime.
time / 1000;
924 utc_datetime.
date = v_date;
925 utc_datetime.
time = v_time * 1000;
945 *date = datetime.
date;
950 *timeval = datetime.
time / 1000;
976 struct tm date_and_time;
977 int conversion_error;
979 conversion_error =
db_tm_encode (&date_and_time, date, timeval);
982 return ((
int) conversion_error);
985 retval = ((int) strftime (s, (
size_t) smax, fmt, &date_and_time));
1005 temp = localtime_r (epoch_time, &t);
1013 *date =
julian_encode (temp->tm_mon + 1, temp->tm_mday, temp->tm_year + 1900);
1015 if (timeval !=
NULL)
1017 *timeval =
encode_time (temp->tm_hour, temp->tm_min, temp->tm_sec);
1049 temp = localtime_r (epoch_time, &t);
1055 if (datetime !=
NULL)
1057 db_datetime_encode (datetime, temp->tm_mon + 1, temp->tm_mday, temp->tm_year + 1900, temp->tm_hour, temp->tm_min,
1058 temp->tm_sec, millisecond);
1079 if (time (&tloc) == -1)
1084 tmp = localtime_r (&tloc, &t);
1103 return (
init_tm (&tm) == -1) ? -1 : tm.tm_year +
YBIAS;
1118 int part[3] = { 0, 0, 0 };
1119 int part_char_len[3] = { 0, 0, 0 };
1120 int month, day, year;
1124 int date_style = -1;
1125 int year_part, month_part, day_part;
1126 int separator =
'\0';
1127 const char *strend = buf + buf_len;
1135 for (i = 0, p = buf; i < DIM (part); i++)
1146 part[
i] = part[
i] * 10 + (*p -
'0');
1150 if (i < DIM (part) - 1)
1164 if (separator !=
'\0' && *p !=
'\0' && *p != separator)
1229 if (0 <= year_part && 10000 <= part[year_part])
1235 if (year_part == -1)
1241 year = part[year_part];
1242 if (part_char_len[year_part] == 2)
1255 month = part[month_part];
1256 day = part[day_part];
1264 if (year == 0 && month == 0 && day == 0)
1278 if (month == part[0] && day == part[1] && year == part[2])
1280 *date = julian_date;
1313 if (is_msec ==
true)
1318 *time = mtime / 1000;
1336 const char *result =
parse_mtime (str, str_len, &mtime,
NULL, &is_explicit);
1337 if (result ==
NULL || *result !=
'\0')
1352 parse_mtime (
const char *buf,
int buf_len,
unsigned int *mtime,
bool * is_msec,
bool * is_explicit)
1354 int part[4] = { 0, 0, 0, 0 };
1357 double fraction = 100;
1358 const char *strend = buf + buf_len;
1365 if (is_msec !=
NULL)
1370 if (is_explicit !=
NULL)
1372 *is_explicit =
true;
1375 for (i = 0, p = buf; i < DIM (part); i++)
1383 part[
i] = part[
i] * 10 + (*p -
'0');
1387 part[
i] += (int) (fraction * (*p -
'0') + 0.5);
1391 if (i < DIM (part) - 1)
1396 if (p < strend && *p ==
':')
1404 else if (p < strend && *p ==
'.' && i == 2)
1407 if (is_msec !=
NULL)
1419 if (is_explicit !=
NULL && i < 3)
1421 *is_explicit =
false;
1438 else if (part[0] > 12)
1450 else if (part[0] == 0)
1455 else if (i == 0 && *buf)
1461 if (part[0] < 0 || part[0] > 23)
1465 if (part[1] < 0 || part[1] > 59)
1469 if (part[2] < 0 || part[2] > 59)
1473 if (part[3] < 0 || part[3] > 999)
1478 *mtime =
encode_mtime (part[0], part[1], part[2], part[3]);
1515 strftime (str, 10,
"%p", &tm);
1520 strftime (str, 10,
"%p", &tm);
1594 unsigned char separator = 0;
1597 int date_parts[3] = { 0, 0, 0 };
1598 unsigned char date_parts_len[3] = { 0, 0, 0 };
1599 unsigned char parts_found = 0, year_part, month_part, day_part;
1601 char const *
p =
str, *q;
1603 assert (!syntax_check || *syntax_check ==
NULL);
1626 unsigned char new_separator = separator;
1670 separator = new_separator;
1676 date_parts[parts_found] *= 10;
1677 date_parts[parts_found] += *p -
'0';
1680 if (date_parts_len[parts_found] < 4)
1682 date_parts_len[parts_found]++;
1703 if (parts_found < 2)
1725 if (parts_found == 2)
1727 date_parts[2] = date_parts[1];
1728 date_parts[1] = date_parts[0];
1729 date_parts_len[2] = date_parts_len[1];
1730 date_parts_len[1] = date_parts_len[0];
1735 if (parts_found == 2)
1747 *year_digits = date_parts_len[year_part];
1750 if (date_parts_len[year_part] == 2)
1752 if (date_parts[year_part] < 70)
1754 date_parts[year_part] += 2000;
1758 date_parts[year_part] += 1900;
1763 if (date_parts[month_part] == 0 && date_parts[day_part] == 0 && date_parts[year_part] == 0)
1770 if (date_parts[year_part] >= 10000 || date_parts[month_part] > 12 || date_parts[month_part] < 1
1771 || date_parts[day_part] > 31 || date_parts[day_part] < 1)
1783 int year, month, day;
1785 cdate =
julian_encode (date_parts[month_part], date_parts[day_part], date_parts[year_part]);
1788 if (day == date_parts[day_part] && month == date_parts[month_part] && year == date_parts[year_part])
1834 int *time_parts,
char *sep_ch,
bool * has_explicit_msec,
bool is_datetime)
1836 int h = 0, m = 0, s = 0, msec = 0;
1837 char const *
p =
str, *q;
1839 assert (!syntax_check || !*syntax_check);
1978 if (p < strend && *p ==
'.')
1983 msec += (*p -
'0') * 100;
1988 msec += (*p -
'0') * 10;
2008 if (has_explicit_msec)
2010 *has_explicit_msec =
true;
2074 if (time_parts && *time_parts < 3)
2086 if (h > 23 || m > 59 || s > 59)
2123 bool * has_explicit_msec)
2125 int h = 0, m = 0, s = 0, msec = 0, time_parts[3] = { 0, 0, 0 };
2126 unsigned time_parts_found = 0;
2127 char const *
p =
str, *q;
2128 int msec_digit_order = 100;
2148 while (((time_parts_found && p[0] ==
':' && (++p < strend &&
char_isdigit (*p)))
2149 || (!time_parts_found &&
char_isdigit (p[0]))) && time_parts_found < DIM (time_parts))
2153 time_parts[time_parts_found] *= 10;
2154 time_parts[time_parts_found] += *p -
'0';
2176 if (time_parts_found == DIM (time_parts) && *(p - 1) ==
':')
2181 if (p < strend && *p ==
'.')
2185 while (msec_digit_order && p < strend &&
char_isdigit (*p))
2187 msec += (*p++ -
'0') * msec_digit_order;
2188 msec_digit_order /= 10;
2198 if (has_explicit_msec)
2200 *has_explicit_msec =
true;
2205 switch (time_parts_found)
2261 if (h > 23 || m > 59 || s > 59)
2292 int y = 0, mo = 0, d = 0, h = 0, m = 0, s = 0, msec = 0;
2310 if (p != q || q[0] ==
'.')
2317 y = DECODE (p[0]) * 1000 + DECODE (p[1]) * 100 + DECODE (p[2]) * 10 + DECODE (p[3]);
2318 mo = DECODE (p[4]) * 10 + DECODE (p[5]);
2319 d = DECODE (p[6]) * 10 + DECODE (p[7]);
2320 h = DECODE (p[8]) * 10 + DECODE (p[9]);
2321 m = DECODE (p[10]) * 10 + DECODE (p[11]);
2322 s = DECODE (p[12]) * 10 + DECODE (p[13]);
2325 msec += DECODE (*p++) * 100;
2328 msec += DECODE (*p++) * 10;
2333 msec += DECODE (*p++);
2364 mo += DECODE (*p++);
2369 mo += DECODE (*p++);
2370 d += DECODE (*p++) * 10;
2408 msec += DECODE (*p++) * 100;
2411 msec += DECODE (*p++) * 10;
2414 msec += DECODE (*p++);
2427 y = DECODE (p[0]) * 10 + DECODE (p[1]);
2428 mo = DECODE (p[2]) * 10 + DECODE (p[3]);
2429 d = DECODE (p[4]) * 10 + DECODE (p[5]);
2448 int year, month, day;
2451 if ((ndigits == 12) || (ndigits == 7))
2463 else if (ndigits <= 10)
2473 if (y != year || mo != month || d != day)
2512 if (h > 23 || m > 59 || s > 59)
2550 bool * has_explicit_time,
bool * has_explicit_msec)
2552 int y = 0, mo = 0, d = 0, h = 0, m = 0, s = 0, msec = 0, ndigits = 0;
2553 char const *
p =
str, *q =
str;
2581 y = DECODE (p[0]) * 10 + DECODE (p[1]);
2582 mo = DECODE (p[2]) * 10 + DECODE (p[3]);
2583 d = DECODE (p[4]) * 10 + DECODE (p[5]);
2606 mo += DECODE (*p++);
2611 mo += DECODE (*p++);
2625 y = DECODE (p[0]) * 10 + DECODE (p[1]);
2639 y = DECODE (p[0]) * 1000 + DECODE (p[1]) * 100 + DECODE (p[2]) * 10 + DECODE (p[3]);
2645 mo = DECODE (p[0]) * 10 + DECODE (p[1]);
2646 d = DECODE (p[2]) * 10 + DECODE (p[3]);
2647 h = DECODE (p[4]) * 10 + DECODE (p[5]);
2669 if (has_explicit_msec)
2671 *has_explicit_msec =
true;
2678 if (has_explicit_msec)
2680 *has_explicit_msec =
true;
2683 msec += DECODE (*p++) * 100;
2687 msec += DECODE (*p++) * 10;
2691 msec += DECODE (*p);
2705 if (has_explicit_time)
2707 *has_explicit_time = (ndigits == 7 || ndigits > 8);
2717 if (ndigits == 6 || ndigits == 7)
2734 if (ndigits > 8 || ndigits == 7)
2771 if (mo <= 12 && d <= 31 && h <= 23 && m <= 59 && s <= 59)
2774 int year, month, day;
2778 if (y != year || mo != month || d != day)
2787 return (p == strend ?
"" : p);
2821 bool * has_explicit_msec)
2824 char const *
p =
str, *syntax_check =
NULL;
2829 if (!p && !syntax_check)
2832 if (has_explicit_msec)
2834 *has_explicit_msec =
false;
2839 if (p || syntax_check)
2841 if (sep_ch !=
'0' && sep_ch !=
':')
2846 syntax_check =
NULL;
2851 if (p || syntax_check)
2853 bool space_separator =
false;
2864 space_separator =
true;
2868 if (space_separator)
2870 char const *q = syntax_check;
2872 syntax_check =
NULL;
2879 if (p || syntax_check)
2881 if (sep_ch ==
'-' || sep_ch ==
'/')
2891 *syntax = syntax_check;
2901 if ((p || syntax_check) && (sep_ch ==
'-' || sep_ch ==
'/'))
2909 *syntax = syntax_check;
2935 char const *syntax_check =
NULL;
2936 int year_digits = 0;
2938 unsigned int mtime = 0;
2940 char const *strend = str + str_len;
2947 *time = mtime / 1000;
2948 *millisecond = mtime % 1000;
2969 if (p || syntax_check)
2971 bool space_separator =
false;
2972 bool has_explicit_date_part =
false;
2975 if (sep_ch ==
'-' || sep_ch ==
'/')
2977 has_explicit_date_part =
true;
2989 space_separator =
true;
2993 if (space_separator)
3001 syntax_check =
NULL;
3018 if (year_digits >= 3 || (time_parts >= 2 && year_digits))
3020 *time = mtime / 1000;
3021 *millisecond = mtime % 1000;
3037 if (has_explicit_date_part)
3051 syntax_check =
NULL;
3053 if (p || syntax_check)
3061 if (has_explicit_date_part)
3071 if (p && has_explicit_date_part)
3082 syntax_check =
NULL;
3088 if (p < strend && p[0] ==
' ')
3103 *time = mtime / 1000;
3104 *millisecond = mtime % 1000;
3134 *time = mtime / 1000;
3135 *millisecond = mtime % 1000;
3177 bool * has_explicit_msec,
bool * fits_as_timestamp,
char const **endp)
3180 unsigned int mtime = 0;
3181 char const *strend = str + str_len;
3182 char const *syntax_check =
NULL, *
p;
3189 if (has_explicit_time)
3191 *has_explicit_time =
true;
3194 if (has_explicit_msec && !*has_explicit_msec && fits_as_timestamp)
3200 if (*fits_as_timestamp !=
NO_ERROR)
3206 datetime->
date = date;
3207 datetime->
time = mtime;
3221 if (has_explicit_msec)
3223 *has_explicit_msec =
true;
3232 syntax_check =
NULL;
3244 if (has_explicit_time)
3247 while (r < strend && ((*r == sep_ch) || (
char_isdigit (*r))))
3256 *has_explicit_time =
true;
3259 if (has_explicit_msec && !*has_explicit_msec && fits_as_timestamp)
3265 if (*fits_as_timestamp !=
NO_ERROR)
3284 if (has_explicit_time)
3286 *has_explicit_time =
false;
3289 if (has_explicit_msec)
3291 *has_explicit_msec =
false;
3294 if (fits_as_timestamp)
3300 if (*fits_as_timestamp !=
NO_ERROR)
3331 if (r && (*r == 0 || (*r && (r - str >= 6))))
3333 if (has_explicit_msec && !*has_explicit_msec && fits_as_timestamp)
3339 if (*fits_as_timestamp !=
NO_ERROR)
3349 *datetime = cdatetime;
3369 if (has_explicit_msec && !*has_explicit_msec && fits_as_timestamp)
3375 if (*fits_as_timestamp !=
NO_ERROR)
3402 if (datetime->
time != 0)
3453 time = datetime.
time / 1000;
3489 *date = datetime.
date;
3615 datetime->
date = date;
3616 datetime->
time = mtime;
3632 const char *result =
NULL;
3642 if (result ==
NULL || result[0] !=
'\0')
3664 const char *p_end = str + str_len;
3674 if (p ==
NULL || (p < p_end && p[0] !=
'\0'))
3711 const char *p_end = str + str_len;
3721 if (p ==
NULL || (p < p_end && p[0] !=
'\0'))
3760 const char *
p, *p_end;
3763 p_end = str + str_len;
3772 if (p ==
NULL || (p < p_end && p[0] !=
'\0'))
3825 int str_zone_size = 0;
3826 const char *
p, *p_end, *str_zone;
3830 p_end = str + str_len;
3871 if (p ==
NULL || (p < p_end && p[0] !=
'\0'))
3912 bool dummy_has_zone;
3956 const int len_out = 10;
3959 if (buf ==
NULL || bufsize == 0)
3963 if (bufsize <= len_out)
3969 buf[cnt++] = mon / 10 +
'0';
3970 buf[cnt++] = mon % 10 +
'0';
3972 buf[cnt++] = day / 10 +
'0';
3973 buf[cnt++] = day % 10 +
'0';
3975 buf[cnt++] = year / 1000 +
'0';
3976 buf[cnt++] = (year / 100) % 10 +
'0';
3977 buf[cnt++] = (year / 10) % 10 +
'0';
3978 buf[cnt++] = year % 10 +
'0';
3999 const int len_out = 11;
4001 if (buf ==
NULL || bufsize == 0)
4005 if (bufsize <= len_out)
4011 pm = (hour >= 12) ?
true :
false;
4021 buf[cnt++] = hour / 10 +
'0';
4022 buf[cnt++] = hour % 10 +
'0';
4024 buf[cnt++] = min / 10 +
'0';
4025 buf[cnt++] = min % 10 +
'0';
4027 buf[cnt++] = sec / 10 +
'0';
4028 buf[cnt++] = sec % 10 +
'0';
4066 if (bufsize - m < 2)
4109 if (bufsize - res < 2)
4175 return ((((((hour * 60) + minute) * 60) + second) * 1000) + millisecond);
4189 decode_mtime (
int mtimeval,
int *hourp,
int *minutep,
int *secondp,
int *millisecondp)
4191 int hours, minutes, seconds, milliseconds;
4193 milliseconds = mtimeval % 1000;
4194 seconds = (mtimeval / 1000) % 60;
4195 minutes = (mtimeval / 60000) % 60;
4196 hours = (mtimeval / 3600000) % 24;
4202 if (minutep !=
NULL)
4206 if (secondp !=
NULL)
4210 if (millisecondp !=
NULL)
4212 *millisecondp = milliseconds;
4228 int hour, minute, second, millisecond;
4231 const int len_out = 26;
4233 if (buf ==
NULL || bufsize == 0)
4237 if (bufsize <= len_out)
4242 db_datetime_decode (datetime, &mon, &day, &year, &hour, &minute, &second, &millisecond);
4243 pm = (hour >= 12) ?
true :
false;
4253 buf[cnt++] = hour / 10 +
'0';
4254 buf[cnt++] = hour % 10 +
'0';
4256 buf[cnt++] = minute / 10 +
'0';
4257 buf[cnt++] = minute % 10 +
'0';
4259 buf[cnt++] = second / 10 +
'0';
4260 buf[cnt++] = second % 10 +
'0';
4262 buf[cnt++] = millisecond / 100 +
'0';
4263 buf[cnt++] = (millisecond / 10) % 10 +
'0';
4264 buf[cnt++] = millisecond % 10 +
'0';
4277 buf[cnt++] = mon / 10 +
'0';
4278 buf[cnt++] = mon % 10 +
'0';
4280 buf[cnt++] = day / 10 +
'0';
4281 buf[cnt++] = day % 10 +
'0';
4283 buf[cnt++] = year / 1000 +
'0';
4284 buf[cnt++] = (year / 100) % 10 +
'0';
4285 buf[cnt++] = (year / 10) % 10 +
'0';
4286 buf[cnt++] = year % 10 +
'0';
4322 if (retval <= 0 || retval > bufsize + 1)
4378 int hour, minute, second, millisecond;
4381 if (buf ==
NULL || bufsize == 0)
4386 db_datetime_decode (datetime, &mon, &day, &year, &hour, &minute, &second, &millisecond);
4388 if (millisecond > 0)
4391 snprintf (buf, bufsize,
"%04d-%02d-%02d %02d:%02d:%02d.%03d", year, mon, day, hour, minute, second,
4396 retval = snprintf (buf, bufsize,
"%04d-%02d-%02d %02d:%02d:%02d", year, mon, day, hour, minute, second);
4398 if (bufsize < retval)
4417 const char *p_end = str + str_len;
4425 if (p ==
NULL || (p < p_end && p[0] !=
'\0'))
4458 int str_zone_size = 0;
4459 const char *
p, *p_end;
4460 const char *str_zone =
NULL;
4463 p_end = str + str_len;
4503 if (p ==
NULL || (p < p_end && p[0] !=
'\0'))
4537 bool dummy_has_zone;
4629 result_bi = bi1 - bi2;
4642 result_datetime->
date = (int) tmp_bi;
4673 result_bi = bi1 + bi2;
4687 result_datetime->
date = (int) tmp_bi;
4699 int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
4700 int day_of_year = 0;
4703 for (i = 1; i < month; i++)
4705 day_of_year += days[i - 1];
4725 if (year == 0 && month == 0 && day == 0)
4736 return (day + (2 * month) + (
int) (6 * (month + 1) / 10) + year + (
int) (year / 4) - (
int) (year / 100) +
4737 (
int) (year / 400) + 1) % 7;
4772 return 7 + 8 - dow_1Jan;
4780 else if (dow_1Jan > 5)
4782 return 16 - dow_1Jan;
4786 return 9 - dow_1Jan;
4791 return 7 + 8 - dow_1Jan;
4795 return 8 - dow_1Jan;
4802 else if (dow_1Jan == 2)
4808 return 16 - dow_1Jan;
4813 return 7 + 8 - dow_1Jan;
4817 return 8 - dow_1Jan;
4826 return 16 - dow_1Jan;
4858 int week_number = 0;
4859 int last_week_of_year = 0;
4860 int days_last_year = 0;
4862 assert ((0 <= mode) && (mode <= 7));
4864 if (day_of_year > end_of_first_week)
4867 week_number = 1 + (day_of_year - end_of_first_week) / 7;
4868 if ((day_of_year - end_of_first_week) % 7)
4875 if (day_of_year > end_of_first_week - 7)
4882 if (mode == 0 || mode == 1 || mode == 4 || mode == 5)
4890 days_last_year = 365;
4897 week_number = 1 + (days_last_year - last_week_of_year) / 7;
4898 if ((days_last_year - last_week_of_year) % 7 != 0)
4916 int i, res = 0,
len;
4917 int format_type[256];
4918 bool has_timezone =
false;
4921 memset (format_type, 0,
sizeof (format_type));
4958 for (i = 1; i <
len; i++)
4960 if (format_s[i - 1] !=
'%')
4962 if (i > 1 && format_s[i - 2] ==
'%')
4965 if (format_type[(
unsigned char) format_s[i]] != 0)
4967 res |= format_type[(
unsigned char) format_s[i]];
4969 if (i + 2 < len && format_s[i] ==
'T' && format_s[i + 1] ==
'Z')
4971 switch (format_s[i + 2])
4977 has_timezone =
true;
5011 int days_months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
5019 if (*year < 0 || *year > 9999)
5024 if (*month < 1 || *month > 12)
5031 days_months[2] += 1;
5034 if (*day < 0 || *day > days_months[*month])
5044 if (day_week < 0 || day_week > 6)
5052 for (i = 1; i <= weeks; i++)
5055 if (d > days_months[m])
5057 d = d - days_months[m];
5064 if ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0))
5066 days_months[2] = 29;
5070 days_months[2] = 28;
5077 if (d > days_months[m])
5079 d = d - days_months[m];
5088 if (y < 0 || y > 9999)
int julian_encode(int m, int d, int y)
static unsigned int encode_mtime(int hour, int minute, int second, int millisecond)
static int db_timestamp_encode_w_reg(const DB_DATE *date, const DB_TIME *timeval, const TZ_REGION *tz_region, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
int db_strftime(char *s, int smax, const char *fmt, DB_DATE *date, DB_TIME *timeval)
static char local_pm_str[10]
static bool is_local_pm_str(const char *p, const char *p_end)
int db_timestamptz_to_string(char *buf, int bufsize, DB_TIMESTAMP *utime, const TZ_ID *tz_id)
int db_get_week_of_year(int year, int month, int day, int mode)
int db_string_to_timestamp(const char *str, DB_TIMESTAMP *utime)
int db_datetime_to_string2(char *buf, int bufsize, DB_DATETIME *datetime)
DB_BIGINT tz_timestamp_encode_leap_sec_adj(const int year_century, const int year, const int mon, const int day)
#define DATETIME_FIELD_LIMIT
int db_add_int_to_datetime(DB_DATETIME *datetime, DB_BIGINT bi2, DB_DATETIME *result_datetime)
int db_date_to_string(char *buf, int bufsize, DB_DATE *date)
int db_date_parse_datetime(char const *str, int str_len, DB_DATETIME *datetime)
int db_string_to_datetimetz(const char *str, DB_DATETIMETZ *dt_tz, bool *has_zone)
static int get_end_of_week_one_of_year(int year, int mode)
int db_datetime_decode(const DB_DATETIME *datetime, int *month, int *day, int *year, int *hour, int *minute, int *second, int *millisecond)
int db_timestamp_encode_ses(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
static const char * parse_date(const char *buf, int buf_len, DB_DATE *date)
const TZ_ID * tz_get_utc_tz_id(void)
static const char * parse_for_timestamp(const char *buf, int buf_len, DB_DATE *date, DB_TIME *time, bool allow_msec)
int db_string_to_datetime(const char *str, DB_DATETIME *datetime)
void tz_get_session_tz_region(TZ_REGION *tz_region)
int db_subtract_int_from_datetime(DB_DATETIME *dt1, DB_BIGINT bi2, DB_DATETIME *result_datetime)
static char const * parse_timestamp_compact(char const *str, char const *strend, DB_DATE *date, unsigned int *mtime, bool *has_explicit_time, bool *has_explicit_msec)
int db_timestampltz_to_string(char *buf, int bufsize, DB_TIMESTAMP *utime)
int db_string_to_datetime_ex(const char *str, int str_len, DB_DATETIME *datetime)
#define OR_CHECK_ADD_OVERFLOW(a, b, c)
int db_string_to_timestamptz_ex(const char *str, int str_len, DB_TIMESTAMPTZ *ts_tz, bool *has_zone, bool is_cast)
int db_string_to_timestamp_ex(const char *str, int str_len, DB_TIMESTAMP *utime)
static const char * parse_mtime(const char *buf, int buf_len, unsigned int *mtime, bool *is_msec, bool *is_explicit)
#define ER_TIMESTAMP_CONVERSION
static int local_pm_strlen
int tz_conv_tz_datetime_w_region(const DB_DATETIME *src_dt, const TZ_REGION *src_tz_region, const TZ_REGION *dest_tz_region, DB_DATETIME *dest_dt, TZ_ID *src_tz_id_out, TZ_ID *dest_tz_id_out)
int db_timestamp_encode_sys(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
static int init_tm(struct tm *)
static int tm_encode(struct tm *c_time_struct, time_t *retval, DB_DATE *date, DB_TIME *timeval)
int db_date_weekday(DB_DATE *date)
int db_datetimetz_to_string(char *buf, int bufsize, DB_DATETIME *dt, const TZ_ID *tz_id)
int db_date_encode(DB_DATE *date, int month, int day, int year)
int db_timestamp_to_string(char *buf, int bufsize, DB_TIMESTAMP *utime)
int db_check_time_date_format(const char *format_s)
static int encode_time(int hour, int minute, int second)
int db_string_to_time(const char *str, DB_TIME *time)
int tz_create_datetimetz(const DB_DATETIME *dt, const char *tz_str, const int tz_size, const TZ_REGION *default_tz_region, DB_DATETIMETZ *dt_tz, const char **end_tz_str)
unsigned int DB_TIMESTAMP
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
bool db_string_check_explicit_date(const char *str, int str_len)
static char const * parse_mtime_separated(char const *str, char const *strend, unsigned int *mtime, char const **syntax_check, int *time_parts, char *sep_ch, bool *has_explicit_msec, bool is_datetime)
int tz_create_session_tzid_for_datetime(const DB_DATETIME *src_dt, bool src_is_utc, TZ_ID *tz_id)
static char local_am_str[10]
int db_string_to_datetimetz_ex(const char *str, int str_len, DB_DATETIMETZ *dt_tz, bool *has_zone)
static char const * parse_explicit_mtime_compact(char const *str, char const *strend, unsigned int *mtime)
#define ER_TIME_CONVERSION
int db_timestamp_encode_utc(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime)
int db_time_encode(DB_TIME *timeval, int hour, int minute, int second)
int tz_id_to_str(const TZ_ID *tz_id, char *tz_str, const int tz_str_size)
static char const * parse_date_separated(char const *str, char const *strend, DB_DATE *date, char const **syntax_check, int *year_digits, char *sep_ch)
void db_localdatetime(time_t *epoch_time, DB_DATETIME *datetime)
int db_date_parse_timestamp(char const *str, int str_len, DB_TIMESTAMP *utime)
static enum scanner_mode mode
#define OR_CHECK_INT_OVERFLOW(i)
static void fill_local_ampm_str(char str[10], bool am)
bool db_string_check_explicit_time(const char *str, int str_len)
int db_date_parse_time(char const *str, int str_len, DB_TIME *time, int *millisecond)
int db_get_day_of_week(int year, int month, int day)
void tz_get_system_tz_region(TZ_REGION *tz_region)
int db_tm_encode(struct tm *c_time_struct, DB_DATE *date, DB_TIME *timeval)
static const char * parse_time(const char *buf, int buf_len, DB_TIME *mtime)
static bool is_local_am_str(const char *p, const char *p_end)
#define MILLISECONDS_OF_ONE_DAY
void tz_timestamp_decode_sec(int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp, int *minutesp, int *secondsp)
int db_timestamp_decode_w_tz_id(const DB_TIMESTAMP *utime, const TZ_ID *tz_id, DB_DATE *date, DB_TIME *timeval)
int db_date_parse_date(char const *str, int str_len, DB_DATE *date)
#define OR_CHECK_SUB_UNDERFLOW(a, b, c)
int db_timestamp_decode_ses(const DB_TIMESTAMP *utime, DB_DATE *date, DB_TIME *timeval)
int db_time_to_string(char *buf, int bufsize, DB_TIME *time)
int db_datetime_to_string(char *buf, int bufsize, DB_DATETIME *datetime)
#define ER_DATE_CONVERSION
static void error(const char *msg)
time_t db_mktime(DB_DATE *date, DB_TIME *timeval)
int db_string_to_timestampltz(const char *str, DB_TIMESTAMP *ts)
int tz_utc_datetimetz_to_local(const DB_DATETIME *dt_utc, const TZ_ID *tz_id, DB_DATETIME *dt_local)
int db_string_to_timestampltz_ex(const char *str, int str_len, DB_TIMESTAMP *ts)
#define ER_QPROC_TIME_UNDERFLOW
int db_string_to_time_ex(const char *str, int str_len, DB_TIME *time)
static const char * parse_datetime(const char *buf, int buf_len, DB_DATETIME *datetime)
void db_date_locale_init(void)
void julian_decode(int jul, int *monthp, int *dayp, int *yearp, int *weekp)
int db_string_to_datetimeltz(const char *str, DB_DATETIME *datetime)
int db_datetime_encode(DB_DATETIME *datetime, int month, int day, int year, int hour, int minute, int second, int millisecond)
int intl_mbs_ncasecmp(const char *mbs1, const char *mbs2, size_t n)
int tz_create_timestamptz(const DB_DATE *date, const DB_TIME *time, const char *tz_str, const int tz_size, const TZ_REGION *default_tz_region, DB_TIMESTAMPTZ *ts_tz, const char **end_tz_str)
static int get_current_year(void)
void db_timestamp_decode_utc(const DB_TIMESTAMP *utime, DB_DATE *date, DB_TIME *timeval)
static int local_am_strlen
int db_timestamp_encode(DB_TIMESTAMP *utime, DB_DATE *date, DB_TIME *timeval)
int db_add_weeks_and_days_to_date(int *day, int *month, int *year, int weeks, int day_week)
void db_date_decode(const DB_DATE *date, int *monthp, int *dayp, int *yearp)
int db_timestamp_decode_w_reg(const DB_TIMESTAMP *utime, const TZ_REGION *tz_region, DB_DATE *date, DB_TIME *timeval)
const TZ_REGION * tz_get_utc_tz_region(void)
static char const * parse_timedate_separated(char const *str, char const *strend, DB_DATE *date, unsigned int *mtime, char const **syntax_check, bool *has_explicit_msec)
static char const * parse_explicit_mtime_separated(char const *str, char const *strend, unsigned int *mtime, char const **syntax_check, bool *has_explicit_msec)
static void decode_time(int timeval, int *hourp, int *minutep, int *secondp)
int db_string_to_timestamptz(const char *str, DB_TIMESTAMPTZ *ts_tz, bool *has_zone)
static void decode_mtime(int mtimeval, int *hourp, int *minutep, int *secondp, int *millisecondp)
void db_time_decode(DB_TIME *timeval, int *hourp, int *minutep, int *secondp)
int db_string_to_date_ex(const char *str, int str_len, DB_DATE *date)
int tz_create_session_tzid_for_timestamp(const DB_UTIME *src_ts, TZ_ID *tz_id)
int day_of_week(int jul_day)
int db_string_to_datetimeltz_ex(const char *str, int str_len, DB_DATETIME *datetime)
void db_localdatetime_msec(time_t *epoch_time, int millisecond, DB_DATETIME *datetime)
int db_string_to_date(const char *str, DB_DATE *date)
int db_get_day_of_year(int year, int month, int day)
void db_localtime(time_t *epoch_time, DB_DATE *date, DB_TIME *timeval)
int db_datetimeltz_to_string(char *buf, int bufsize, DB_DATETIME *dt)
int db_date_parse_datetime_parts(char const *str, int str_len, DB_DATETIME *datetime, bool *has_explicit_time, bool *has_explicit_msec, bool *fits_as_timestamp, char const **endp)
#define TZ_IS_UTC_TZ_REGION(r)