Custom Time Format
We can usestrftime() function to format the time as we want. Its prototype is as follows:
size_tstrftime(
Char *strdest,
size_t MaxSize,
const Char *format,
Const struct TM *timeptr
);
We can place the time information stored in the timeptr in the string pointed to by the strdest, with a maximum of maxsize characters in the strdest, according to format commands in format point to String. The function returns the number of characters placed in the string pointed to by strdest.
Functionstrftime() is somewhat similar to sprintf (): Identifies a collection of formatting commands that begin with a percent sign (%), and the formatted output is placed in a string. The format command describes the exact representation of the various date and time information in the string strdest. Other characters in the format string are placed in the string as is. The format commands are listed below, and they are case-sensitive.
%a shorthand for the day of the week
%A full name of the week
%b of the Month
Full name of the%B month
Time string for the date of%c standard
After two digits of the%c year
The day ordinal of a month in%d decimal notation
%d Month/day/year
%e the day ordinal of a month in a two-character field, in decimal notation
%F year-month-day
%g two digits of the year, using week-based
%G years, using week-based years
%h Abbreviated month name
%H 24-Hour Hour
%I 12-Hour Hour
%j decimal indicates the day ordinal of the year
%m the month represented by decimal
%M minutes in a 10 o'clock-hour representation
%n New Line character
%p equivalent display of the local AM or PM
%r 12 hours of time
%R display hours and minutes: hh:mm
%s number of seconds in decimal
%t Horizontal Tab
%T display time seconds: hh:mm:ss
%u days of the week, Monday for the first day (values from 0 to 6, Monday to 0)
%u year of the week, the Sunday as the first day (value from 0 to 53)
%V Week of the year, using week-based
%w Decimal Day of the week (value from 0 to 6, Sunday is 0)
%W Week of the year, Monday as the first day (value from 0 to 53)
Date string for%x standard
Time string for%x standard
%y decimal Year without century (values from 0 to 99)
%Y decimal year with century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent hundred percent semicolon
If you want to show what time it is now and display it as a 12-hour system, just like this procedure:
#include "time.h"
#include "stdio.h"
int main (void)
{
struct TM *ptr;
time_t lt;
Char str[80];
Lt=time (NUL);
Ptr=localtime (&LT);
strftime(Str,100,"it is now%I%p",ptr);
printf (str);
return 0;
}
The result of this operation is:
It's now 4PM
The following program displays the current full date:
#include <stdio.h>
#include <time.h>
void Main (void)
{
struct TM *newtime;
Char tmpbuf[128];
time_t LT1;
Time (&LT1);
Newtime=localtime (&LT1);
strftime(Tmpbuf, " Today is%A, day%d of%B in the year%y./n", NewTime);
printf (TMPBUF);
}
Operation Result:
Today is Saturday, Day's July in the year 2005.
==========================================================strftime and Strptime use
Both functions are format-controlled functions of the time-date, and appear to be functionally opposite. Strftime formats a TM structure as a string, and strptime formats a string as a TM structure.
Function prototypes: size_t strftime (char *s,size_t maxsize,char *format,const struct TM *timeptr)
The Strftime function formats the time and date represented by the TM structure that the timeptr points to, and the result is placed in the string s. The length of the string is set to a minimum of maxsize characters. Format strings are used to control the characters that are written to the string, which contains the ordinary characters that will be passed to the string, and the conversion controls that arrange the time and date formats. The conversion control is shown in the following table
Convert a control character |
Description |
%a |
Abbreviated form of the day of the week |
%A |
Full name of the day of the week |
%b |
Abbreviated form of the month |
%B |
Full name of the month |
%c |
Date and time |
%d |
Date in the month, 0-31 |
%H |
Hours, 00-23 |
%I |
12 hours of input, 01-12 |
%j |
Date in year, 001-366 |
%m |
Month in year, 01-12 |
%M |
Points, 00-59 |
%p |
Morning or afternoon |
%s |
Seconds, 00-60 |
%u |
Day of the week, 1-7 |
%w |
Day of the week, 0-6 |
%x |
Date in local format |
%x |
Time in local format |
%y |
Last two digits in the year, 00-99 |
%Y |
Years |
%Z |
Geographic time zone Name |
The specific control translator description allows you to view the man.
Function prototypes: Char *strptime (const char *buf,const char *format,struct TM *timeptr)
The format string is constructed exactly like the strftime format string. Strptime returns a pointer to the character that follows the last character processed by the conversion process.
After Linux acquires time--Custom time format