说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年、月、日、小时、分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示。

一.对相关函数的学习

1.time_t time(time_t *second);

a.头文件:<time.h>

b.该函数返回自纪元1970-01-01 00:00:00 起经过的秒数,这是一个很大的数,若 second 不为NULL,则返回值将自动存储在该指针中。

注意:此处的 time_t 以及 __time32_t 都是 long 的别名,其实也就是long int 变量类型。

2.struct tm * localtime (const time_t * timer);

a.头文件:<stdio.h>

b. 该函数的参数为从1990.1.1 00:00:00 到现在累积的秒数,即上个函数的返回值取地址,其返回值为一个结构体类型,其中包含了日期、小时等数据成员;具体见下表:

3.int kbhit(void);

a.头文件:<conio.h>

b.该函数的功能在于检测当前是否有键盘按键按下,如果有则返回一个非 0 值,否则返回0.

3.void sleep(unsigned second);

a.头文件:<unistd.h>

b.该函数能把进程挂起一段时间,单位为秒,无返回值。

4.void usleep(int micro_second);

a.头文件:<unistd.h>

b.把进程挂起一段时间,单位为微秒,无返回值。这个函数实测在 windows Qt编译器下不准确。(网络上解释:该函数不能用在在windows系统下,只能用于 linux 的测试环境下面。)

注意:usleep() 与sleep() 类似,都用于将进程挂起一段时间。当需延迟时间数量级为秒的时候,尽量使用 sleep(),当为几十毫秒或者更小时,使用 usleep() 更精确。

5.int system(char *command);

a.头文件:<stdlib.h>

b.执行一个 windows 的 DOS 命令。这里主要讲一个命令:清屏。用来定时的刷新显示屏上的系统时间。在windows环境下,该函数为 system(“cls”);而在linux环境下,该函数为system(“clear”);并且,在linux环境下,也可以用如下语句来代替该语句:puts(“\033c”),printf(“\033c”)。两种方式也存在一定的差别,system()函数在调用命令时会“占用 ”一定的进程时间,如果需要精确的定时会导致定时不准确。而puts(“\033c”)和printf(“\033c”)则相对来说占用系统进程时间较少,用于定时比较精确。

c.system 的一些其他常用命令

(1.)system(“Date /T”) 为获取当前系统时间并显示,显示方式为 2018/10/17 周三,system(“Time /T”) 为获取当前系统时间并显示,显示方式为 09:56;运行程序后时间不会自动刷新。

(2.)system(“title XX”) 设置命令台窗口的标题为XX。

(3.)system(“mode con cols = n lines = m”) 设置窗口的宽度和高度。

(4.)system(“color 0B”) 设置窗口的颜色,其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色。

(5.)system(“shutdown –p”) 为立即关闭计算机;system(“shutdown –l”) 为注销计算机(不关闭);system(“shutdown -s“)关闭计算机(默认一分钟关);system(“shutdown -s –t num“)定时num 秒后关闭计算机,system(“shutdown -r”) 为重启计算机;system(“shutdown -r –t num“) 定时重启计算机(默认一分钟后重启);system (“shutdown -a”) 取消定时任务。

(6.)system("pause") 可以实现冻结屏幕,便于观察程序的执行结果。

(7.)system("del c::xxx.txt") 删除文件。

(8.)system("dir C:\\Users\\YJ\\Desktop\\CPlusExample") 查看文件目录的详细信息。

(9.)system(“start C:\\xxx\\YYY.exe”);执行 YYY.exe,strat 可以省略。

以下程序用到了以上的大部分函数,实现功能:显示当前程序运行的时间,实现关机管理。可以选择定时关机、立即关机、注销(不关机)以及退出此程序等选项,代码如下:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
int print()
{
printf(" ======================= \n");
printf(" ===C Turn Off Programa===\n");
printf("1.Turn off after some minute\n");
printf("2.Turn off at once\n");
printf("3.Logout\n");
printf("4.Exit\n");
printf("========================\n");
return 0;
}
int main()
{
system("title C Turn Off Program");
system("mode con cols=48 lines=15");
system("color 0B");
system("DAte /T");
system("TiME /T");
char cmd[20]="shutdown -s -t ";
char t[5]="";
print();
int c;
scanf("%d",&c);
getchar();
switch(c)
{
case 1:printf("The Waiting Time for Turn Off (0~600)\n");
scanf("%s",t);
system(strcat(cmd,t));
int d = 0;
printf("\n\nYou can press 1 to end this off system!!\n");
scanf("%d",&d);
getchar();
if(d == 1)
system("shutdown -a");
break;
case 2:system("shutdown -p");break;
case 3:system("shutdown -l");break;
case 4:break;
default:printf("Error!\n");
}
system("pause");
exit(0);
}

查看代码

二.数字式时钟的实现

代码功能:获取系统实时的时间(包括年、月、日、小时、分钟以及秒)等,显示在屏幕上,并刷新屏幕。

 #include<time.h>
#include<iostream>
#include<unistd.h>
#include<conio.h>
using namespace std;
int main()
{
while(!kbhit())
{
time_t t = time(NULL);
struct tm *localt = localtime(&t);
system("cls");
cout<<'\n'<<"\t\t";
cout<<localt->tm_year+1900<<'\\';
cout<<localt->tm_mon+1<<'\\';
cout<<localt->tm_mday<<'\t';
cout<<localt->tm_hour<<':',
cout<<localt->tm_min<<':',
cout<<localt->tm_sec;
sleep(1);
}
return 0;
}

程序运行结果:

(C/C++学习)12.获取系统时间制作时钟(system()略解)的更多相关文章

  1. Android获取系统时间的多种方法

    Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...

  2. Android获取系统时间方法的总结

    Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...

  3. c++ 怎样获取系统时间

    c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...

  4. shell下获取系统时间

    shell下获取系统时间的方法直接调用系统变量 获取今天时期:`date +%Y%m%d` 或 `date +%F` 或 $(date +%y%m%d) 获取昨天时期:`date -d yesterd ...

  5. VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  6. 获取系统时间的DOS命令

    DOS C:\Users\yaozhendong>echo %date:~0,10% %time%2011/12/24 19:45:41.25 前段时间工作中需要对一个地址做定时PING操作,并 ...

  7. 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  8. 用PHP获取系统时间时,时间比当前时间少8个小时

    自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...

  9. C/C++获取系统时间

    C/C++获取系统时间需要使用Windows API,包含头文件"windows.h". 系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义: ty ...

随机推荐

  1. jedis 2.7.2 jar

    jedis 2.7.2 已经公布. 源码https://github.com/xetorthio/jedis/releases/tag/jedis-2.7.2 jar 下载地址  http://dow ...

  2. 2015南阳CCPC H - Sudoku 数独

    H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny g ...

  3. pymysql 本地不抱错 连接云数据库报错

    import pymysql h, pt, u, p, db = 'rm-2zeoye0j3957bw1w2.mysql.rds.aliyuncs.com', 3306, 'tongji_2017', ...

  4. Chrome格式化JavaScript

    在network或者source的tab中找到对应的JavaScript文件 重点在右下角的{}图标,点击一下,就会帮你自动格式化了 https://plus.google.com/+AddyOsma ...

  5. SQL 2005批量插入数据的二种方法

    SQL 2005批量插入数据的二种方法 Posted on 2010-07-22 18:13 moss_tan_jun 阅读(2635) 评论(2) 编辑 收藏 在SQL Server 中插入一条数据 ...

  6. Hadoop 的使用

    hadoop:hadoop启动:./sbin/start-dfs.shhadoop关闭:./sbin/stop-dfs.shbin文件用于在HDFS创建数据HDFS 中创建用户目录:./bin/hdf ...

  7. 51. ExtJs4之Ext.util.JSON编码和解码JSON对象

    转自:https://blog.csdn.net/iteye_9439/article/details/82518158 1.decode() 该方法用于将符合JSON格式的String进行解码成为一 ...

  8. ubuntu 更显列表 [Connecting to archive.ubuntu.com (2001:67c:1360:8001::21)] 超时的解决方法

    问题描述: 在使用apt-get update 时更行列表,显示[Connecting to archive.ubuntu.com (2001:67c:1360:8001::21)]超时 分析: 我已 ...

  9. C# System.Environment.GetFolderPath的使用 [转]

    原文:https://blog.csdn.net/yongyong521/article/details/75105853 获取系统文件目录 string strPath = Environment. ...

  10. Centos7 时间不正确修复

    查看系统支持的时区列表 timedatectl list-timezones 使用 date -R 查看时区是否正确 date -R 修改时区 timedatectl set-timezone Asi ...