Her er der en masse eksempler hvordan du kan bruge time du kan så selv vælge det eksempel der passer bedst
-------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
/* The __USE_XOPEN definition is needed to force the declaration
of strptime() in time.h. */
#define __USE_XOPEN
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
/* --------------------------------------------------------------
time
Part of the SourceLib Project at LinuxProgramming.com
http://www.linuxprogramming.com/SourceLib This sample program has been released into the public domain.
Purpose: Exercise several time-related functions for retrieving,
converting, and formatting time and date values.
Use the command \"make\" to build this program.
---------------------------------------------------------------
Our program performs a series of date manipulation operations.
We retrieve the system time as seconds since the Unix epoch,
midnight on January 1, 1970.
We then retrieve the local and GMT times and format them with
asctime(). We also format the local time with ctime().
We next build a date via hand using a tm structure, and then
convert it into seconds-since-the-epoch format via mktime(),
and then convert that from GMT to local time and format it
with asctime().
We then calculate the number of elapsed seconds since Y2K.
Finally, we demonstrate strftime() and strptime(). First we
convert our Y2K tm structure into a printable string with
strftime(), then we convert the string back into a tm structure
with strptime() and then back into a string again with strftime(),
to show that they match.
Note: You should carefully read the man pages for the various
functions used in this sample, particularly strftime and strptime,
as they have many formatting options not used in this program.
Also, pay attention to the return values, as some functions, like
gmtime(), provide their output via a pointer to a structure.
--------------------------------------------------------------- */
#define buffer_size 1000
int main(int argc, char **argv)
{
time_t now_time_t;
struct tm *now_tm;
struct tm y2k_tm;
struct tm *y2k_tm_ptr;
struct tm y2k_tm_converted;
char *buffer, *nl;
time_t y2k_time_t;
double time_since_y2k;
char strftime_buffer[buffer_size];
struct timeval start_time, end_time;
gettimeofday(&start_time,NULL);
now_time_t = time(NULL);
printf(\"Current time, as a time_t integer: %ld\\n\",now_time_t);
now_tm = gmtime(&now_time_t);
buffer = asctime(now_tm);
if((nl = strrchr(buffer,\'\\n\')) != NULL)
*nl = \'\\0\';
printf(\"Current time, GMT, formatted by asctime = \\\"%s\\\".\\n\",
buffer);
now_tm = localtime(&now_time_t);
buffer = asctime(now_tm);
if((nl = strrchr(buffer,\'\\n\')) != NULL)
*nl = \'\\0\';
printf(\"Current time, local, formatted by asctime = \\\"%s\\\".\\n\",
buffer);
buffer = ctime(&now_time_t);
if((nl = strrchr(buffer,\'\\n\')) != NULL)
*nl = \'\\0\';
printf(\"Current time formatted by ctime = \\\"%s\\\".\\n\",
buffer);
y2k_tm.tm_sec = 0; /* Seconds, 0..61 to allow for leap sec. */
y2k_tm.tm_min = 0; /* Minutes, 0..59 */
y2k_tm.tm_hour = 0; /* Hours, 0..23 */
y2k_tm.tm_mday = 1; /* Day of month, 1..31 */
y2k_tm.tm_mon = 0; /* Month, 0..11 */
y2k_tm.tm_year = 100; /* Years since 1900 */
/* The tm_wday and tm_yday fields do NOT have to be specified */
/* Convert our tm struct into a time integer */
y2k_time_t = mktime(&y2k_tm);
/* Convert it back to a struct representing local time */
y2k_tm_ptr = localtime(&y2k_time_t);
buffer = asctime(y2k_tm_ptr);
if((nl = strrchr(buffer,\'\\n\')) != NULL)
*nl = \'\\0\';
printf(\"Y2k, local, formatted by asctime = \\\"%s\\\".\\n\",
buffer);
/* Calculate the number of seconds from 1/1/2000 until now */
time_since_y2k = difftime(now_time_t,y2k_time_t);
printf(\"Seconds elapsed since Y2K: %f\\n\",time_since_y2k);
/* Use strftime() to do a little custom formatting. */
if(strftime(strftime_buffer,buffer_size,
\"%A, %B %e, %Y at %r\",y2k_tm_ptr) == 0)
{
printf(\"Unable to perform strftime conversion!\\n\");
exit(EXIT_FAILURE);
}
printf(\"A custom formatted version of Y2K, using strftime: \\\"%s\\\"\\n\",
strftime_buffer);
if(strptime(strftime_buffer,\"%A, %B %e, %Y at %r\",&y2k_tm_converted) == NULL)
{
printf(\"Unable to perform strptime conversion!\\n\");
exit(EXIT_FAILURE);
}
if(strftime(strftime_buffer,buffer_size,
\"%A, %B %e, %Y at %r\",&y2k_tm_converted) == 0)
{
printf(\"Unable to perform strftime conversion!\\n\");
exit(EXIT_FAILURE);
}
printf(\"After converting the above date string to a tm structure with strptime\\nand then back to a string with strftime, we have: \\\"%s\\\"\\n\",
strftime_buffer);
printf(\"\\nHit ENTER, and we can calculate the running time for this program, using\\n\");
printf(\"the gettimeofday() function.\\n\");
getchar();
gettimeofday(&end_time,NULL);
end_time.tv_sec -= start_time.tv_sec;
end_time.tv_usec -= start_time.tv_usec;
if(end_time.tv_usec < 0)
{
end_time.tv_sec--;
end_time.tv_usec += 1000000;
}
printf(\"The elapsed time was %ld.%06ld seconds\\n\",end_time.tv_sec,end_time.tv_usec);
/* Indicate normal termination via the
EXIT_SUCCESS constant from stdlib.h */
return EXIT_SUCCESS;
}