Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

localtime, localtime64 - Date and current time as a structure

&pagelevel(4)&pagelevel

Definition   

#include <time.h>

struct tm *localtime(const time_t *sec_p);
struct tm *localtime64(const time64_t *sec_p);

localtime and localtime64 interpret the time specification to which sec_p points as the
number of seconds which have passed since the reference date (epoch). The functions
calculate the date and time from this and store the result in a structure of the type tm.
Negative values are interpreted as seconds before the reference date. The earliest
displayable date with localtime is 12/13/1901 20:45:52 local time and 01/01/1900
00:00:00 with localtime64.

The latest date which can be displayed with localtime is 01/19/2038 03:14:07 and
3/18/4317 02:44:48 with localtime64.

Return val.

Pointer to the calculated structure. localtime and localtime64 store the result in a structure declared in <time.h> as follows:



struct tm
{
  int   tm_sec;        /* seconds (0-59) */
  int   tm_min;        /* minutes (0-59) */
  int   tm_hour;       /* hours (0-23) */
  int   tm_mday;       /* day of the month (1-31) */
  int   tm_mon;        /* month from the start of the year (0-11) */
  int   tm_year;       /* years since 1900 */
  int   tm_wday;       /* weekday (0-6, Sunday=0) */
  int   tm_yday;       /* days since January 1 (0-365) */
  int   tm_isdst;      /* daylight saving time flag */
};


NULL

In the event of an error

Notes

The asctime, ctime, ctime64, gmtime, gmtime64, localtime and localtime64 functions write their result into the same internal C data area. This means that each of these function calls overwrites the previous result of any of the other functions.

localtime and localtime64 map all dates before 1/1/1900 01:00:00 to 1/1/1900 01:00:00.

Example

#include <time.h>
#include <stdio.h>
struct tm *t;
time_t clk;
char *s;
int main(void)
{
  clk = time((time_t *)0);
  t = localtime(&clk);
  printf("Year: %d\n", t->tm_year + 1900);
  printf("Time in hours: %d\n", t->tm_hour);
  printf("Day of the year: %d\n", t->tm_yday);
  s = asctime(t);
  printf("%s", s);
  return 0;
}