Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - localtime()

C library function - localtime()

C Standard Library - <time.h> C Standard Library -

Description

C Library Function **struct tm localtime(const time_t timer) Uses the value of timer to fill in the tm structure.timer The value of tm is broken down into a tm structure and expressed in the local time zone.

Declaration

Here is the declaration of the localtime() function.



struct tm *localtime(const time_t *timer)

Parameters

Return Value

This function returns a pointer to tm a structure containing the filled-in time information. Here are the details of the tm structure:

struct

tm

{

int

tm_sec

;

/*

Seconds, ranging from 0 to 59

*/

int

tm_min

;

/*

Minutes, ranging from 0 to 59

*/

int

tm_hour

;

/*

Hours, ranging from 0 to 23

*/

int

tm_mday

;

/*

Day of the month, ranging from 1 to 31

*/

int

tm_mon

;

/*

Month, ranging from 0 to 11

*/

int

tm_year

;

/*

Years since 1900

*/

int

tm_wday

;

/*

Day of the week, ranging from 0 to 6

*/

int

tm_yday

;

/*

Day of the year, ranging from 0 to 365

*/

int

tm_isdst

;

/*

Daylight Saving Time

*/

}

;

Example

The following example demonstrates the use of the localtime() function.

Example

#include

<

stdio.h

#include

<

time.h

int

main

(

)

{

time_t

rawtime

;

struct

tm

info

;

char

buffer

[

80

]

;

time

(

&

rawtime

)

;

info

=

localtime

(

&

rawtime

)

;

printf

(

"

Current local time and date: %s

"

,

asctime

(

info

)

)

;

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



当前的本地时间和日期:Thu Aug 23 09:12:05 2012

C Standard Library - <time.h> C Standard Library -