/* Some time fetching function Written by Jose Mari Reyes www.angelfire.com/co2/xtechnica jet_reyes@jetemail.net wufei_ph@yahoo.com */ #include #include #include void fetch_time24(char *currtime) { time_t ltime; struct tm *today; time( <ime ); today = localtime( <ime ); /* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ sprintf(currtime,"%.8s", asctime( today ) + 11); } void fetch_time(char *currtime) { char ampm[] = "AM"; time_t ltime; struct tm *today; time( <ime ); today = localtime( <ime ); if( today->tm_hour > 12 ) { strcpy( ampm, "PM" ); today->tm_hour -= 12; } if( today->tm_hour == 0 ) /* Adjust if midnight hour. */ today->tm_hour = 12; /* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ sprintf(currtime,"%.8s%s", asctime( today ) + 11, ampm ); } void fetch_date(char *currdate) { struct tm *ntime; time_t long_time; int mo, day, year; char strmo[3], strday[3]; time( &long_time ); /* Get time as long integer. */ ntime = localtime( &long_time ); /* Convert to local time. */ mo = ntime->tm_mon + 1; day = ntime->tm_mday; year = ntime->tm_year + 1900; if (mo < 10) sprintf(strmo, "0%d", mo); else sprintf(strmo, "%d", mo); if (day < 10) sprintf(strday, "0%d", day); else sprintf(strday, "%d", day); sprintf(currdate, "%s/%s/%d",strmo, strday, year); } void main() { char currdate[30]; char currtime[30]; char curr24time[30]; fetch_date(currdate); printf("\n%s",currdate); fetch_time(currtime); printf("\n%s",currtime); fetch_time24(curr24time); printf("\n%s",curr24time); getchar(); }