Easy Tutorial
❮ Passing Parameters By References Cpp Inline Functions ❯

C Library Function - asctime()

C Standard Library - <time.h>

Description

The C library function char *asctime(const struct tm *timeptr) returns a pointer to a string which represents the day and time of the structure struct timeptr.

Declaration

Here is the declaration for the asctime() function.

char *asctime(const struct tm *timeptr)

Parameters

timeptr is a pointer to a tm structure that contains a calendar time broken down into its components:

struct tm {
   int tm_sec;         /* seconds, range 0 to 59             */
   int tm_min;         /* minutes, range 0 to 59             */
   int tm_hour;        /* hours, range 0 to 23               */
   int tm_mday;        /* day of the month, range 1 to 31    */
   int tm_mon;         /* month, range 0 to 11               */
   int tm_year;        /* The number of years since 1900     */
   int tm_wday;        /* day of the week, range 0 to 6      */
   int tm_yday;        /* day in the year, range 0 to 365    */
   int tm_isdst;       /* Daylight saving time              */    
};

Return Value

The function returns a C string containing the date and time information in a readable format Www Mmm dd hh:mm:ss yyyy, where Www is the weekday, Mmm is the month in letters, dd is the day of the month, hh:mm:ss is the time, and yyyy is the year.

Example

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

#include <stdio.h>
#include <string.h>
#include <time.h>

int main()
{
   struct tm t;

   t.tm_sec    = 10;
   t.tm_min    = 10;
   t.tm_hour   = 6;
   t.tm_mday   = 25;
   t.tm_mon    = 2;
   t.tm_year   = 89;
   t.tm_wday   = 6;

   puts(asctime(&t));

   return(0);
}

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

Sat Mar 25 06:10:10 1989

C Standard Library - <time.h>

❮ Passing Parameters By References Cpp Inline Functions ❯