Conversion between common time and strings in Linux

Source: Internet
Author: User

In Linux, it is common to encounter time and string conversions, with two functions specifically corresponding conversions.

1. Time-to-string function strftime

Function prototypes: size_t strftime (char *s,size_t Maxsize,char *format,conststruct 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.

Input: const struct TM *timeptr

Output: Char *s,size_t Maxsize,char *format

2, string to time function strptime

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

The format string is constructed exactly like the strftime format string, and Strptime returns a pointer to the character after the last character processed by the conversion process.

Input: const char *buf,const char *format

Output: struct TM *timeptr

Experiment Code:

#include <stdio.h>
#include <time.h>


int main ()
{
struct TM Tm_time;
Strptime ("2010-11-15 10:39:30", "%y-%m-%d%h:%m:%s", &tm_time);
printf ("%ld/n", Mktime (&tm_time));
printf ("-------------------------------------/n");


Char szbuf[256] = {0};
time_t timer = time (NULL);
Strftime (szbuf, sizeof (SZBUF), "%y-%m-%d%h:%m:%s", LocalTime (&timer));
printf ("%s/n", szbuf);

return 0;
}

Operation Result:

1289788770

-------------------------------------

2017-07-11 on 19:4

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

Conversion between common time and strings in Linux

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.