C ++ How to get the current system time?

1, call the cmd function to get the current time

This module is not particularly troublesome, because this method is the direct use cmd command and get a way to change the system time,

But do not suggest that you use, especially those with antivirus software, since this program involves some changes to the system, of course, very easily be mistaken for a Trojan virus, so they took Lenovo's Yang days, the installed capacity comes Mike Philippines met with some program you want to change the system of direct closure, not as 360 antivirus Trojan blacklist first included in the program, but you can force yourself to run, that ado, first look at the program:

#include <stdlib.h>
using namespace std;

int main()
{
	system("time");
	return 0;
}

Of course, I do not speak here of the command system, we need, then you can refer to my other blog post: C ++ calls in cmd command method

I tried to get it in here just to show the effect after running this program:
cmd-time Example 1

2, the identification system API to get the current time

The system uses an internal API to get the current time is my favorite use, it is not only good, but tall on! Ahem, sorry, wrong, should be:It is not only easy to use, and accurate, but also because, very easy to understandThis is for the novice white never had the Gospel!

That direct look at the program:

#include <stdio.h>
#include <windows.h>
using namespace std;

int main()
{
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	printf("%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
	return 0;
}

Here I think it is necessary to look at, sys.wYear here is a figure for, sys.wDayOfWeek there is a week day of the week, and so on, in front of every word in the English are a plusLowercase "w" command is to call this function

Then we look at a picture:
API acquisition time Example 1

Of course, the junior partner will find attentive,The end result printf inside some space and "/" These things we all can be modified, because this style is to control the output does not affect the program, Like on this chart below:

Acquisition time API Example 2

Finally, more than one mouth:API command acquired by the time can be directly stored as variables, that is, whether the variable can be used directly

For example, on a program just recently completed, I used the API commands to get the time, and defined as a variable of type int, and finally realize the function of judge ~~

3, direct access to the system time

One way law is more difficult to understand the direct access, but will use it once, it is called aUnimpededHowever, this method is difficult to understand how in the end it?

Analogy:There is a 100 meter tree, every day during the day, you can rise 2 cm, at night, you want to swap one centimeter, that is to say, a whole day can only rise by 1 cm, but the tree is now clear height of 100 meters, calculate how much days, then imagine yourself if I am afraid the number of days in that really the case, not just a number that simple

That love for the challenges of a small partner, this is undoubtedly aGreat opportunityThat we have to look at the code it!

#include<iostream>  
#include<time.h>  
using namespace std;  
  
int main()  
{  
 
	//获取系统时间  
	time_t now_time=time(NULL);  
	//获取本地时间  
	tm*  t_tm = localtime(&now_time);  
	//转换为年月日星期时分秒结果,如图:  
	printf("local time is    : %s\n", asctime(t_tm));  
	//将时间转换为秒  
	time_t mk_time = mktime(t_tm);  
	
	//也可以自己定义一个时间  
	//定义截止时间  
	struct tm deadline_tm;  
	deadline_tm.tm_sec=0;//[0~59]  
	deadline_tm.tm_min=10;//[0~59]  
	deadline_tm.tm_hour=13;//[0~23]  
	deadline_tm.tm_isdst=0;//default  
	deadline_tm.tm_mday=31;//[1~31]  
	deadline_tm.tm_mon=2;//[0~11]  
}

Many small partners include I see so much code and comments,Skull pain fast with Wantong bones stickers!Emm. . . (Suddenly feels like implantable advertising) but do not pull the skull , we take a look at this interpretation of the code:

The first is the header file:

#include<iostream>  
#include<time.h>  

This should not have too much to explain, is the ordinary two header files

Followed by obtaining two kinds of time:

  • 1. Get the system time
  • 2, get local time
//获取系统时间  
time_t now_time=time(NULL);  
//获取本地时间  
tm*  t_tm = localtime(&now_time);

That these two get time what use is it, in fact, where I tried to get only one time to get the current time, but my computer run like no, but we can try, for example, the comment period, etc. but here I would like to remind everyone that:There are two kinds of time correlation, that is, not this time, then next time you can not perform the functions that operate

//转换为年月日星期时分秒结果,如图:  
printf("local time is    : %s\n", asctime(t_tm));  
//将时间转换为秒  
time_t mk_time = mktime(t_tm);  

This is just a section of the module with the conversion operation, when the output back to facilitate visualization of otherwise. . . (We can not try to convert the operation of the module is something, what I do not understand ~)

Finally, speaking about the last of a module:

//定义截止时间  
struct tm deadline_tm;  
deadline_tm.tm_sec=0;//[0~59]  
deadline_tm.tm_min=10;//[0~59]  
deadline_tm.tm_hour=13;//[0~23]  
deadline_tm.tm_isdst=0;//default  
deadline_tm.tm_mday=31;//[1~31]  
deadline_tm.tm_mon=2;//[0~11]  

This I do not understand, just aTells the compiler that this time is 60 hexadecimal just it!Suddenly feeling the computer is a "wise fool"

Well, that's today's content, then I will continue to update my blog, welcome attention to my blog to see more articles! Also welcome to publish some of their own experience in the community and found inside CSDN! Bye!

Released five original articles · won praise 6 · views 110

Guess you like

Origin blog.csdn.net/dxfjdvsijdfhiv/article/details/104891989