Thread: Convert time difference into seconds?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    Exclamation Convert time difference into seconds?

    I am writing an alarm clock program. Basically, it takes a time from the user and converts the difference between that time and the current into seconds. However, this function only returns the number of seconds the user gives. That is to say, it never seems to execute the if statements.
    I.E. -
    Code:
    Enter the hour- 12
    
    Enter the minute- 00
    
    Enter the seconds- 00
    
    Sat Aug 13 12:00:00 2011
    Total number of seconds = 0
    This is the current time ===> Sat Aug 13 11:58:19 2011
    There is probably a better way to achieve the time difference conversion; I would be open to suggestions.

    Code:
    int TimeDifference(time_t time, time_t now)
    {
    	int Hour, Min; 
    	long int Sec;
    	struct tm *currentTime;
    	currentTime = localtime (&now);
    
    	struct tm *timeinfo;
    	timeinfo = localtime (&time);
    	Sec = timeinfo->tm_sec;
    
    	if (currentTime->tm_min != timeinfo->tm_min)
    	{
    	Min = (timeinfo->tm_min - currentTime->tm_min);
    	Sec += (Min * 60);
    	}
    
    
    	if (currentTime->tm_hour != timeinfo->tm_hour)
    	{
    	Hour = (timeinfo->tm_hour - currentTime->tm_hour);
    	Sec += (Hour * 60 * 60);	//to minutes then to seconds
    	}
    
    	printf("Total number of seconds = %li", Sec);
    
    	return Sec;
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There is probably a better way to achieve the time difference conversion; I would be open to suggestions.
    Code:
    int TimeDifference(time_t time, time_t now)
    {
        return (int)difftime(now, time);
    }
    My best code is written with the delete key.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Prelude's answer is the way to go, on the assumption you've filled a time_t with the user info. You might have gotten a hint if you had checked "currentTime==timeinfo"; my man page says
    The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions.
    and yours very well may too.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    Alright, thanks guys. I knew I was making this more complicated then it should be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  2. Time in seconds
    By udaraliyanage in forum C Programming
    Replies: 1
    Last Post: 04-09-2011, 07:21 AM
  3. difference between Convert & Int32
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 12-06-2005, 04:23 PM
  4. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  5. Replies: 3
    Last Post: 11-15-2003, 11:20 AM

Tags for this Thread