System Time
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | // https://www.runoob.com/w3cnote/c-time-func-summary.html
#include <stdio.h>
#include <time.h>
int main ()
{
time_t curtime;
time(&curtime);
printf("Current Time = %s", ctime(&curtime));
return(0);
}
|
Result:
Current Time = Mon Aug 2 16:48:29 2021
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | #include <stdio.h>
#include <inttypes.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <unistd.h> // sleep
void timediff()
{
static struct timeval record_stamp;
struct timeval curr_stamp;
gettimeofday(&curr_stamp, NULL);
if (0 == record_stamp.tv_sec && 0 == record_stamp.tv_usec)
{
printf(">>> record time stamp\n");
}
else
{
printf(">>> diff time stamp\n");
double secs = 0;
secs = (double)(curr_stamp.tv_usec - record_stamp.tv_usec) / 1000000 + (double)(curr_stamp.tv_sec - record_stamp.tv_sec);
printf(">>> time taken %16f\n", secs);
}
record_stamp = curr_stamp;
}
int main(void)
{
timediff();
usleep(300);
timediff();
sleep(1);
timediff();
return 0;
}
|
Result: