Strftime and Strptime Use (time-date format control function) __ function

Source: Internet
Author: User
Tags string format time and date

strftime and Strptime use

Organized from Linux programming
Both functions are format control functions of time and date, and appear to be the opposite in function. Strftime formats a TM structure as a string, strptime formats a string as a TM structure. Strftime

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 (least) MaxSize characters. Formatting string format is used to control the characters that are written to a string, which contains the normal characters that will be transferred to the string and the conversion controls that are formatted in time and date format. The conversion control character is shown in the following table

Conversion control character Description
%a Abbreviated form of the day of the week
%A The full name of the week
%b Abbreviated form of the month
%B Full name of the month
%c Date and time
%d The date in the month, 0-31
%H Hours, 00-23
%I 12 in-hour, 01-12
%j Date in the year, 001-366
%m Months of the year, 01-12
%m Minutes, 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 Geographical time zone name

The specific control converter description can view the man. Strptime

Function prototypes: Char *strptime (const char *buf,const char *format,struct TM *timeptr)

The format string is constructed in exactly the same way as the strftime format string. Strptime returns a pointer to the character after the last character processed by the conversion process.

Look at one of the programs below? View Code C

 1 2 3 4 5 6 7 8 9 to be in the "[*]" /td> 
 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <tim
  e.h>   int main () {struct TM *tm_ptr,timestruct;
  time_t the_time;
Char *buf[256];
    (void) time (&the_time);
  Tm_ptr=localtime (&the_time);
  Strftime (buf,256, "%A%d%b,%i:%s%p", tm_ptr);
printf ("Strftime gives:%s/n", buf);
  strcpy (BUF, "Sat July 2003,17:53 'll do Fine");
    printf ("Calling Strptime with:%s/n", buf);
tm_ptr+&timestruct;
    Result=strptime (buf, "%a%d%b%y,%r", tm_ptr);
printf ("Strptime consumed up to:%s/n", result);
    printf ("Strptime gives:/n");
  printf ("Date:%02d/%02d/%02d/n", tm_ptr->tm_year%100,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
  printf ("Time:%02d:%02d/n", tm_ptr->tm_hour,tm_ptr->tm_min);
Exit (0); }

After run output
Strftime gives:sunday June, 11:55 AM
Calling Strptime With:sat July 2003,17:53 'll do fine
Strptime consumed up To:will todo fine
Strptime gives:
Date:03/07/26
time:17:53

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.