Struct tm-& gt; time () localtime () gmtime (), localtimegmtime

Source: Internet
Author: User
Tags epoch time

Struct tm-> time () localtime () gmtime (), localtimegmtime

Struct tm-> time () localtime () gmtime ()

Struct tm {int tm_sec;/* indicates the current number of seconds. The normal range is 0-59, but the value can be 61 seconds */int tm_min;/* indicates the current score, the value range is 0-59 */int tm_hour;/* The number of hours from midnight. The value range is 0-23 */int tm_mday;/* The number of days of the current month, the value range is 01-31 */int tm_mon;/* indicates the current month. The value ranges from 0 to 11 x/int tm_year; /* Number of years since January 1, 1900 */int tm_wday;/* Number of days of a week, from Monday to 0 */int tm_yday; /* The number of days since January 1, January 1 this year, ranging from 0 to 365 */int tm_isdst;/* The time-saving flag for daylight saving */};

Time ()

Functions in C language. Header file:Time. h Function prototype:Time_t time (time_t * timer) Function:Obtain the current system Time. The returned result is of the time_t type, which is actually a large integer. The value indicates 00:00:00, January 1, January 1, 1970 (called Epoch Time of UNIX system) from CUT (Coordinated Universal Time) the number of seconds to the current time point. Call localtime to convert the CUT time represented by time_t to the local time (we are in the + 8 zone, 8 hours more than the CUT time) and convert it to the struct tm type, each data member of this type represents year, month, day, hour, minute, and second respectively. Note: The prototype of the time function can also be interpreted as long time (long * tloc), which returns a long integer. Because in the header file time. h, time_t is actually:
#ifndef _TIME_T_DEFINEDtypedef long time_t; /* time value */#define _TIME_T_DEFINED /* avoid multiple defines of time_t */#endif
That is, long.
# Include <stdio. h> # include <stddef. h> # include <time. h> int main (void) {time_t timer; // time_t is the long int type struct tm * tblock; timer = time (NULL ); // This sentence can also be changed to time (& timer); tblock = localtime (& timer); printf ("Local time is: % s \ n ", asctime (tblock); return 0 ;}
  Localtime () Benefits:Converts the number of seconds that the system has offset from 00:00 to the current time to the calendar time. Note:The time of the tm struct obtained by this function is the time when the time zone has been converted to the local time. Usage:Struct tm * localtime (const time_t * clock ); Return Value: Returns the pointer to the tm struct. the tm struct is time. the struct defined in h is used to separately store the amount of time (year, month, day, and so on. note that the year plus 1900 and the month plus 1.
# Include <time. h> # include <stdio. h> int main () {struct tm * t; time_t tt; time (& tt); t = localtime (& tt ); printf ("% 4d % 02d % 02d % 02d: % 02d: % 02d \ n", t-> tm_year + 1900, t-> tm_mon + 1, t-> tm_mday, t-> tm_hour, t-> tm_min, t-> tm_sec); return 0 ;}

Gmtime ()

Header file:Time. h Prototype:Struct tm * gmtime (long * clock ); Function:Convert date and time Greenwich Mean Time (GMT). Convert the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then return the result from the structure tm. Time and date returned by this function Without time zone conversion, But UTC time. Return ValueThe returned structure tm indicates the current UTC time.
#include "stdio.h"#include "time.h"#include "stdlib.h"int main(void){time_t t;struct tm *gmt, *area;tzset(); /* tzset()*/t = time(NULL);area = localtime(&t);printf("Local time is: %s", asctime(area));gmt = gmtime(&t);printf("GMT is: %s", asctime(gmt));return 0;}

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.