All pastes #757281 Raw Edit

Add one second

public c v1 · immutable
·published 2007-11-01 03:56 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <time.h>struct Time{	int seconds;	int minutes;	int hours;};int		IsTimeValid(const struct Time* timePtr);void	AddOneSecond(struct Time* timePtr);void	AddSeconds(struct Time* timePtr, int seconds);// === main ====================================================================//  // =============================================================================int main(void){	struct Time smallTime;	int num;		printf("Enter the start time in HH:MM:SS format: ");		if (3 != scanf("%d:%d:%d", &smallTime.hours, &smallTime.minutes				   , &smallTime.seconds))		{		puts("Error reading input...");		exit(1);		}		// check if time entered is valid	auto	int	notvalid = IsTimeValid(&smallTime);		if (notvalid)		{		exit(1);		}		printf("How many seconds would you like to add? ");	scanf("%d", &num);		while (num--)		{		AddOneSecond(&smallTime);		printf("%02d:%02d:%02d\n", smallTime.hours, smallTime.minutes,					smallTime.seconds);		}			return 0;} // end of "main"// === IsTimeValid =============================================================//  // =============================================================================int		IsTimeValid(const struct Time* timePtr){	int invalidMask = 0;	// 000	// hms	if ((timePtr->seconds > 59) || (timePtr->seconds < 0))		{		invalidMask |= 1;		}		if ((timePtr->minutes > 59) || (timePtr->minutes < 0))		{		invalidMask |= 2;		}	if ((timePtr->hours > 23) || (timePtr->hours < 0))		{		invalidMask |= 4;		}	// print error mesasge	if (invalidMask)		{		printf("Invalid data: the ");		if (invalidMask & 4)			{			printf("hours ");			invalidMask ^= 4;			if (invalidMask)				{				printf("and ");				}			}		if (invalidMask & 2)			{			printf("minutes ");			invalidMask ^= 2;			if (invalidMask)				{				printf("and ");				}			}		if (invalidMask & 1)			{			printf("seconds ");			}		puts("are out of range...");		}	return invalidMask;} // end of "IsTimeValid"void	AddOneSecond(struct Time* timePtr){	char 	timeStr[10];		// Store the current time in the myTime tm structure (from stdlib)	// so that it's valid when passed to mktime.	time_t	myTime_t = time(&myTime_t);		struct	tm *myTime = localtime(&myTime_t);		myTime->tm_sec = timePtr->seconds + 1;	myTime->tm_min = timePtr->minutes;	myTime->tm_hour = timePtr->hours;		// corrects the values of myTime	mktime(myTime);		timePtr->seconds = myTime->tm_sec;	timePtr->minutes = myTime->tm_min;	timePtr->hours = myTime->tm_hour;		/* print	strftime(timeStr, 10, "%H:%M:%S", myTime);	printf("%s\n", timeStr);	*/}void	AddSeconds(struct Time* timePtr, int num){	char 	timeStr[10];	time_t	myTime_t;	myTime_t = time(&myTime_t);		struct tm *myTime = localtime(&myTime_t);		myTime->tm_sec = timePtr->seconds + num;	myTime->tm_min = timePtr->minutes;	myTime->tm_hour = timePtr->hours;		// corrects time	mktime(myTime);		timePtr->seconds = myTime->tm_sec;	timePtr->minutes = myTime->tm_min;	timePtr->hours = myTime->tm_hour;		/* print	strftime(timeStr, 10, "%H:%M:%S", myTime);	printf("myTime: %s\n", timeStr);	*/}