当前位置:网站首页>『C语言』系统日期&时间
『C语言』系统日期&时间
2022-06-24 06:46:00 【謓泽】
write in front
大家好,我是謓泽,希望你看完之后,能对你有所帮助,不足请指正!共同学习交流
2021年度博客之星物联网与嵌入式开发TOP5~2021博客之星Top100~阿里云专家 ^ 星级博主~掘金⇿InfoQ创作者~周榜77»总榜1766
本文由 謓泽 原创 CSDN首发 如需转载还请通知
个人主页-謓泽的博客_CSDN博客
欢迎各位→点赞 + 收藏️ + 留言
系列专栏-YY_謓泽的博客-CSDN博客
️我们并非登上我们所选择的舞台,演出并非我们所选择的剧本

目录「系统日期&时间」↓
⒈题目内容
输出系统的日期以及时间。
在本代码当中只有一个main函数将各个控制命令保存在数组当中,然后适用循环语句设置一个死循环。在该循环当中让用户输入命令指令,并且判断用户输入的命令是否和数组当中存储的命令是否相同。如果它们是相同的,则执行相对应的内容。
⒉题目要求
用户进行某一个操作需要输入一个命令,如果命令输入错误,系统会进行提示。
当用户输入命令字符"0"会显示帮助信息。
当用户输入命令字符"1"会显示系统日期。
当用户输入命令字符"2"会显示系统时间。
当用户输入命令字符"3"会执行退出系统。
⒊思考问题
一:需要保证程序能够一直执行下去,等待用户的命令防止主函数结束。
二:获取系统日期和系统时间。
⒋解题思路
结构体struct tm当中的结构成员如下↓
int tm_sec 分后的秒(0-61) 多出来的两秒是用来处理跳秒问题用的 int tm_min 小时后的分(0-59) int tm_hour 午夜后的小时(0-23) int tm_mday 月中的天(0-31) 本月第几日 int tm_mon 一月后的月数(0-11) 本年第几月 int tm_year 1900年后的年数,要加1900表示那一年 int tm_wday 星期日后的天数(0-6) 本周第几日 int tm_yday 一月一日后的天数(0-365),本年第几日,闰年有366日 int tm_isdst 夏令时标志(大于0的值说明夏令时有效,0说明无效,负数说明信息不可用)¹time - 库函数
描述
C语言当中的库函数 time_t time(time_t *seconds) 返回自纪元 Epoch(1970-01-01 00:00:00 UTC)起经过的时间,以秒为单位。如果 seconds 不为空,则返回值也存储在变量 seconds 中。
声明
下面是 time() 函数的声明。
time_t time(time_t *seconds)注→这个存储的类型是时间类型也就是time_t在我们获取系统日期之前我们需要定义一个时间类型的变量。
参数
seconds -- 这是指向类型为 time_t 的对象的指针,用来存储 seconds 的值。
返回值
以 time_t 对象返回当前日历时间。
²localtime - 库函数
描述
C 库函数 struct tm *localtime(const time_t *timer) 使用 timer 的值来填充 tm 结构。timer 的值被分解为 tm 结构,并用本地时区表示。
声明
下面是 localtime() 函数的声明。
struct tm *localtime(const time_t *timer)参数
timer -- 这是指向表示日历时间的 time_t 值的指针。
返回值
该函数返回指向 tm 结构的指针,该结构带有被填充的时间信息。
⒌程序代码
系统日期&时间 → 代码示例如下↓
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<time.h> #include<Windows.h> //结构体指针变量指向(->)的就是结构体类型当中成员变量 struct tm* fun_Time; void color(short x) { if (x >= 0 && x <= 15) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } void menu() { color(0); system("cls"); color(10); printf("|--------- 系统&时间 --------|\n"); printf("|-------* 1.系统日期 *------|\n"); printf("|-------* 2.系统时间 *------|\n"); printf("|-------* 3.退出系统 *------|\n"); } void Time() { time_t Time;//定义时间类型 time(&Time);//获取系统日期 //注:指针变量接收地址,在这里我们定义了结构体指针变量所以只需要用结构体指针当中变量用来存储地址即可。 //struct tm* fun_Time = localtime(&Time);同理 fun_Time = localtime(&Time);//转换为系统时间 } int main(void) { const int date[4] = {0,1,2,3}; int n = 0; printf("请输入[0]获取帮助信息:"); while (1) { color(1); scanf_s("%d", &n); if (date[0] == n) menu(); else if (date[1] == n) { Time();//时间属性 printf("系统日期:%d-%d-%d\n", 1900 + fun_Time->tm_year, fun_Time->tm_mon + 1, fun_Time->tm_hour); } else if (date[2] == n) { Time();//时间属性 printf("系统日期:%d:%d:%d\n", fun_Time->tm_hour, fun_Time->tm_min, fun_Time->tm_sec); } else if (date[3] == n) { printf("退出EXIT!\n"); break; } else printf("你输入的指令错误,请重新输入:"); } return 0; }⒍代码运行结果
⒈获取信息
⒉系统日期⒊系统时间⒋退出EXIT
边栏推荐
- [Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
- 图形技术之管线概念
- 简单的折射效果
- Terminal network in VPN client connection settings of router
- Global and Chinese markets for maritime transport of perishable goods 2022-2028: Research Report on technology, participants, trends, market size and share
- Deploy L2TP in VPN (medium)
- Only two lines are displayed, and the excess part is displayed with Ellipsis
- Teach you how to use the reflect package to parse the structure of go - step 2: structure member traversal
- Camera calibration (calibration purpose and principle)
- Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
![[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence](/img/c5/f548223666d7379a7d4aaed2953587.png)
[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence

Baidu map, coordinate inversion, picking coordinate position

云开发谁是卧底小程序源码

Free ICP domain name filing interface

希尔伯特-黄变换

相機標定(標定目的、原理)

Session & cookie details

Blue Bridge Cup seven segment code (dfs/ shape pressing + parallel search)

面试中的最常被问到的两种锁

LeetCode 207:课程表(拓扑排序判断是否成环)
随机推荐
Spark stage and shuffle for daily data processing
L2tp/ipsec one click installation script
Take my brother to make a real-time Leaderboard
[Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
Counter attack from outsourcing to big factories! Android has been developed for 5 years, and after a year of dormancy, it has tried to become an offer harvester. Tencent has a fixed salary of 20*15
面试中的最常被问到的两种锁
Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
Phonics
Black box and white box models for interpretable AI
Win10 build webservice
Commandes de console communes UE
[understanding of opportunity -29]: Guiguzi - internal dialogue - five levels of communication with superiors
More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
科一易错点
[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer
New features of PHP: bytecode cache and built-in server
pair类备注
The startup mode of cloudbase init is \Cloudbase init has hidden dangers
atguigu----15-内置指令
Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
