asctime() and asctime_s() functions in C with Examples

asctime() function:
The asctime() function is defined in time.h header file. This function returns the pointer to the string that contains the information stored in the structure pointed to struct tm type. This function is used to return the local time defined by the system.

Syntax:

char *asctime(const struct tm* tm_ptr);

0Parameters: This function accepts single parameter time_ptr i.e pointer to the tm object to be converted.



Return Type: This function returns the calendar time in the form “Www Mmm dd hh:mm:ss yyyy”, where:

  • Www: represents the day in three letter abbrivated (Mon, Tue, Wed.., )
  • Mmm: represents the month in three letter abbrivated (Jan, Feb, Mar.., )
  • dd: represents the date in two digits (01, 02, 10, 21, 31.., )
  • hh: represents the hour (11, 12, 13, 22…, )
  • mm: represents the minutes (10, 11, 12, 45…, )
  • ss: represents the seconds (10, 20, 30…, )
  • yyyy: represents the year in four digits (2000, 2001, 2019, 2020…, )

Below program demonstrate the asctime() function in C:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to demonstrate
// the asctime() function
  
#include <stdio.h>
#include <time.h>
  
int main()
{
    struct tm* ptr;
    time_t lt;
    lt = time(NULL);
    ptr = localtime(<);
  
    // using the asctime() function
    printf("%s", asctime(ptr));
  
    return 0;
}

chevron_right


Output:

Wed Aug 14 04:21:25 2019

asctime_s() function:

This function is used to convert the given calendar time into a textual representation. We can’t modify the output calendar time in asctime() function whereas we can modify the calendar time in asctime_s() function. The general syntax of asctime_s is “Www Mmm dd hh:mm:ss yyyy“.

Syntax:

errno_t asctime_s(char *buf, rsize_t bufsz, 
                  const struct tm *time_ptr)

Parameters: This function accepts three parameters:

  • time_ptr: pointer to a tm object specifying the time to print
  • buf: pointer to a user-supplied buffer at least 26 bytes in length
  • bufsz: size of the user-supplied buffer

Return Value: This function returns pointer to a static null-terminated character string holding the textual representation of date and time. The original calendar time will be obtained from the asctime() function.

Note: In some C-compilers asctime_s() won’t be supported. We can use strftime() function instead of asctime_s() function.

Below program investigates the asctime_s() function in C:

filter_none

edit
close

play_arrow

link
brightness_4
code

// __STDC_WANT_LIB_EXT1__ is a User defined
// standard to get astime_s() function to work
#define __STDC_WANT_LIB_EXT1__ 1
  
#include <stdio.h>
#include <time.h>
  
int main(void)
{
    struct tm tm
        = *localtime(&(time_t){ time(NULL) });
    printf("%s", asctime(&tm));
  
// Calling C-standard to execute
// asctime_s() function
#ifdef __STDC_LIB_EXT1__
  
    char str[50];
  
    // Using the asctime_s() function
    asctime_s(str, sizeof str, &tm);
  
    // Print the current time
    // using the asctime_s() function
    printf("%s", str);
#endif
}

chevron_right


Output:

Wed Aug 14 04:33:54 2019

Reference: https://en.cppreference.com/w/c/chrono/asctime



My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.