/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... read and set the RTC.
 */

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

/*
 * The current date is displayed. When a new date is entered,
 * the current date is modified and written back to the RTC.
 */
int
main(void)
{
	while (1) {
		char line[30];			// input buffer
		time_t t = time(0);		// current date

		printf("Current date: %s\n"
		       "New date (hh mm ss DD MM YYYY, no check!): ",
			ctime(&t)
		);
		if (*fgets(line, sizeof(line), stdin) != '\n') {
			struct tm *tp = localtime(&t);

			tp->tm_mon++;			// correct month
			tp->tm_year += 1900;		//         year
			sscanf(line, "%d%d%d%d%d%d",	// scan line
				&tp->tm_hour,
				&tp->tm_min,
				&tp->tm_sec,
				&tp->tm_mday,
				&tp->tm_mon,
				&tp->tm_year
			);
			tp->tm_mon--;			// tm_mon  : 0.. 11
			tp->tm_year -= 1900;		// tm_year : years since 1900
			_settime(mktime(tp), 0);	// set new date
		}
	}
}
