Linux C gets local time

Source: Internet
Author: User

in standard C + +, we can obtain the date and time through the TM structure, the TM structure is defined in Time.h as follows: #ifndef _tm_definedstruct TM {int tm_sec;/* seconds – The value interval is [0,59] */int tm_min; /* min-value interval is [0,59] */int tm_hour; /* Time-the value interval is [0,23] */int tm_mday; /* Date in one months-the value interval is [1,31] */int Tm_mon; /* Month (starting from January, 0 for January)-the value interval is [0,11] */int tm_year; /* year, whose value starts at 1900 */int Tm_wday; /* Week – The value interval is [0,6], where 0 stands for Sunday, 1 for Monday, and so on */

int tm_yday; /* Number of days starting January 1 of each year – the value interval is [0,365], where 0 is January 1, 1 is January 2, and so on.

int tm_isdst; /* Daylight Saving time identifier, the TM_ISDST is positive when daylight savings is applied. When daylight saving time is not practiced, the TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */

long int tm_gmtoff; /* Specifies the date Line East time zone in UTC East time zone positive seconds or UTC West time zone negative seconds */const Char *tm_zone; /* The name of the current time zone (related to the environment variable TZ) */}; #define _TM_DEFINED#ENDIFANSI C Standard says that the time to use the TM structure is expressed as the decomposition time (broken-down times). You can use the function localtime () to convert the time () time_t structure to a TM structure. Put down one of your own implementations of the code.

1 {2 3time_t now;//The time type used to store the number of seconds from January 1, 1970 00:00:00 to now4     inthour;5     intmin;6     intI=0;7     inttemp;8     structTM *timenow;//structure of the time type9     Charlogtime[6]; TenTime (&now);//get calendar time, that's how many seconds OneTimeNow = LocalTime (&now);//Convert calendar time to local TM format time Ahour=timenow->tm_hour;//when you take out one of them -temp=hour; -logtime[i++]=temp/Ten+'0'; thelogtime[i++]=hour%Ten+'0'; -logtime[i++]=':';//Converts a time to a string into a logtime, with the same operation as the following -Min=timenow->tm_min; -temp=min; +logtime[i++]=temp/Ten+'0'; -logtime[i++]=min%Ten+'0'; +logtime[i]=' /'; Aprintf"%s\n", logtime); at}

This gets the hour and minute of the current time.

Linux C gets local time

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.