-
Notifications
You must be signed in to change notification settings - Fork 0
/
gettiem.c
98 lines (69 loc) · 1.66 KB
/
gettiem.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*===============================================================
* 文件名称:gettiem.c
* 创建日期:2016年06月22日
================================================================*/
#include <stdio.h>
#include <time.h>
int libutil_getPONIfUpTime(int *vptime)
{
time_t upTime = 0;
FILE *f = fopen("/home/yaweili/test/o5time.log", "r");
if(NULL != f)
{
char *line = NULL;
size_t len = 0;
ssize_t r = 0;
r = getline(&line, &len, f);
if(-1 == r)
{
return -1;
}
if(NULL != line)
{
upTime = atoi(line);
free(line);
line = NULL;
}
}
fclose(f);
time_t curTime = 0;
time(&curTime);
*vptime = curTime - upTime;
return 0;
}
int main(void)
{
time_t timep = 0;
time_t timeq = 0;
struct tm *p;
int uptime = 0;
time(&timep);
printf("%s\n", asctime(gmtime(&timep)));
printf("%s\n",ctime(&timep));
printf("time(): %d\n", timep);
p = localtime(&timep);
timep = mktime(p);
printf("mktime(): %d\n", timep);
char cmd[128] = {0};
snprintf(cmd, sizeof(cmd)-1, "echo %d > /home/yaweili/test/o5time.log", timep);
system(cmd);
FILE *f = fopen("/home/yaweili/test/o5time.log", "r");
if(f)
{
char *line = NULL;
size_t len = 0;
ssize_t r = 0;
getline(&line, &len, f);
if(line)
{
printf("line: %s", line);
free(line);
line = NULL;
}
}
fclose(f);
sleep(5);
libutil_getPONIfUpTime(&uptime);
printf("time; %d\n", uptime);
return 0;
}