我是靠谱客的博主 瘦瘦蜗牛,这篇文章主要介绍Day6-C语言-linux操作系统下的计时器,现在分享给大家,希望可以做个参考。

目录

      • Day6
        • linux操作系统下的计时器
        • time函数
        • localtime函数
        • gmtime函数
        • linux操作系统,计时器

Day6

linux操作系统下的计时器

time函数

取得目前的时间返回秒数

复制代码
1
2
time_t time(time_t *t);

linuxTime1.c

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h> #include<sys/time.h> #include <unistd.h> /** * @brief 获取此时的时间 */ int main() { time_t t; printf("现在的时间是:"); time(&t); printf("%d",t); return 0; }

localtime函数

返回值 返回结构tm代表目前的当地时间。

复制代码
1
2
struct tm *localtime(const time_t * timep);

gmtime函数

此函数返回的时间日期未经时区转换,而是UTC时间。

复制代码
1
2
struct tm*gmtime(const time_t*timep);

linuxTime1s.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
#include <stdio.h> #include<sys/time.h> #include <unistd.h> /** * @brief 获取此时的时间 UTC 时间 和 本地时区 */ int main() { time_t t; struct tm *now; time(&t); //取得目前的时间(单位:s) now = gmtime(&t); //UTC 时间 printf("%d年%d月%d日 %d时%d分%d秒n", 1900 + now->tm_year, 1 + now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); now = localtime(&t); //当地时区 printf("%d年%d月%d日 %d时%d分%d秒n", 1900 + now->tm_year, 1 + now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec); printf("s:%dn", t); printf("min:%dn", t/60); printf("hour:%dn", t/60/60); printf("day:%dn", t/60/60/24); printf("year:%dn", t/60/60/24/365); printf("所以time()函数是从1970年1月1日开始算"); return 0; }

运行结果:

复制代码
1
2
3
4
5
6
7
8
20217227536202172215536秒 s:1626937536 min:27115625 hour:451927 day:18830 year:51

linux操作系统,计时器

linuxTime2s

复制代码
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
#include <stdio.h> #include <time.h> #include <unistd.h> /** * @brief 定时提醒 * 输入 年月日,时分秒 * 输入时间要比现在时间只早不晚,否则报错、重输 */ //作用:将给定的 年月日时分秒 转化为和 函数time() 的返回值 相同 time_t convert(int y, int m, int d, int h, int min, int s) { time_t nowTime, userTime; //现在时间,用户输入时间 struct tm *nowInfo, userInfo; //同上 time(&nowTime); //获取此刻时间 nowInfo = localtime(&nowTime); //当地时区 printf("此刻时间:%d年%d月%d日 %d时%d分%d秒n", 1900 + nowInfo-> tm_year, 1 + nowInfo->tm_mon, nowInfo->tm_mday, nowInfo->tm_hour, nowInfo-> tm_min, nowInfo->tm_sec); userInfo.tm_year = y - 1900; userInfo.tm_mon = m - 1; userInfo.tm_mday = d; userInfo.tm_hour = h; userInfo.tm_min = min; userInfo.tm_sec = s; userTime = mktime(&userInfo); int res = userTime - nowTime; printf("res:%dn",res); if (res > 0) { return res; } return -1; } int main() { time_t t; int y, m, d, h, min, s; printf("程序启动...nn"); printf("请您输入提醒时间:n"); printf("Please enter the reminder time:n"); do { printf("年:"); scanf("%d", &y); printf("n月:"); scanf("%d", &m); printf("n日:"); scanf("%d", &d); printf("n时:"); scanf("%d", &h); printf("n分:"); scanf("%d", &min); printf("n秒:"); scanf("%d", &s); t = convert(y, m, d, h, min, s); if (t == -1) printf("输入时间错误,请重新输入...n"); } while (t == -1); int j = 1; for (int i = 0; i < t; i++) { sleep(1); //休眠1s printf("%dn", j++); } printf("时间到nn"); return 0; }

运行结果:

复制代码
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
gcc time1.c -o time1 ./time1 程序启动... 请您输入提醒时间: Please enter the reminder time::2021:7:22:22:35:30 此刻时间:202172222355秒 res:25 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 时间到

最后

以上就是瘦瘦蜗牛最近收集整理的关于Day6-C语言-linux操作系统下的计时器的全部内容,更多相关Day6-C语言-linux操作系统下内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部