The asctime_s
function converts calendar time into its textual representation. It modifies the output calendar time, unlike the asctime
function. To use this function, time.h
header file needs to be included as shown below:
#include <time.h>
The asctime_s
function is written as shown below:
errno_t asctime_s(char *buff, rsize_t buffSize, const struct tm *timePtr)
This function takes three arguments:
buff
- the pointer to a buffer supplied by the userbuffSize
- the size of buff. This is at least 26 bytes in lengthtimePtr
- the pointer to a tm
object that specifies the time to printThe return value is a static null-terminated character string. This string holds the textual representation of date and time. The asctime
function will be used to get the original calendar time. The asctime
function returns calendar time in the format below:
www Mmm dd hh:mm:ss yyyy
where:
www
- the day in three letter abbreviated form (Mon, Tue, Wed…, )mmm
- the month in three letter abbreviated form (Jan, * dd - the date in two digits (01, 10, 21, 30…, )hh
- the hour (04, 11, 12, 23…, )mm
- the minutes (10, 11, 12, 45…, )ss
- the seconds (10, 20, 30…, )yyyy
- the year in four digits (2000, 2010, 2016, 2021…, )The following code shows the use of the asctime_s
function. __STDC_WANT_LIB_EXT1__
is a user-defined standard to get the astime_s
function to work. timePtr
is being passed the local time that we wish to print. The asctime
function is used to obtain the original calendar time:
#define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>#include <time.h>int main(void){struct tm timePtr = *localtime( &(time_t){ time(NULL) } );printf("%s", asctime(&timePtr));// Calling C-standard to execute// asctime_s() function#ifdef __STDC_LIB_EXT1__char buff[32];// Using the asctime_s() functionasctime_s(buff, sizeof buff, &timePtr);// Print the current time using the asctime_s() functionprintf("%s", buff);#endif}
Free Resources