/* Return the day of the week */ static int dateExtract(decNumber *res, const decNumber *x, int *y, int *m, int *d) { if (decNumberIsSpecial(x) || extract_date(x, y, m, d)) { set_NaN(res); return 0; } return 1; }
int main() { void *ed; char buf[1024],*r,datebuf[32]; printf("%ld\n",date2secs("2011-04-05 10:33:28")); printf("%ld\n",date2secs("2011-4-5 10:33:28")); printf("%ld\n",http_atotm("Fri, 15 Apr 2011 2:33:28 GMT")); ed = extract_date_init(); if(!ed) return 0; while(fgets(buf,1024,stdin)) { trim(buf); if(strcasecmp("exit",buf) == 0) break; r = extract_date(ed,buf,datebuf); if(r) printf("DATE: %s\n",datebuf); else printf("No DATE\n"); } extract_date_free(ed); return 0; }
void extract_info(char *str, string &site_name, visit &visit) { char *ips=NULL, *site=NULL, *date=NULL; #define TOK_ERR "Found a record with invalid formatting... ignoring entry" if((ips = strtok(str," "))==NULL) throw (string)(TOK_ERR); if((site = strtok(NULL," "))==NULL) throw (string)(TOK_ERR); if((date = strtok(NULL," "))==NULL) throw (string)(TOK_ERR); #undef TOK_ERR if(!strlen(site)) throw("Found a record with site name missing... ignoring entry"); UI ipn; mmddyyyy sd; try { ipn = extract_ip(ips); sd = extract_date(date); } catch(string e) { throw "Found a record with an " + e + " ignoring entry"; } // success site_name = site; visit.ip = ipn; visit.date = sd; }
/* Conversion routines from Julian days to and from dates */ decNumber *dateToJ(decNumber *res, const decNumber *x) { if (decNumberIsSpecial(x)) err: set_NaN(res); else { int y, m, d; if (extract_date(x, &y, &m, &d)) goto err; int_to_dn(res, JDN(y, m, d)); } return res; }
void date_alphamonth(enum nilop op) { decNumber x; int y, m, d; static const char mons[12*3] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"; getX(&x); if (decNumberIsSpecial(&x) || extract_date(&x, &y, &m, &d)) err(ERR_BAD_DATE); else { copy3(&(mons[3*m - 3])); } }
void date_alphaday(enum nilop op) { decNumber x; int y, m, d, dow; getX(&x); if (decNumberIsSpecial(&x) || extract_date(&x, &y, &m, &d)) err(ERR_BAD_DATE); else { dow = day_of_week(y, m, d, NULL); copy3(&("MONTUEWEDTHUFRISATSUN"[3*(dow-1)])); } }
date_range input_a_date_range() { date_range dr; char str[11]; both_inputs: start_input: cout<<"Enter a start date: "; fflush(stdin); fgets(str,11,stdin); if(*str=='\0'||*str=='\n') goto start_input; try { dr.start = extract_date(str); } catch(string e) { cout<<((char)(toupper(*(e.c_str()))) + e.substr(1, e.length()-1) + " please re-enter information\n"); goto start_input; } end_input: cout<<"Enter a finish date: "; fflush(stdin); fgets(str,11,stdin); if(*str=='\0'||*str=='\n') goto end_input; try { dr.end = extract_date(str); } catch(string e) { cout<<((char)(toupper(*(e.c_str()))) + e.substr(1, e.length()-1) + " please re-enter information\n"); goto end_input; } if(dr.end < dr.start) { cout<<"Dates out of order... please re-enter information\n"; goto both_inputs; } return dr; }
/* 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; }
/* Date and times to the Alpha register */ void date_alphadate(enum nilop op) { decNumber x; int d, m, y; char buf[16]; char *p; getX(&x); xset(buf, '\0', sizeof(buf)); if (extract_date(&x, &y, &m, &d)) { err(ERR_BAD_DATE); return; } switch (UState.date_mode) { default: p = num_arg(buf, y); *p++ = '-'; p = num_arg_0(p, m, 2); *p++ = '-'; num_arg_0(p, d, 2); break; case DATE_DMY: p = num_arg(buf, d); *p++ = '.'; p = num_arg_0(p, m, 2); *p++ = '.'; num_arg_0(p, y, 4); break; case DATE_MDY: p = num_arg(buf, m); *p++ = '/'; p = num_arg_0(p, d, 2); *p++ ='/'; num_arg_0(p, y, 4); break; } add_string(buf); }
void date_setdate(enum nilop op) { int d, m, y, dow; decNumber x; getX(&x); if (extract_date(&x, &y, &m, &d)) { err(ERR_BAD_DATE); return; } dow = day_of_week(y, m, d, NULL); #ifdef REALBUILD if (Xtal) { busy(); RTC_SetDate((unsigned short) y, (unsigned char) m, (unsigned char) d, (unsigned char) dow); } else err(ERR_NO_CRYSTAL); #else // So that very strict compilers (i.e. gcc4.6 do not complain that dow is unused with -Wall) (void) dow; err(ERR_ILLEGAL); #endif }
static value_ptr extract_object(bplist_info_ptr bplist, uint64_t objectRef) { uint64_t offset; value_ptr result = NULL; uint8_t objectTag; if (objectRef >= bplist->object_count) { // Out-of-range object reference. bplist_log("Bad binary plist: object index is out of range.\n"); return NULL; } // Use cached object if it exists result = cache_lookup(bplist->cache, objectRef); if (result != NULL) return result; // Otherwise, find object in file. offset = read_offset(bplist, objectRef); if (offset > bplist->length) { // Out-of-range offset. bplist_log("Bad binary plist: object outside container.\n"); return NULL; } objectTag = *(bplist->data_bytes + offset); switch (objectTag & 0xF0) { case kTAG_SIMPLE: result = extract_simple(bplist, offset); break; case kTAG_INT: result = extract_int(bplist, offset); break; case kTAG_REAL: result = extract_real(bplist, offset); break; case kTAG_DATE: result = extract_date(bplist, offset); break; case kTAG_DATA: result = extract_data(bplist, offset); break; case kTAG_ASCIISTRING: result = extract_ascii_string(bplist, offset); break; case kTAG_UNICODESTRING: result = extract_unicode_string(bplist, offset); break; case kTAG_UID: result = extract_uid(bplist, offset); break; case kTAG_ARRAY: result = extract_array(bplist, offset); break; case kTAG_DICTIONARY: result = extract_dictionary(bplist, offset); break; default: // Unknown tag. bplist_log("Bad binary plist: unknown tag 0x%X.\n", (objectTag & 0x0F) >> 4); result = NULL; } // Cache and return result. if (result != NULL) cache_insert(&bplist->cache, objectRef, result); return result; }
/*========================================== * do_zformat_date * See description above for do_format_date * (Except this returns alloc'd ZSTR *========================================*/ static ZSTR do_zformat_date (STRING str, INT dfmt, INT mfmt, INT yfmt, INT sfmt, INT efmt, INT cmplx) { STRING smo, syr; static char daystr[3]; GDATEVAL gdv = 0; ZSTR zstr=zs_newn(40); if (!str) return zstr; initialize_if_needed(); if (sfmt==12) { /* This is what used to be the shrt flag */ zs_free(&zstr); return zshorten_date(str); } if (sfmt==14) { zs_sets(zstr, str); return zstr; } if (!cmplx) { /* simple */ gdv = extract_date(str); if (gdv->valid == GDV_V_PHRASE) { /* GEDCOM date phrases (parenthesized) shown "as is" */ return zs_news(gdv->text); } format_day(gdv->date1.day, dfmt, daystr); smo = format_month(gdv->date1.calendar, gdv->date1.month, mfmt); syr = format_year(gdv->date1.year, yfmt); format_ymd(zstr, syr, smo, daystr, sfmt); format_eratime(zstr, gdv->date1.eratime, efmt); if (gdv->date1.calendar) { format_cal(zstr, gdv->date1.calendar); } free_gdateval(gdv); return zstr; } else { ZSTR zstr2 = zs_newn(40); ZSTR zstr3=0; /* complex (include modifier words) */ gdv = extract_date(str); if (gdv->valid == GDV_V_PHRASE) { /* GEDCOM date phrases (parenthesized) shown "as is" */ return zs_news(gdv->text); } format_day(gdv->date1.day, dfmt, daystr); smo = format_month(gdv->date1.calendar, gdv->date1.month, mfmt); syr = (gdv->date1.year.str ? gdv->date1.year.str : format_year(gdv->date1.year, yfmt)); format_ymd(zstr, syr, smo, daystr, sfmt); format_eratime(zstr, gdv->date1.eratime, efmt); if (gdv->date1.calendar) { format_cal(zstr, gdv->date1.calendar); } if (gdateval_isdual(gdv)) { /* build 2nd date string into ymd2 */ format_day(gdv->date2.day, dfmt, daystr); smo = format_month(gdv->date2.calendar, gdv->date2.month, mfmt); syr = (gdv->date2.year.str ? gdv->date2.year.str : format_year(gdv->date2.year, yfmt)); format_ymd(zstr2, syr, smo, daystr, sfmt); format_eratime(zstr2, gdv->date2.eratime, efmt); if (gdv->date2.calendar) { format_cal(zstr2, gdv->date2.calendar); } } zstr3 = format_complex(gdv, cmplx, zs_str(zstr), zs_str(zstr2)); zs_free(&zstr); zs_free(&zstr2); free_gdateval(gdv); return zstr3; } }
static void csv_write_var__ (struct csv_writer *w, const struct csv_var *cv, const union value *value) { const char *label; label = val_labs_find (cv->val_labs, value); if (label != NULL) csv_output_string (w, label); else if (cv->width == 0 && value->f == SYSMIS) csv_output_buffer (w, " ", 1); else if (w->opts.use_print_formats) csv_output_format (w, cv, value); else { char s[MAX (DBL_STRLEN_BOUND, 128)]; char *cp; switch (cv->format.type) { case FMT_F: case FMT_COMMA: case FMT_DOT: case FMT_DOLLAR: case FMT_PCT: case FMT_E: case FMT_CCA: case FMT_CCB: case FMT_CCC: case FMT_CCD: case FMT_CCE: case FMT_N: case FMT_Z: case FMT_P: case FMT_PK: case FMT_IB: case FMT_PIB: case FMT_PIBHEX: case FMT_RB: case FMT_RBHEX: case FMT_WKDAY: case FMT_MONTH: dtoastr (s, sizeof s, 0, 0, value->f); cp = strpbrk (s, ".,"); if (cp != NULL) *cp = w->opts.decimal; break; case FMT_DATE: case FMT_ADATE: case FMT_EDATE: case FMT_JDATE: case FMT_SDATE: case FMT_QYR: case FMT_MOYR: case FMT_WKYR: if (value->f < 0) strcpy (s, " "); else { int y, m, d; extract_date (value->f, &y, &m, &d); snprintf (s, sizeof s, "%02d/%02d/%04d", m, d, y); } break; case FMT_DATETIME: if (value->f < 0) strcpy (s, " "); else { int y, m, d, M, S; double H; extract_time (extract_date (value->f, &y, &m, &d), &H, &M, &S); snprintf (s, sizeof s, "%02d/%02d/%04d %02.0f:%02d:%02d", m, d, y, H, M, S); } break; case FMT_TIME: case FMT_DTIME: { double H; int M, S; extract_time (fabs (value->f), &H, &M, &S); snprintf (s, sizeof s, "%s%02.0f:%02d:%02d", value->f < 0 ? "-" : "", H, M, S); } break; case FMT_A: case FMT_AHEX: csv_output_format (w, cv, value); return; case FMT_NUMBER_OF_FORMATS: NOT_REACHED (); } csv_output_string (w, s); } }