c++ get the current time (with example source code)

hello

When I opened Luogu today, I found

ah this

Forget it, programming relax (relax?)

C++ is a good partner who usually plays ,

There are also many fun or practical functions.

Today I will teach you how to get the current time with C++

hey-hey

This is actually quite simple

Detailed explanation of the time.h header file

definition

  • UTC: Greenwich Mean Time (GMT), Mainland China time is UTC+8, and the United States is UTC-5
  • Calendar time: the number of seconds elapsed from a standard point in time to this time, "relative time"
  • Timepoint: long integer, the number of seconds between the current time and the standard time.
  • Clock timing unit: C/C++ basic timing unit

illustrate

This can be found by looking at the code of time.h by yourself

I've sorted it out

	<time.h>
	clock_t clock(void);//从进程开始到调用clock之间的CPU时钟计时单元
	CLOCKS_PER_SEC//CPU时钟计时单元,1秒钟会有多少个时钟计时单元
	#define CLOCKS_PER_SEC ((clock_t)1000)
	time_t time(time_t* timer);//返回从1970年1月1日0时0分0秒到现在的秒数
	struct tm* gmtime(const time_t *timer);//将日历时间转化为世界标准时间(格林尼治时间)
	struct tm* localtime(const time_t *timer);//将日历时间转化为本地时间
	char* asctime(const struct tm* timeptr);//通过tm结构生成具有固定格式的时间字符串
	char* ctime(const time_t *timer);//通过日历时间生成时间字符串

example

time to string

	size_t strftime(char* strDest,size_t maxsize,const char* format,const struct tm *timeptr);
	time_t timer=time(NULL);
	char szbuf[256]={0};
	strftime(szbuf,sizeof(szbuf),"%Y-%m-%d %H:%M:%S",localtime(&timer));

The above uses time_t to get the number of seconds from 0:00:00 on January 1, 1970 to the present,

%Y is the year

%m is month

%d is a day

%H is when

%M is the cent

%S is seconds

After this operation, you can output the current time 2022-4-24 17:33:38

Because I can't use anything else for the time being today, so let's talk about this first

Example 2: Judging whether the current time is within a certain time period

int huoqu()
{
 	time_t now_time;
	now_time = time(NULL);
	struct tm *local;
	local=localtime(&now_time); //获取当前系统时间
	char buf[80];
	strftime(buf,80,"%H0%M",local);
	if ((buf[0]-'0')*10+(buf[2]-'0')==20)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

This line of if(........==20) may not be understood at first glance

This line of code means that the 24-hour hour is eight o'clock in the evening

Can determine whether the current time is between 8:00 and 9:00

Because if it's eight to nine, the time must be 20:xx

So just judge whether the hour is 20 or not.

Example 3: Anti-addiction system

In fact, I posted this before, but I changed it again today and posted it by the way.

There are repeated parts in the code game part, there is no learning value, you can copy and play by yourself

#include <iostream>
#include <string>
#include <iomanip>
#include <time.h>
#include<stdlib.h>
#include<windows.h>
#include<cstring>
#include<conio.h>
using namespace std;
 
 
int sfz()
{
    int a[18],i,n,b;          
    printf("当前未进行认证\n");
    printf("请输入身份证号:");
    for(i=0;i<18;i++)
    scanf("%1d",&a[i]);
    n=a[6]*1000+a[7]*100+a[8]*10+a[9];
    b=2022-n;
    if(b<18)
        return 0;
    else
        return 1;
}

int huoqu()
{
 	time_t now_time;
	now_time = time(NULL);
	struct tm *local;
	local=localtime(&now_time); //获取当前系统时间
	char buf[80];
	strftime(buf,80,"%H0%M",local);
	if ((buf[0]-'0')*10+(buf[2]-'0')==20)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int jiancha()
{
	time_t now_time;
	now_time = time(NULL);
	struct tm *local;
	local=localtime(&now_time); //获取当前系统时间
	char buf[80];
	strftime(buf,80,"%H0%M",local);
	if ((buf[0]-'0')*10+(buf[2]-'0')!=20)
	{
		return 0;
	}
}

int main()
{
	system("color F0");
	if (sfz()==0)
	{
		if (huoqu()==1)
		{
			start:
				cout<<"        21点"<<endl;
				cout<<"----------------------"<<endl;
				cout<<"输入1开始"<<endl;
				int n;
				cin>>n;
				if (n==1)
				{
					system("cls");
					cout<<"开始"<<endl;
					//输入昵称 
					string name1,name2;
					cout<<"请输入玩家1昵称"<<endl;
					cin>>name1;
					cout<<"请输入玩家2昵称"<<endl;
					cin>>name2;
					/**/
					string op1,op2;
					int n1=0,n2=0;
					int s1=0,s2=0;
					//玩家1 
					cout<<"请玩家1拿牌"<<endl;
					while (true)
					{
						cout<<"是否继续拿?(y/n)"<<endl;
						cin>>op1;
						if (op1=="y")
						{
							srand(time(NULL));
							s1 += (rand()%(7-1+1)+1);
							cout<<"你的牌一共有"<<s1<<endl; 
							n1++;
							if (jiancha()==0)
							{
								cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
								Sleep(1000);
								exit(0);
							}
						}
						else if (op1=="n")
						{
							cout<<"你一共拿了"<<n1<<"张牌";
							if (jiancha()==0)
							{
								cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
								Sleep(1000);
								exit(0);
							}
							break;
						}
						else
						{
							cout<<"请不要乱输!"; 
							if (jiancha()==0)
							{
								cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
								Sleep(1000);
								exit(0);
							}
						}
						if (jiancha()==0)
						{
							cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
							Sleep(1000);
							exit(0);
						}
					}
					Sleep(3000);
					system("cls");
					//玩家2 
					cout<<"请玩家2拿牌"<<endl;
					while (true)
					{
						cout<<"是否继续拿?(y/n)"<<endl;
						cin>>op2;
						if (op2=="y")
						{
							srand(time(NULL));
							s2 += (rand()%(7-1+1)+1);
							cout<<"你的牌一共有"<<s2<<endl;
							n2++;
							if (jiancha()==0)
							{
								cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
								Sleep(1000);
								exit(0);
							}
						}
						else if (op1=="n")
						{
							cout<<"你一共拿了"<<n2<<"张牌";
							if (jiancha()==0)
							{
								cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
								Sleep(1000);
								exit(0);
							}
							break;
						}
						else
						{
							cout<<"请不要乱输!"; 
							if (jiancha()==0)
							{
								cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
								Sleep(1000);
								exit(0);
							}
						}
						if (jiancha()==0)
						{
							cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
							Sleep(1000);
							exit(0);
						}
					}
					Sleep(3000);
					system("cls");
					cout<<"正在统计....."<<endl;
					Sleep(1000);
					cout<<"玩家1总分数:"<<s1<<endl;
					cout<<"玩家2总分数:"<<s2<<endl;
					if (s1<=21 && s2<=21)
					{
						if (s1>s2)    {
							cout<<"【"<<name1<<"】"<<"is the winner!!!";
							cout<<"\n按a重来";
							int chch = _getch();
							if (chch==97) goto start;
						}
						if (s1==s2)  {
							cout<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
							cout<<"\n按a重来";
							int chch = _getch();
							if (chch==97) goto start;
						}  
						if (s1<s2)  {
							cout<<"【"<<name2<<"】"<<"is the winner!!!";
							cout<<"\n按a重来";
							int chch = _getch();
							if (chch==97) goto start;
						}  
					}
					if(s1>21 && s2>21)
					{
						cout<<"两方都超过21分,"<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
						cout<<"\n按a重来";
						int chch = _getch();
						if (chch==97) goto start;
					}
					if (s1>21 && s2<=21)	{
						cout<<"【"<<name2<<"】"<<"is the winner!!!";
						cout<<"\n按a重来\n";
						int chch = _getch();
						if (chch==97) goto start;
					}
					if (s2>21 && s1<=21)	{
						cout<<"【"<<name1<<"】"<<"is the winner!!!";
						cout<<"\n按a重来";
						int chch = _getch();
						if (chch==97) goto start;
					}
					}
		}
		if (huoqu()==0)
		{
			cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
			Sleep(3000);
			exit(0);
		}
	}
	if (sfz()==1)
	{
		start1:
			cout<<"        21点"<<endl;
			cout<<"----------------------"<<endl;
			cout<<"		 输入1开始"<<endl;
			int n;
			cin>>n;
			if (n==1)
			{
				system("cls");
				cout<<"开始"<<endl;
				//输入昵称 
				string name1,name2;
				cout<<"请输入玩家1昵称"<<endl;
				cin>>name1;
				cout<<"请输入玩家2昵称"<<endl;
				cin>>name2;
				/**/
				string op1,op2;
				int n1=0,n2=0;
				int s1=0,s2=0;
				//玩家1 
				cout<<"请玩家1拿牌"<<endl;
				while (true)
				{
					cout<<"是否继续拿?(y/n)"<<endl;
					cin>>op1;
					if (op1=="y")
					{
						srand(time(NULL));
						s1 += (rand()%(7-1+1)+1);
						cout<<"你的牌一共有"<<s1<<endl; 
						n1++;
					}
					else if (op1=="n")
					{
						cout<<"你一共拿了"<<n1<<"张牌";
						break;
					}
					else
					{
						cout<<"请不要乱输!"; 
					}
				}
				Sleep(3000);
				system("cls");
				//玩家2 
				cout<<"请玩家2拿牌"<<endl;
				while (true)
				{
					cout<<"是否继续拿?(y/n)"<<endl;
					cin>>op2;
					if (op2=="y")
					{
						srand(time(NULL));
						s2 += (rand()%(7-1+1)+1);
						cout<<"你的牌一共有"<<s2<<endl;
						n2++;
					}
					else if (op1=="n")
					{
						cout<<"你一共拿了"<<n2<<"张牌";
						break;
					}
					else
					{
						cout<<"请不要乱输!"; 
					}
				}
				Sleep(3000);
				system("cls");
				cout<<"正在统计....."<<endl;
				Sleep(1000);
				cout<<"玩家1总分数:"<<s1<<endl;
				cout<<"玩家2总分数:"<<s2<<endl;
				if (s1<=21 && s2<=21)
				{
					if (s1>s2) {
						cout<<"【"<<name1<<"】"<<"is the winner!!!";
						cout<<"\n按a重来\n";
						int chch = _getch();
						if (chch==97) goto start1;
					}   
					if (s1==s2){
						cout<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
						cout<<"\n按a重来\n";
						int chch = _getch();
						if (chch==97) goto start1;
					}    
					if (s1<s2){
						cout<<"【"<<name2<<"】"<<"is the winner!!!";
						cout<<"\n按a重来\n";
						int chch = _getch();
						if (chch==97) goto start1;
					}   
				}
				if(s1>21 && s2>21)
				{
					cout<<"两方都超过21分,"<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
					cout<<"\n按a重来\n";
					int chch = _getch();
					if (chch==97) goto start1;
				}
				if (s1>21 && s2<=21)	{
					cout<<"【"<<name2<<"】"<<"is the winner!!!";
					cout<<"\n按a重来\n";
					int chch = _getch();
					if (chch==97) goto start1;
				}
				if (s2>21 && s1<=21)	{
					cout<<"【"<<name1<<"】"<<"is the winner!!!";
					cout<<"\n按a重来\n";
					int chch = _getch();
					if (chch==97) goto start1;
				}
			}
	}
	return 0;
 } 

Well, the effect is to enter the ID number first, and then judge

If you are a minor, you can enter the game if the time is between 8:00 pm and 9:00 pm. If it is not between 8:00 pm and 9:00 pm, you cannot enter the game.

If you are an adult, go straight to the game

Finish

haha, done

You should also have some new understanding of time.h

Bye-Bye

Guess you like

Origin blog.csdn.net/m0_64036070/article/details/124387587