Use of the CTime class format

Source: Internet
Author: User
Tags locale month name set time time and date

The format argument consists of one or more codes; As in printf, the formatting codes is preceded by a percent sign (%). Characters that does not begin with% is copied unchanged to strdest. The Lc_time category of the current locale affects the output formatting of strftime. (For more information onlc_time, see setlocale.) The formatting codes for strftime is listed below:

%a

Abbreviated weekday name

%A

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 the 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 in the printf function, the # flag may prefix any formatting code. In this case, the meaning of the format code is changed as follows.

Format Code

Meaning

% #a,% #A,% #b,% #B,% #p,% #X,% #z,% #Z,%#%

# flag is ignored.

% #c

A Long date and time representation, appropriate for the current locale. For example: "Tuesday, March 14, 1995, 12:41:29".

% #x

Long date representation, appropriate to the current locale. For example: "Tuesday, March 14, 1995".

% #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 $, win XP

Wcsftime

<time.h> or <wchar.h>

ANSI, Win 98, win Me, Win NT, Win $, 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:/t/t/t/t%s/n", tmpbuf);
_strdate (TMPBUF);
printf ("OS date:/t/t/t/t%s/n", tmpbuf);

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

/* Display UTC. */
GMT = _gmtime64 (&ltime);
printf ("Coordinated Universal time:/t/t%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 are used to skip the first 11
* characters and printf are used to trim off terminating
* characters.
*/
printf ("12-hour time:/t/t/t/t%.8s%s/n",
Asctime (today) + one, ampm);

/* Print additional time information. */
_ftime64 (&tstruct);
printf ("Plus milliseconds:/t/t/t%u/n", TSTRUCT.MILLITM);
printf ("Zone difference in hours from utc:/t%u/n",
TSTRUCT.TIMEZONE/60);
printf ("Time zone name:/t/t/t/t%s/n", _tzname[0]);
printf ("Daylight savings:/t/t/t%s/n",
Tstruct.dstflag? "YES": "NO");

/* Make time for noon on Christmas, 1993. */
if (_mktime64 (&xmas)! = (__time64_t)-1)
printf ("christmas/t/t/t/t%s/n", 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 25 12:00:00 1993

The simple points are as follows:

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);///minutes and minutes generally used two-bit tables

M_edit1=sztime;

UpdateData (FALSE);

SetTimer (1, 1000,null);

Msg1=t.format ("%d-%m-%y"); You can see the function of format

MessageBox (MSG1);

The meaning of the parameters in format is shown in the description above

Source Document

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.