user641902
user641902

Reputation: 87

a time function in C linux

hi i was wondering if it is possible to edit the time function.for my program to give a time for a user to retry again?

struct tm * abc()
{
char *time_string;
time_t curtime;
struct tm *loctime;

/* Get the current time.  */
curtime = time (NULL);

/* Convert it to local time representation.  */
loctime = localtime (&curtime);
return loctime;
}

this will return the current time but want i want to do is to edit this to add a value. The reason for me doing this is so i can tell a user to try again at a certain time using the current and adding 2minutes to it. Not sure if this is the correct way? Thanks

Upvotes: 0

Views: 4011

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70979

Add the following lines just before the return.

  localtime->tm_min += 2;
  mktime(localtime);

The first line adds two minutes to localtime. The second line renormalizes localtime to a "standard" format, in other words you it will roll the added minutes from values like (61) to values like (hours+1),(minutes = 1).

Upvotes: 0

Erik
Erik

Reputation: 91320

time() returns a timestamp in seconds (number of seconds since the epoch), so you can just add the required delay.

curtime = time (NULL) + 2*60; // Adds two minutes, 

Upvotes: 3

Related Questions