int date(char* argstr) { date_rec* date_p; int err = 0, d, m, y; char *tokenPtr; char *months[12] = {"January","February","March","April","May","June","July", "August","September","October","November","December"}; char *day = sys_alloc_mem(2); char *month = sys_alloc_mem(2); char *year = sys_alloc_mem(4); tokenPtr = strtok(argstr," "); // tokenize those args if(tokenPtr == NULL){ sys_get_date(date_p); printf("%s %d %d\n", months[date_p->month], date_p->day, date_p->year); return err; } while (tokenPtr != NULL) { if(!strcmp(tokenPtr, "-s\0")){ tokenPtr = strtok(NULL, " "); // next token plz if(strlen(tokenPtr) != 8){ // if it ain't 8, we know they f****d up printf("Invalid date string.\n"); break; } // grab those strings strncpy(year, tokenPtr, 4); strncpy(month, tokenPtr + 4, 2); strncpy(day, tokenPtr + 6, 2); // leading zeros? eh, let's make em ints anyway... m = atoi(month)-1; d = atoi(day); y = atoi(year); // are they even valid? if(validDate(d,m,y)){ date_p->day = d; date_p->month = m; date_p->year = y; err = sys_set_date(date_p); printf("Changing system date to %s %d %d\n", months[m], d, y); } else{ printf("Invalid date string.\n"); break; } } if(!strcmp(tokenPtr, "-h\0")){ printf("Usage of date:\n\tdate -s yyyymmdd\tChange the system date to the date specified.\n"); } tokenPtr = strtok(NULL, " "); } return err; }
void test(int index) { char output[100]; char s[10]; strcpy(s, testcase[index].t_date); int d1, d2, m1, m2, y1, y2, y3, y4; d1 = (s[0] - '0'); d2 = (s[1] - '0'); m1 = (s[3] - '0'); m2 = (s[4] - '0'); y1 = (s[6] - '0'); y2 = (s[7] - '0'); y3 = (s[8] - '0'); y4 = (s[9] - '0'); if (validDate(d1 * 10 + d2, m1 * 10 + m2, y1 * 1000 + y2 * 100 + y3 * 10 + y4)) { int no = (d1 * 10) + d2 - 1; strcpy(output, date[no].number); no = m1 * 10 + m2 - 1; strcat(output, month[no].number); if (y1 != 0) { no = y1 - 1; strcat(output, x[no].number); strcat(output, "thousand "); } if (y2 != 0) { no = y2 - 1; strcat(output, x[no].number); strcat(output, "hundred and "); } if (y3 <= 1) { no = (y3 * 10) + y4 - 1; strcat(output, x[no].number); } else { no = y3 + 17; strcat(output, x[no].number); if (y4 > 0){ no = y4 - 1; strcat(output, x[no].number); } } } else strcpy(output, "invalid date"); if (strcmp(output, testcase[index].e_form)) printf("FAIL\n"); else printf("PASS\n"); }
int DateClass::setDate(tm date, string &cause){ int causeCode=0; causeCode |= this->setSec(date.tm_sec); causeCode |= this->setMin(date.tm_min); causeCode |= this->setHour(date.tm_hour); causeCode |= this->setMday(date.tm_mday); causeCode |= this->setMday(date.tm_mday); causeCode |= this->setMon(static_cast<Months>(date.tm_mon)); causeCode |= this->setYear(date.tm_year); causeCode |= this->setMday(date.tm_mday); causeCode |= this->setWday(); causeCode |= this->setYday(); if (validDate(causeCode,cause)== -1){ initiateAttributes(); return -1; }else{ return 1; } }