OS Libraries

Motivation

Character handling

String handling

Date handling

Example: What day of the week is Nov. 24 1995?

#include <stdio.h>
#include <time.h>

int main()
{
	struct tm high; /* "high-level" time: month, day, year, etc. */

	/* given a date, what day of the week is it? */
	high.tm_sec = 0;
	high.tm_min = 30;
	high.tm_hour = 13;
	high.tm_mday = 24;
	high.tm_mon = 10;
	high.tm_year = 95;
	mktime(&high); /* to compute day of week */
	printf("day of week: %d\n",high.tm_wday);
	printf("%s",asctime(&high)); /* human-readable form */

	return 0;
}

Example: What is the date 90 days from Nov. 24 1995?

#include <stdio.h>
#include <time.h>

int main()
{
	struct tm high; /* high-level" time: month, day, year, etc. */

	/* today's date */
	high.tm_sec = 0;
	high.tm_min = 30;
	high.tm_hour = 13;
	high.tm_mday = 24;
	high.tm_mon = 10;
	high.tm_year = 95;

	/* increment */
	high.tm_mday += 90;

	/* to normalize */
	mktime(&high);

	/* display */
	printf("%s",asctime(&high));

	return 0;
}

Example: How many days in the F96 term?

#include <stdio.h>
#include <time.h>

int main()
{
	struct tm high0,high1; /* high-level" time: month, day, year, etc. */
	time_t low0,low1; /* low-level time: ticks since Jan. 1, 1900 */
	int diff;

	/* 13:30 on first day of class F96 */
	high0.tm_sec = 0;
	high0.tm_min = 30;
	high0.tm_hour = 13;
	high0.tm_mday = 4;
	high0.tm_mon = 8;
	high0.tm_year = 96;

	/* 13:30 on last day of class F96 */
	high1.tm_sec = 0;
	high1.tm_min = 30;
	high1.tm_hour = 13;
	high1.tm_mday = 4;
	high1.tm_mon = 11;
	high1.tm_year = 96;

	/* convert to low-level times */
	low0 = mktime(&high0);
	low1 = mktime(&high1);

	/* calculate the difference in seconds */
	diff = difftime(low1,low0);

	/* convert to days */
	diff = diff / (24*60*60);

	/* display */
	printf("%d\n",diff);

	return 0;
}

Bottom line