C. Small problem in obtaining local time

Source: Internet
Author: User

A friend recently asked the following questions:

# Include <stdio. h>
# Include <stdlib. h>
# Include <iconv. h>
# Include <time. h>

Int main ()
{
Struct TM * TMS;
Time_t now = time (0 );
TMS = localtime (& now );

Printf ("% d/N", TMS-> tm_year );
Printf ("% d/N", TMS-> tm_mon );
Printf ("% d/N", TMS-> tm_mday );
Printf ("% d/N", TMS-> tm_hour );
Printf ("% d/N", TMS-> tm_min );
Printf ("% d/N", TMS-> tm_sec );
} Why is the output year 110, not 2010? What is the output month of August instead of July?

This problem is clearly explained on msdn. I don't have to worry about it. Just come up with evidence.

Return Value

LocaltimeReturns a pointer to the structure result. If the value inTimerRepresents a date before midnight, January 1, 1970,LocaltimeReturnsNull. The fields of the Structure Type TM store the following values, each of which isInt:

Tm_sec

Seconds after minute (0-59)

Tm_min

Minutes after hour (0-59)

Tm_hour

Hours after midnight (0-23)

Tm_mday

Day of month (1-31)

Tm_mon

Month (0-11; January = 0) // pay attention to this

Tm_year

Year (current year minus 1900) // pay attention to this

Tm_wday

Day of week (0-6; Sunday = 0)

Tm_yday

Day of year (0-365; January 1 = 0)

Tm_isdst

Positive Value If Daylight Saving Time is in effect; 0 If Daylight Saving Time is not in effect; negative value if status of Daylight Saving Time is unknown. the C run-time library assumes the United States's Rules for Implementing the calculation of Daylight Saving Time (DST ).

Parameter

Timer

Pointer to stored time

Remarks

TheLocaltimeFunction converts a time stored as a time_t value and stores the result in a structure of TypeTM.LongValueTimerRepresents the seconds elapsed since midnight (00:00:00), January 1, 1970, Coordinated Universal Time (UTC). This value is usually obtained fromTimeFunction.

Gmtime,Mktime, AndLocaltimeAll use a single statically allocatedTMStructure for the conversion. Each call to one of these routines destroys the result of the previous call.

LocaltimeCorrects for the local time zone if the user first sets the global environment variableTZ. WhenTZIs set, three other environment variables (_ Timezone,_ Daylight, And_ Tzname) Are automatically set as well. See _ tzset For a description of these variables.TZIs a Microsoft extension and not part of the ANSI standard definitionLocaltime.

NoteThe target environment shocould try to determine whether Daylight Saving Time is in effect.

Example

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.