C++ method to get the current system time summary
- 2020-04-02 03:03:28
- OfStack
This article illustrates how C++ gets the current system time. Share with you for your reference. The details are as follows:
Solution - advantages: use only C standard library; Cons: accurate to seconds
#include <time.h>
#include <stdio.h>
int main( void )
{
time_t t = time(0);
char tmp[64];
strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A This year the first %j day %z",localtime(&t));
puts( tmp );
return 0;
}
Scheme 2 advantages: can be accurate to the millisecond level; Cons: USES the Windows API
#include <windows.h>
#include <stdio.h>
int main( void )
{
SYSTEMTIME sys;
GetLocalTime(&sys);
printf("%4d/%02d/%02d %02d:%02d:%02d.%03d week %1d/n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
return 0;
}
Scheme three, advantages: the use of system functions, but also can modify the system time
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
system("time");
}
Plan 4: convert the current time into seconds, and then convert the corresponding time
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}
Hope that the article described in the C++ programming to help you.