Unix time-dependent structures and functions __ functions

Source: Internet
Author: User
about Unix Time ①time_t Data Typetypedef long int time_t; Used to store the number of seconds from 1970 to the present. ②timeval Structural Bodystruct Timeval
{
Long tv_sec; /* SEC * *
Long tv_usec; /* microsecond * *
};
Structure struct Timeval, it is precise to subtle. ③tm Structural Bodystruct TM
{
int tm_sec; /* seconds, normal range 0-59, but allowed to 61*/
int tm_min; * * minutes, 0-59*/
int tm_hour; * * hours, 0-23*/
int tm_mday; /* Day, that is, the first day of one months, 1-31*/
int Tm_mon; /* month, from January, 0-11*/1+p->tm_mon;
int tm_year; * * year, from 1900 so far has been many years * * * 1900+ p->tm_year;
int tm_wday; * * Week, the first day of the week, from Sunday, 0-6*/
int tm_yday; * * FROM January 1 this year to the current number of days, range 0-365*/
int tm_isdst; * * Daylight Saving Time flag/
};
It is particularly noteworthy that the year has been years since 1900, rather than direct storage such as 2011, the month starting from 0, 0 means January, week is also starting from 0, 0 for Sunday, 1 for Monday.
④ commonly used time functions:#include <time.h>
1. Asctime

Char *asctime (const struct tm* timeptr); The time that the information in the structure is converted to the real world, displayed as a string
2. CTime

Char *ctime (const time_t* TIMEP); Convert TIMEP to really world time, in string display, it differs from asctime in the form of parameters passed in, converted format: "Wed June 21:49:08 1993\n"
3. Difftime

Double Difftime (time_t time1, time_t time2); Returns the number of seconds that differ by two times
4. Gettimeofday int gettimeofday (struct timeval* tv,struct timezone*);
Returns the number of seconds and subtleties in the current distance of 1970, and the TZ is the time zone, generally not
5. Gmtime

struct tm* gmtime (const time_t* TIMEP); Converts the time represented by time_t to UTC without time zone conversion, and is a struct TM structure pointer
6. LocalTime

struct tm* localtime (const time_t* TIMEP); Similar to Gmtime, but it is time to transition through time zone.
7. Mktime

time_t mktime (struct tm* timeptr); Converts the time of the struct TM structure to the number of seconds from 1970 to the present
8. Time

time_t time (time_t* t); Gets the number of seconds since January 1, 1970. 9. Strftime

size_t strftime (char *s, size_t max, const char *format, const struct TM *TM); Converts the TM structure body to a string. Strptime

Char *strptime (const char *s, const char *format, struct TM *tm); Converts a string to a TM structure body. ⑤ a piece of code:

#define _xopen_source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main (void)
{
           struct TM TM;
           Char buf[255];
           memset (&tm, 0, sizeof (struct TM));
           Strptime ("2001-11-12 18:31:01", "%y-%m-%d%h:%m:%s", &TM);
           Strftime (buf, sizeof (BUF), "%d%b%Y%h:%m", &TM);
           Puts (BUF);
           Exit (exit_success);
}

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.