Use the C library function to get the current local time

Source: Internet
Author: User

1. tm struct

Struct tm {

Int
Tm_sec;/* seconds after the minute [0-60] */
Int tm_min;
/* Minutes after the hour [0-59] */
Int tm_hour;
/* Hours since midnight [0-23] */
Int tm_mday;
/* Day of the month [1-31] */
Int tm_mon;
/* Months since January [0-11] */
Int tm_year;
/* Years since 1900 */
Int tm_wday;
/* Days since Sunday [0-6] */
Int tm_yday;
/* Days since January 1 [0-365] */
Int tm_isdst;
/* Daylight Savings Time flag */
Long tm_gmtoff;
/* Offset from CUT in seconds */
Char * tm_zone;
/* Timezone abbreviation */
};

 

2. Use localtime to obtain the local time. The Code is as follows:

 

// Obtain the current time. The C library function is used. The returned value does not need to be released. The efficiency is 3-4 times higher than that of the NSDate class using OC.
-(Struct tm *) getTime
{
// Time format
Struct timeval ticks;
Gettimeofday (& ticks, nil );
Time_t now;
Struct tm * timeNow;
Time (& now );
TimeNow = localtime (& now );
TimeNow-> tm_gmtoff = ticks. TV _usec/1000; // millisecond

TimeNow-> tm_year + = 1900; // The tm_year value in tm is from 1900 to the present.
TimeNow-> tm_mon + = 1; // The tm_mon range is 0-11

Return timeNow;
}

Related Article

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.