Ctime format in VC ++

Source: Internet
Author: User
Tags month name time and date
Ctime format in VC ++
16:26:51 Tue | read (576) times
 


Ctime format usage
You can use the ctime class to conveniently obtain the current system time and convert it to various formats.

TheFormatArgument consists of one or more codes; as inPrintf, The formatting codes are preceded by a percent sign (%). Characters that do not begin%Are copied unchangedStrdest.TheLc_timeCategory of the current locale affects the output formattingStrftime. (For more information onLc_time, See setlocale.) The formatting codesStrftimeAre listed below:
%
Abbreviated weekday name
%
Full weekday name
% B
Abbreviated month name
% B
Full month name
% C
Date and Time Representation appropriate for locale
% D
Day of month as decimal number (01-31)
% H
Hour in 24-hour format (00-23)
% I
Hour in 12-hour format (01-12)
% J
Day of year as decimal number (001-366)
% M
Month as decimal number (01-12)
% M
Minute as decimal number (00-59)
% P
Current locale's a. m./P. M. Indicator for 12-hour clock
% S
Second as decimal number (00-59)
% U
Week of year as decimal number, with Sunday as first day of week (00-53)
% W
Weekday as decimal number (0-6; Sunday is 0)
% W
Week of year as decimal number, with Monday as first day of week (00-53)
% X
Date representation for current locale
% X
Time Representation for current locale
% Y
Year without Century, as decimal number (00-99)
% Y
Year with Century, as decimal number
% Z, % Z
Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
%
Percent sign

As inPrintfFunction,#Flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.

Format code Meaning
% #,% #,% # B,% # B,% # P,% # X,% # Z,% # Z,% # % #Flag is ignored.
% # C Long Date and Time Representation, appropriate for current locale. For example: "Tuesday, March 14,199 5, 12:41:29 ".
% # X Long date representation, appropriate to current locale. For example: "Tuesday, March 14,199 5 ".
% # D,% # H,% # I,% # J,% # M,% # M,% # S,% # U,% # W,% # W,% # Y,% # Y Remove leading zeros (if any ).
Requirements
Routine Required Header Compatibility
Strftime <Time. h> ANSI, WIN 98, win me, Win NT, Win 2000, Win XP
Wcsftime <Time. h> or <wchar. h> ANSI, WIN 98, win me, Win NT, Win 2000, Win XP


Example

// crt_times.c
/* This program demonstrates these time and date functions:
* _time64 _ftime64 _ctime64 asctime
* _localtime64 _gmtime64 _mktime64 _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

int main()
{
char tmpbuf[128], ampm[] = "AM";
__time64_t ltime;
struct __timeb64 tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();

/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:tttt%sn", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:tttt%sn", tmpbuf );

/* Get UNIX-style time and display as number and string. */
_time64( &ltime );
printf( "Time in seconds since UTC 1/1/70:t%ldn", ltime );
printf( "UNIX time and date:ttt%s", _ctime64( &ltime ) );

/* Display UTC. */
gmt = _gmtime64( &ltime );
printf( "Coordinated universal time:tt%s", asctime( gmt ) );

/* Convert to time structure and adjust for PM if necessary. */
today = _localtime64( &ltime );
if( today->tm_hour >= 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;

/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:tttt%.8s %sn",
asctime( today ) + 11, ampm );

/* Print additional time information. */
_ftime64( &tstruct );
printf( "Plus milliseconds:ttt%un", tstruct.millitm );
printf( "Zone difference in hours from UTC:t%un",
tstruct.timezone/60 );
printf( "Time zone name:tttt%sn", _tzname[0] );
printf( "Daylight savings:ttt%sn",
tstruct.dstflag   "YES" : "NO" );

/* Make time for noon on Christmas, 1993. */
if( _mktime64( &xmas ) != (__time64_t)-1 )
printf( "Christmastttt%sn", asctime( &xmas ) );

/* Use time structure to build a customized time string. */
today = _localtime64( &ltime );

/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.n", today );
printf( tmpbuf );
}
Sample output

OS time:                                14:15:49
OS date: 02/07/02
Time in seconds since UTC 1/1/70: 1013120149
UNIX time and date: Thu Feb 07 14:15:49 2002
Coordinated universal time: Thu Feb 07 22:15:49 2002
12-hour time: 02:15:49 PM
Plus milliseconds: 455
Zone difference in hours from UTC: 8
Time zone name: Pacific Standard Time
Daylight savings: NO
Christmas Sat Dec 25 12:00:00 1993

The following is a simple example:
Cstring msg1 = "aaaaaaaaaaa ";

Killtimer (1 );

Ctime T = ctime: getcurrenttime ();
Char sztime [8];
Int nhour = T. gethour ();
Int nminute = T. getminute ();
Int nsecond = T. getsecond ();
Wsprintf (sztime, "% 02i: % 02i: % 02i", nhour, nminute, nsecond); // two tables are generally used in minutes.
M_edit1 = sztime;
Updatedata (false );
Settimer (1, 1000, null );
Msg1 = T. Format ("% d-% m-% Y"); // you can see the format function.
MessageBox (msg1 );

For the parameter meanings in format, see the preceding description.

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.