C ++ obtains the current system time

Source: Internet
Author: User
Tags month name

C ++ obtains the current system time

# Include

// * Method 1

Time_t tt = time (NULL); // The returned result is only a time (cuo ).

Tm * t = localtime (& tt );

Printf ("% d-% 02d-% 02d % 02d: % 02d: % 02dn ",

T-> maid + 1900,

T-> tm_mon + 1,

T-> tm_mday,

T-> tm_hour,

T-> tm_min,

T-> tm_sec );

// * Method 2

SYSTEMTIME st = {0 };

GetLocalTime (& st );

Printf ("% d-% 02d-% 02d % 02d: % 02d: % 02dn ",

St. wYear,

St. wMonth,

St. wDay,

St. wHour,

St. wMinute,

St. wSecond );

Below a few, is online to find: Reprint address: http://apps.hi.baidu.com/share/detail/17815869

I personally think the second one is more practical and most commonly used ~

However, do not forget second when computing algorithms are time-consuming. You can't just use Milliseconds to reduce them without negative values. If the algorithm is time-consuming, you must use minutes. Otherwise, hours ......

// Solution-advantage: only the C standard library is used; disadvantage: only seconds are allowed

# Include

# Include

Int main (void)

{

Time_t t = time (0 );

Char tmp [64];

Strftime (tmp, sizeof (tmp), "% Y/% m/% d % X % A % j day % z this year", localtime (& t ));

Puts (tmp );

Return 0;

}

Size_t strftime (char * strDest, size_t maxsize, const char * format, const struct tm * timeptr );

Generate a string based on the format string.

Struct tm * localtime (const time_t * timer );

Obtain the local time. the result obtained by localtime is returned by the structure tm.

The returned string can be in the following format:

% A abbreviation of the day of the week. Eg: Tue

% A full name of the day of the week. Eg: Tuesday

The abbreviation of % B month name.

% B full name of the month name.

% C the local date time is better than the string.

% D indicates the day of the month (range: 00 to 31) with a number ). Date

% H indicates the hour in the 24-hour format (ranging from 00 to 23 ).

% I represents the hour in 12-hour format (range: 01 to 12 ).

% J indicates the day of the year (range: 001 to 366 ).

The number of % m months (ranging from 1 to 12 ).

% M minutes.

% P uses ''am'' or ''pm ''to indicate the local time.

% S seconds.

% U indicates the week of the current year. The first week starts from the first Sunday.

% W indicates the week of the current year. The first week starts from the first Monday.

% W indicates the day of the week by number (0 indicates Sunday ).

% X date representation without time.

% X does not include the time representation of the date. Eg: 15:26:30

The two digits % y indicate the year (range: 00 to 99 ).

% Y indicates the complete year number, that is, the four-digit number. Example: 2008

% Z (% z) Time Zone or abbreviation. Eg: China Standard Time

% Characters.

// Solution 2 advantages: accurate to milliseconds; disadvantage: windows API is used

# Include

# Include

Int main (void)

{

SYSTEMTIME sys;

GetLocalTime (& sys );

Printf ("% 4d/% 02d/% 02d % 02d: % 02d: % 02d. % 03d week % 1dn ", sys. wYear, sys. wMonth, sys. wDay, sys. wHour, sys. wMinute, sys. wSecond, sys. wMilliseconds, sys. wDayOfWeek );

Return 0;

}

// Solution 3. Advantages: using system functions, you can modify the system time.

// This file must be a c ++ File

# Include

# Include

Using namespace std;

Void main ()

{

System ("time ");

}

// Solution 4: Convert the current time to seconds, and then use the corresponding time to convert

// This file must be a c ++ File

# Include

# Include

Using namespace std;

Int main ()

{

Time_t now_time;

Now_time = time (NULL );

Cout <

Return 0;

}

Note: GetLocalTime () and GetSystemTime () are different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.